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