Client/MainForm.cs
author sl
Sat, 25 Oct 2014 13:35:11 +0200
changeset 72 fd0bb39a7818
parent 71 f444344fc205
child 73 926cdf23b485
child 75 2549a8055bd1
permissions -rw-r--r--
Now having a single class for both text and bitmap field.
Thus we should soon be able to use common functions to pass in fields.
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@26
    20
        Client iClient;
sl@26
    21
        Callback iCallback;
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@31
    35
            iCallback = new Callback(this);
sl@57
    36
            iClient = new Client(iCallback);
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@31
    72
                    Trace.TraceInformation("Closing client: " + iClient.SessionId);
sl@31
    73
                    iClient.Close();
sl@31
    74
                    Trace.TraceInformation("Closed client: " + iClient.SessionId);
sl@31
    75
                }
sl@29
    76
sl@31
    77
                iClient = null;
sl@31
    78
                iCallback = 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@29
   108
            return (iClient != null && iClient.State == CommunicationState.Opened);
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@43
   137
            iClient.SetText(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@72
   146
            iClient.SetTexts(new DataField[]
sl@60
   147
            {
sl@72
   148
                new DataField(0, textBoxTop.Text, Alignment),
sl@72
   149
                new DataField(1, textBoxBottom.Text, Alignment)
sl@43
   150
            });
sl@43
   151
        }
sl@62
   152
sl@62
   153
        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62
   154
        {
sl@65
   155
            //Define a 2 by 2 layout
sl@62
   156
            TableLayout layout = new TableLayout(2,2);
sl@65
   157
            //Second column only takes up 25%
sl@63
   158
            layout.Columns[1].Width = 25F;
sl@65
   159
            //Send layout to server
sl@62
   160
            iClient.SetLayout(layout);
sl@62
   161
        }
sl@67
   162
sl@68
   163
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   164
        {
sl@68
   165
            int x1 = 0;
sl@68
   166
            int y1 = 0;
sl@68
   167
            int x2 = 256;
sl@68
   168
            int y2 = 32;
sl@68
   169
sl@68
   170
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   171
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   172
sl@67
   173
            // Draw line to screen.
sl@67
   174
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   175
            {
sl@67
   176
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   177
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   178
            }
sl@67
   179
sl@72
   180
            DataField field = new DataField(0, bitmap);
sl@70
   181
            field.ColumnSpan = 2;
sl@70
   182
            iClient.SetBitmap(field);
sl@70
   183
        }
sl@70
   184
sl@71
   185
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   186
        {
sl@71
   187
            SetLayoutWithBitmap();
sl@71
   188
        }
sl@71
   189
sl@71
   190
        /// <summary>
sl@71
   191
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   192
        /// </summary>
sl@71
   193
        private void SetLayoutWithBitmap()
sl@70
   194
        {
sl@70
   195
            //Define a 2 by 2 layout
sl@70
   196
            TableLayout layout = new TableLayout(2, 2);
sl@71
   197
            //First column only takes 25%
sl@70
   198
            layout.Columns[0].Width = 25F;
sl@71
   199
            //Second column takes up 75% 
sl@70
   200
            layout.Columns[1].Width = 75F;
sl@70
   201
            //Send layout to server
sl@70
   202
            iClient.SetLayout(layout);
sl@70
   203
sl@70
   204
            //Set a bitmap for our first field
sl@70
   205
            int x1 = 0;
sl@70
   206
            int y1 = 0;
sl@70
   207
            int x2 = 64;
sl@70
   208
            int y2 = 64;
sl@70
   209
sl@70
   210
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   211
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   212
sl@70
   213
            // Draw line to screen.
sl@70
   214
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   215
            {
sl@70
   216
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   217
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   218
            }
sl@70
   219
sl@71
   220
            //Create a bitmap field from the bitmap we just created
sl@72
   221
            DataField field = new DataField(0, bitmap);
sl@71
   222
            //We want our bitmap field to span across two rows
sl@70
   223
            field.RowSpan = 2;
sl@71
   224
            //Send it to our server
sl@70
   225
            iClient.SetBitmap(field);
sl@70
   226
sl@70
   227
            //Set texts
sl@72
   228
            iClient.SetTexts(new DataField[]
sl@70
   229
            {
sl@72
   230
                new DataField(1, textBoxTop.Text, Alignment),
sl@72
   231
                new DataField(2, textBoxBottom.Text, Alignment)
sl@70
   232
            });
sl@70
   233
sl@67
   234
        }
sl@18
   235
    }
sl@18
   236
}