Client/MainForm.cs
author sl
Sat, 24 Jan 2015 11:19:28 +0100
changeset 102 7e529cdd3391
parent 80 408ef0501cb7
child 106 32270ff62819
permissions -rw-r--r--
Now properly disposing of tray icon on exit.
sl@18
     1
using System;
sl@18
     2
using System.Collections.Generic;
sl@18
     3
using System.ComponentModel;
sl@18
     4
using System.Data;
sl@18
     5
using System.Drawing;
sl@18
     6
using System.Linq;
sl@18
     7
using System.Text;
sl@18
     8
using System.Threading.Tasks;
sl@18
     9
using System.Windows.Forms;
sl@18
    10
using System.ServiceModel;
sl@18
    11
using System.ServiceModel.Channels;
sl@31
    12
using System.Diagnostics;
sl@55
    13
using SharpDisplay;
sl@20
    14
sl@18
    15
sl@18
    16
namespace SharpDisplayClient
sl@18
    17
{
sl@18
    18
    public partial class MainForm : Form
sl@18
    19
    {
sl@73
    20
        DisplayClient iClient;
sl@73
    21
sl@43
    22
        ContentAlignment Alignment;
sl@72
    23
        DataField iTextFieldTop;
sl@18
    24
sl@18
    25
        public MainForm()
sl@18
    26
        {
sl@18
    27
            InitializeComponent();
sl@43
    28
            Alignment = ContentAlignment.MiddleLeft;
sl@72
    29
            iTextFieldTop = new DataField(0);
sl@18
    30
        }
sl@18
    31
sl@18
    32
sl@18
    33
        private void MainForm_Load(object sender, EventArgs e)
sl@18
    34
        {
sl@73
    35
            iClient = new DisplayClient(this);
sl@73
    36
            iClient.Open();
sl@18
    37
sl@29
    38
            //Connect using unique name
sl@32
    39
            //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@32
    40
            string name = "Client-" + (iClient.ClientCount() - 1);
sl@32
    41
            iClient.SetName(name);
sl@30
    42
            //Text = Text + ": " + name;
sl@32
    43
            Text = "[[" + name + "]]  " + iClient.SessionId;
sl@18
    44
sl@33
    45
            //
sl@33
    46
            textBoxTop.Text = iClient.Name;
sl@33
    47
            textBoxBottom.Text = iClient.SessionId;
sl@33
    48
sl@18
    49
        }
sl@21
    50
sl@31
    51
sl@60
    52
sl@31
    53
        public delegate void CloseConnectionDelegate();
sl@31
    54
        public delegate void CloseDelegate();
sl@31
    55
sl@31
    56
        /// <summary>
sl@60
    57
        ///
sl@31
    58
        /// </summary>
sl@31
    59
        public void CloseConnectionThreadSafe()
sl@21
    60
        {
sl@31
    61
            if (this.InvokeRequired)
sl@29
    62
            {
sl@31
    63
                //Not in the proper thread, invoke ourselves
sl@31
    64
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31
    65
                this.Invoke(d, new object[] { });
sl@29
    66
            }
sl@31
    67
            else
sl@31
    68
            {
sl@31
    69
                //We are in the proper thread
sl@31
    70
                if (IsClientReady())
sl@31
    71
                {
sl@74
    72
                    string sessionId = iClient.SessionId;
sl@74
    73
                    Trace.TraceInformation("Closing client: " + sessionId);
sl@31
    74
                    iClient.Close();
sl@74
    75
                    Trace.TraceInformation("Closed client: " + sessionId);
sl@31
    76
                }
sl@29
    77
sl@31
    78
                iClient = null;
sl@31
    79
            }
sl@26
    80
        }
sl@26
    81
sl@31
    82
        /// <summary>
sl@60
    83
        ///
sl@31
    84
        /// </summary>
sl@31
    85
        public void CloseThreadSafe()
sl@31
    86
        {
sl@31
    87
            if (this.InvokeRequired)
sl@31
    88
            {
sl@31
    89
                //Not in the proper thread, invoke ourselves
sl@31
    90
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31
    91
                this.Invoke(d, new object[] { });
sl@31
    92
            }
sl@31
    93
            else
sl@31
    94
            {
sl@31
    95
                //We are in the proper thread
sl@31
    96
                Close();
sl@31
    97
            }
sl@31
    98
        }
sl@31
    99
sl@31
   100
sl@26
   101
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
   102
        {
sl@31
   103
            CloseConnectionThreadSafe();
sl@29
   104
        }
sl@29
   105
sl@29
   106
        public bool IsClientReady()
sl@29
   107
        {
sl@73
   108
            return (iClient != null && iClient.IsReady());
sl@21
   109
        }
sl@43
   110
sl@43
   111
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@43
   112
        {
sl@43
   113
            Alignment = ContentAlignment.MiddleLeft;
sl@43
   114
            textBoxTop.TextAlign = HorizontalAlignment.Left;
sl@43
   115
            textBoxBottom.TextAlign = HorizontalAlignment.Left;
sl@43
   116
        }
sl@43
   117
sl@43
   118
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@43
   119
        {
sl@43
   120
            Alignment = ContentAlignment.MiddleCenter;
sl@43
   121
            textBoxTop.TextAlign = HorizontalAlignment.Center;
sl@43
   122
            textBoxBottom.TextAlign = HorizontalAlignment.Center;
sl@43
   123
        }
sl@43
   124
sl@43
   125
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@43
   126
        {
sl@43
   127
            Alignment = ContentAlignment.MiddleRight;
sl@43
   128
            textBoxTop.TextAlign = HorizontalAlignment.Right;
sl@43
   129
            textBoxBottom.TextAlign = HorizontalAlignment.Right;
sl@43
   130
        }
sl@43
   131
sl@43
   132
        private void buttonSetTopText_Click(object sender, EventArgs e)
sl@43
   133
        {
sl@43
   134
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   135
            iTextFieldTop.Text = textBoxTop.Text;
sl@60
   136
            iTextFieldTop.Alignment = Alignment;
sl@81
   137
            bool res = iClient.SetField(iTextFieldTop);
sl@81
   138
sl@81
   139
            if (!res)
sl@81
   140
            {
sl@81
   141
                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@81
   142
            }
sl@81
   143
sl@81
   144
sl@43
   145
        }
sl@43
   146
sl@43
   147
        private void buttonSetText_Click(object sender, EventArgs e)
sl@43
   148
        {
sl@78
   149
            //Set one column two lines layout
sl@78
   150
            TableLayout layout = new TableLayout(1, 2);
sl@78
   151
            iClient.SetLayout(layout);
sl@78
   152
sl@78
   153
            //Set our fields
sl@80
   154
            iClient.CreateFields(new DataField[]
sl@60
   155
            {
sl@72
   156
                new DataField(0, textBoxTop.Text, Alignment),
sl@72
   157
                new DataField(1, textBoxBottom.Text, Alignment)
sl@43
   158
            });
sl@43
   159
        }
sl@62
   160
sl@62
   161
        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62
   162
        {
sl@65
   163
            //Define a 2 by 2 layout
sl@62
   164
            TableLayout layout = new TableLayout(2,2);
sl@65
   165
            //Second column only takes up 25%
sl@63
   166
            layout.Columns[1].Width = 25F;
sl@65
   167
            //Send layout to server
sl@62
   168
            iClient.SetLayout(layout);
sl@62
   169
        }
sl@67
   170
sl@68
   171
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   172
        {
sl@68
   173
            int x1 = 0;
sl@68
   174
            int y1 = 0;
sl@68
   175
            int x2 = 256;
sl@68
   176
            int y2 = 32;
sl@68
   177
sl@68
   178
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   179
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   180
sl@67
   181
            // Draw line to screen.
sl@67
   182
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   183
            {
sl@67
   184
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   185
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   186
            }
sl@67
   187
sl@72
   188
            DataField field = new DataField(0, bitmap);
sl@80
   189
            //field.ColumnSpan = 2;
sl@74
   190
            iClient.SetField(field);
sl@70
   191
        }
sl@70
   192
sl@71
   193
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   194
        {
sl@71
   195
            SetLayoutWithBitmap();
sl@71
   196
        }
sl@71
   197
sl@71
   198
        /// <summary>
sl@71
   199
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   200
        /// </summary>
sl@71
   201
        private void SetLayoutWithBitmap()
sl@70
   202
        {
sl@70
   203
            //Define a 2 by 2 layout
sl@70
   204
            TableLayout layout = new TableLayout(2, 2);
sl@71
   205
            //First column only takes 25%
sl@70
   206
            layout.Columns[0].Width = 25F;
sl@73
   207
            //Second column takes up 75%
sl@70
   208
            layout.Columns[1].Width = 75F;
sl@70
   209
            //Send layout to server
sl@70
   210
            iClient.SetLayout(layout);
sl@70
   211
sl@70
   212
            //Set a bitmap for our first field
sl@70
   213
            int x1 = 0;
sl@70
   214
            int y1 = 0;
sl@70
   215
            int x2 = 64;
sl@70
   216
            int y2 = 64;
sl@70
   217
sl@70
   218
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   219
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   220
sl@70
   221
            // Draw line to screen.
sl@70
   222
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   223
            {
sl@70
   224
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   225
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   226
            }
sl@70
   227
sl@71
   228
            //Create a bitmap field from the bitmap we just created
sl@72
   229
            DataField field = new DataField(0, bitmap);
sl@71
   230
            //We want our bitmap field to span across two rows
sl@70
   231
            field.RowSpan = 2;
sl@70
   232
sl@70
   233
            //Set texts
sl@80
   234
            iClient.CreateFields(new DataField[]
sl@70
   235
            {
sl@75
   236
                field,
sl@72
   237
                new DataField(1, textBoxTop.Text, Alignment),
sl@72
   238
                new DataField(2, textBoxBottom.Text, Alignment)
sl@70
   239
            });
sl@70
   240
sl@67
   241
        }
sl@79
   242
sl@79
   243
        private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
sl@79
   244
        {
sl@79
   245
            //Define a 2 by 4 layout
sl@79
   246
            TableLayout layout = new TableLayout(2, 4);
sl@79
   247
            //First column
sl@79
   248
            layout.Columns[0].Width = 87.5F;
sl@79
   249
            //Second column
sl@79
   250
            layout.Columns[1].Width = 12.5F;
sl@79
   251
            //Send layout to server
sl@79
   252
            iClient.SetLayout(layout);
sl@79
   253
sl@79
   254
            //Create a bitmap for our indicators field
sl@79
   255
            int x1 = 0;
sl@79
   256
            int y1 = 0;
sl@79
   257
            int x2 = 32;
sl@79
   258
            int y2 = 16;
sl@79
   259
sl@79
   260
            Bitmap bitmap = new Bitmap(x2, y2);
sl@79
   261
            Pen blackPen = new Pen(Color.Black, 3);
sl@79
   262
sl@79
   263
            // Draw line to screen.
sl@79
   264
            using (var graphics = Graphics.FromImage(bitmap))
sl@79
   265
            {
sl@79
   266
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@79
   267
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@79
   268
            }
sl@79
   269
sl@79
   270
            //Create a bitmap field from the bitmap we just created
sl@79
   271
            DataField indicator1 = new DataField(2, bitmap);
sl@79
   272
            //Create a bitmap field from the bitmap we just created
sl@79
   273
            DataField indicator2 = new DataField(3, bitmap);
sl@79
   274
            //Create a bitmap field from the bitmap we just created
sl@79
   275
            DataField indicator3 = new DataField(4, bitmap);
sl@79
   276
            //Create a bitmap field from the bitmap we just created
sl@79
   277
            DataField indicator4 = new DataField(5, bitmap);
sl@79
   278
sl@79
   279
            //
sl@79
   280
            DataField textFieldTop = new DataField(0, textBoxTop.Text, Alignment);
sl@79
   281
            textFieldTop.RowSpan = 2;
sl@79
   282
sl@79
   283
            DataField textFieldBottom = new DataField(1, textBoxBottom.Text, Alignment);
sl@79
   284
            textFieldBottom.RowSpan = 2;
sl@79
   285
sl@79
   286
sl@79
   287
            //Set texts
sl@80
   288
            iClient.CreateFields(new DataField[]
sl@79
   289
            {
sl@79
   290
                textFieldTop,
sl@79
   291
                indicator1,
sl@79
   292
                indicator2,
sl@79
   293
                textFieldBottom,
sl@79
   294
                indicator3,
sl@79
   295
                indicator4
sl@79
   296
            });
sl@79
   297
sl@79
   298
        }
sl@80
   299
sl@80
   300
        private void buttonUpdateTexts_Click(object sender, EventArgs e)
sl@80
   301
        {
sl@80
   302
sl@80
   303
            bool res = iClient.SetFields(new DataField[]
sl@80
   304
            {
sl@80
   305
                new DataField(0, textBoxTop.Text, Alignment),
sl@80
   306
                new DataField(1, textBoxBottom.Text, Alignment)
sl@80
   307
            });
sl@80
   308
sl@80
   309
            if (!res)
sl@80
   310
            {
sl@80
   311
                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@80
   312
            }
sl@80
   313
sl@80
   314
        }
sl@18
   315
    }
sl@18
   316
}