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