Client/MainForm.cs
author sl
Tue, 16 Dec 2014 16:35:55 +0100
changeset 79 76564df23849
parent 78 f0dda362f77e
child 80 408ef0501cb7
permissions -rw-r--r--
Adding indicators layout demo.
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@74
   137
            iClient.SetField(iTextFieldTop);
sl@43
   138
        }
sl@43
   139
sl@43
   140
        private void buttonSetText_Click(object sender, EventArgs e)
sl@43
   141
        {
sl@43
   142
            //iClient.SetText(0,"Top");
sl@43
   143
            //iClient.SetText(1, "Bottom");
sl@43
   144
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   145
sl@78
   146
            //Set one column two lines layout
sl@78
   147
            TableLayout layout = new TableLayout(1, 2);
sl@78
   148
            iClient.SetLayout(layout);
sl@78
   149
sl@78
   150
            //Set our fields
sl@74
   151
            iClient.SetFields(new DataField[]
sl@60
   152
            {
sl@72
   153
                new DataField(0, textBoxTop.Text, Alignment),
sl@72
   154
                new DataField(1, textBoxBottom.Text, Alignment)
sl@43
   155
            });
sl@43
   156
        }
sl@62
   157
sl@62
   158
        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62
   159
        {
sl@65
   160
            //Define a 2 by 2 layout
sl@62
   161
            TableLayout layout = new TableLayout(2,2);
sl@65
   162
            //Second column only takes up 25%
sl@63
   163
            layout.Columns[1].Width = 25F;
sl@65
   164
            //Send layout to server
sl@62
   165
            iClient.SetLayout(layout);
sl@62
   166
        }
sl@67
   167
sl@68
   168
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   169
        {
sl@68
   170
            int x1 = 0;
sl@68
   171
            int y1 = 0;
sl@68
   172
            int x2 = 256;
sl@68
   173
            int y2 = 32;
sl@68
   174
sl@68
   175
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   176
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   177
sl@67
   178
            // Draw line to screen.
sl@67
   179
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   180
            {
sl@67
   181
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   182
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   183
            }
sl@67
   184
sl@72
   185
            DataField field = new DataField(0, bitmap);
sl@70
   186
            field.ColumnSpan = 2;
sl@74
   187
            iClient.SetField(field);
sl@70
   188
        }
sl@70
   189
sl@71
   190
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   191
        {
sl@71
   192
            SetLayoutWithBitmap();
sl@71
   193
        }
sl@71
   194
sl@71
   195
        /// <summary>
sl@71
   196
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   197
        /// </summary>
sl@71
   198
        private void SetLayoutWithBitmap()
sl@70
   199
        {
sl@70
   200
            //Define a 2 by 2 layout
sl@70
   201
            TableLayout layout = new TableLayout(2, 2);
sl@71
   202
            //First column only takes 25%
sl@70
   203
            layout.Columns[0].Width = 25F;
sl@73
   204
            //Second column takes up 75%
sl@70
   205
            layout.Columns[1].Width = 75F;
sl@70
   206
            //Send layout to server
sl@70
   207
            iClient.SetLayout(layout);
sl@70
   208
sl@70
   209
            //Set a bitmap for our first field
sl@70
   210
            int x1 = 0;
sl@70
   211
            int y1 = 0;
sl@70
   212
            int x2 = 64;
sl@70
   213
            int y2 = 64;
sl@70
   214
sl@70
   215
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   216
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   217
sl@70
   218
            // Draw line to screen.
sl@70
   219
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   220
            {
sl@70
   221
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   222
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   223
            }
sl@70
   224
sl@71
   225
            //Create a bitmap field from the bitmap we just created
sl@72
   226
            DataField field = new DataField(0, bitmap);
sl@71
   227
            //We want our bitmap field to span across two rows
sl@70
   228
            field.RowSpan = 2;
sl@70
   229
sl@70
   230
            //Set texts
sl@74
   231
            iClient.SetFields(new DataField[]
sl@70
   232
            {
sl@75
   233
                field,
sl@72
   234
                new DataField(1, textBoxTop.Text, Alignment),
sl@72
   235
                new DataField(2, textBoxBottom.Text, Alignment)
sl@70
   236
            });
sl@70
   237
sl@67
   238
        }
sl@79
   239
sl@79
   240
        private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
sl@79
   241
        {
sl@79
   242
            //Define a 2 by 4 layout
sl@79
   243
            TableLayout layout = new TableLayout(2, 4);
sl@79
   244
            //First column
sl@79
   245
            layout.Columns[0].Width = 87.5F;
sl@79
   246
            //Second column
sl@79
   247
            layout.Columns[1].Width = 12.5F;
sl@79
   248
            //Send layout to server
sl@79
   249
            iClient.SetLayout(layout);
sl@79
   250
sl@79
   251
            //Create a bitmap for our indicators field
sl@79
   252
            int x1 = 0;
sl@79
   253
            int y1 = 0;
sl@79
   254
            int x2 = 32;
sl@79
   255
            int y2 = 16;
sl@79
   256
sl@79
   257
            Bitmap bitmap = new Bitmap(x2, y2);
sl@79
   258
            Pen blackPen = new Pen(Color.Black, 3);
sl@79
   259
sl@79
   260
            // Draw line to screen.
sl@79
   261
            using (var graphics = Graphics.FromImage(bitmap))
sl@79
   262
            {
sl@79
   263
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@79
   264
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@79
   265
            }
sl@79
   266
sl@79
   267
            //Create a bitmap field from the bitmap we just created
sl@79
   268
            DataField indicator1 = new DataField(2, bitmap);
sl@79
   269
            //Create a bitmap field from the bitmap we just created
sl@79
   270
            DataField indicator2 = new DataField(3, bitmap);
sl@79
   271
            //Create a bitmap field from the bitmap we just created
sl@79
   272
            DataField indicator3 = new DataField(4, bitmap);
sl@79
   273
            //Create a bitmap field from the bitmap we just created
sl@79
   274
            DataField indicator4 = new DataField(5, bitmap);
sl@79
   275
sl@79
   276
            //
sl@79
   277
            DataField textFieldTop = new DataField(0, textBoxTop.Text, Alignment);
sl@79
   278
            textFieldTop.RowSpan = 2;
sl@79
   279
sl@79
   280
            DataField textFieldBottom = new DataField(1, textBoxBottom.Text, Alignment);
sl@79
   281
            textFieldBottom.RowSpan = 2;
sl@79
   282
sl@79
   283
sl@79
   284
            //Set texts
sl@79
   285
            iClient.SetFields(new DataField[]
sl@79
   286
            {
sl@79
   287
                textFieldTop,
sl@79
   288
                indicator1,
sl@79
   289
                indicator2,
sl@79
   290
                textFieldBottom,
sl@79
   291
                indicator3,
sl@79
   292
                indicator4
sl@79
   293
            });
sl@79
   294
sl@79
   295
        }
sl@18
   296
    }
sl@18
   297
}