Client/MainForm.cs
author sl
Tue, 16 Dec 2014 11:08:18 +0100
changeset 77 042c1fa136b9
parent 75 2549a8055bd1
parent 74 60d584bad780
child 78 f0dda362f77e
permissions -rw-r--r--
Merge field unification changes and client recovery feature.
     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             iClient.SetField(iTextFieldTop);
   138         }
   139 
   140         private void buttonSetText_Click(object sender, EventArgs e)
   141         {
   142             //iClient.SetText(0,"Top");
   143             //iClient.SetText(1, "Bottom");
   144             //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   145 
   146             iClient.SetFields(new DataField[]
   147             {
   148                 new DataField(0, textBoxTop.Text, Alignment),
   149                 new DataField(1, textBoxBottom.Text, Alignment)
   150             });
   151         }
   152 
   153         private void buttonLayoutUpdate_Click(object sender, EventArgs e)
   154         {
   155             //Define a 2 by 2 layout
   156             TableLayout layout = new TableLayout(2,2);
   157             //Second column only takes up 25%
   158             layout.Columns[1].Width = 25F;
   159             //Send layout to server
   160             iClient.SetLayout(layout);
   161         }
   162 
   163         private void buttonSetBitmap_Click(object sender, EventArgs e)
   164         {
   165             int x1 = 0;
   166             int y1 = 0;
   167             int x2 = 256;
   168             int y2 = 32;
   169 
   170             Bitmap bitmap = new Bitmap(x2,y2);
   171             Pen blackPen = new Pen(Color.Black, 3);
   172 
   173             // Draw line to screen.
   174             using (var graphics = Graphics.FromImage(bitmap))
   175             {
   176                 graphics.DrawLine(blackPen, x1, y1, x2, y2);
   177                 graphics.DrawLine(blackPen, x1, y2, x2, y1);
   178             }
   179 
   180             DataField field = new DataField(0, bitmap);
   181             field.ColumnSpan = 2;
   182             iClient.SetField(field);
   183         }
   184 
   185         private void buttonBitmapLayout_Click(object sender, EventArgs e)
   186         {
   187             SetLayoutWithBitmap();
   188         }
   189 
   190         /// <summary>
   191         /// Define a layout with a bitmap field on the left and two lines of text on the right.
   192         /// </summary>
   193         private void SetLayoutWithBitmap()
   194         {
   195             //Define a 2 by 2 layout
   196             TableLayout layout = new TableLayout(2, 2);
   197             //First column only takes 25%
   198             layout.Columns[0].Width = 25F;
   199             //Second column takes up 75%
   200             layout.Columns[1].Width = 75F;
   201             //Send layout to server
   202             iClient.SetLayout(layout);
   203 
   204             //Set a bitmap for our first field
   205             int x1 = 0;
   206             int y1 = 0;
   207             int x2 = 64;
   208             int y2 = 64;
   209 
   210             Bitmap bitmap = new Bitmap(x2, y2);
   211             Pen blackPen = new Pen(Color.Black, 3);
   212 
   213             // Draw line to screen.
   214             using (var graphics = Graphics.FromImage(bitmap))
   215             {
   216                 graphics.DrawLine(blackPen, x1, y1, x2, y2);
   217                 graphics.DrawLine(blackPen, x1, y2, x2, y1);
   218             }
   219 
   220             //Create a bitmap field from the bitmap we just created
   221             DataField field = new DataField(0, bitmap);
   222             //We want our bitmap field to span across two rows
   223             field.RowSpan = 2;
   224             iClient.SetField(field);
   225 
   226             //Set texts
   227             iClient.SetFields(new DataField[]
   228             {
   229                 field,
   230                 new DataField(1, textBoxTop.Text, Alignment),
   231                 new DataField(2, textBoxBottom.Text, Alignment)
   232             });
   233 
   234         }
   235     }
   236 }