Client/MainForm.cs
author sl
Sat, 30 Aug 2014 18:45:06 +0200
changeset 46 0c0ee86760ea
parent 33 1363bda20171
child 55 b5ed2e29be23
permissions -rw-r--r--
Adding support for display type selection.
     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 SharpDisplayInterface;
    14 
    15 
    16 namespace SharpDisplayClient
    17 {
    18     public partial class MainForm : Form
    19     {
    20         Client iClient;
    21         Callback iCallback;
    22         ContentAlignment Alignment;
    23         TextField iTextFieldTop;
    24 
    25         public MainForm()
    26         {
    27             InitializeComponent();
    28             Alignment = ContentAlignment.MiddleLeft;
    29             iTextFieldTop = new TextField(0);
    30         }
    31 
    32 
    33         private void MainForm_Load(object sender, EventArgs e)
    34         {
    35             iCallback = new Callback(this);
    36             //Instance context is then managed by our client class
    37             InstanceContext instanceContext = new InstanceContext(iCallback);
    38             iClient = new Client(instanceContext);
    39 
    40             //Connect using unique name
    41             //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    42             string name = "Client-" + (iClient.ClientCount() - 1);
    43             iClient.SetName(name);
    44             //Text = Text + ": " + name;
    45             Text = "[[" + name + "]]  " + iClient.SessionId;
    46 
    47             //
    48             textBoxTop.Text = iClient.Name;
    49             textBoxBottom.Text = iClient.SessionId;
    50 
    51         }
    52 
    53 
    54        
    55         public delegate void CloseConnectionDelegate();
    56         public delegate void CloseDelegate();
    57 
    58         /// <summary>
    59         /// 
    60         /// </summary>
    61         public void CloseConnectionThreadSafe()
    62         {
    63             if (this.InvokeRequired)
    64             {
    65                 //Not in the proper thread, invoke ourselves
    66                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
    67                 this.Invoke(d, new object[] { });
    68             }
    69             else
    70             {
    71                 //We are in the proper thread
    72                 if (IsClientReady())
    73                 {
    74                     Trace.TraceInformation("Closing client: " + iClient.SessionId);
    75                     iClient.Close();
    76                     Trace.TraceInformation("Closed client: " + iClient.SessionId);
    77                 }
    78 
    79                 iClient = null;
    80                 iCallback = null;
    81             }
    82         }
    83 
    84         /// <summary>
    85         /// 
    86         /// </summary>
    87         public void CloseThreadSafe()
    88         {
    89             if (this.InvokeRequired)
    90             {
    91                 //Not in the proper thread, invoke ourselves
    92                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
    93                 this.Invoke(d, new object[] { });
    94             }
    95             else
    96             {
    97                 //We are in the proper thread
    98                 Close();
    99             }
   100         }
   101 
   102 
   103         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   104         {
   105             CloseConnectionThreadSafe();
   106         }
   107 
   108         public bool IsClientReady()
   109         {
   110             return (iClient != null && iClient.State == CommunicationState.Opened);
   111         }
   112 
   113         private void buttonAlignLeft_Click(object sender, EventArgs e)
   114         {
   115             Alignment = ContentAlignment.MiddleLeft;
   116             textBoxTop.TextAlign = HorizontalAlignment.Left;
   117             textBoxBottom.TextAlign = HorizontalAlignment.Left;
   118         }
   119 
   120         private void buttonAlignCenter_Click(object sender, EventArgs e)
   121         {
   122             Alignment = ContentAlignment.MiddleCenter;
   123             textBoxTop.TextAlign = HorizontalAlignment.Center;
   124             textBoxBottom.TextAlign = HorizontalAlignment.Center;
   125         }
   126 
   127         private void buttonAlignRight_Click(object sender, EventArgs e)
   128         {
   129             Alignment = ContentAlignment.MiddleRight;
   130             textBoxTop.TextAlign = HorizontalAlignment.Right;
   131             textBoxBottom.TextAlign = HorizontalAlignment.Right;
   132         }
   133 
   134         private void buttonSetTopText_Click(object sender, EventArgs e)
   135         {
   136             //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   137             iTextFieldTop.Text = textBoxTop.Text;
   138             iClient.SetText(iTextFieldTop);
   139         }
   140 
   141         private void buttonSetText_Click(object sender, EventArgs e)
   142         {
   143             //iClient.SetText(0,"Top");
   144             //iClient.SetText(1, "Bottom");
   145             //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   146 
   147             iClient.SetTexts(new TextField[]
   148             { 
   149                 new TextField(0, textBoxTop.Text, Alignment),
   150                 new TextField(1, textBoxBottom.Text, Alignment)
   151             });
   152         }
   153     }
   154 }