Server/MainForm.cs
author sl
Tue, 12 Aug 2014 21:27:35 +0200
changeset 19 1a85ec255882
parent 17 19c1aaf900dc
child 20 e3d394dd0388
permissions -rw-r--r--
Adding server method to set all texts at once.
     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.IO;
    11 using CodeProject.Dialog;
    12 using System.Drawing.Imaging;
    13 using System.ServiceModel;
    14 
    15 
    16 namespace SharpDisplayManager
    17 {
    18     public partial class MainForm : Form
    19     {
    20         DateTime LastTickTime;
    21         Display iDisplay;
    22         System.Drawing.Bitmap iBmp;
    23         bool iCreateBitmap; //Workaround render to bitmap issues when minimized
    24         ServiceHost iServiceHost;
    25 
    26         public MainForm()
    27         {
    28             LastTickTime = DateTime.Now;
    29             iDisplay = new Display();
    30 
    31             InitializeComponent();
    32             UpdateStatus();
    33 
    34             //Load settings
    35             marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
    36             marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
    37             checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
    38             checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
    39             checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen;
    40             //
    41             tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
    42             //We have a bug when drawing minimized and reusing our bitmap
    43             iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
    44             iCreateBitmap = false;
    45         }
    46 
    47         private void MainForm_Load(object sender, EventArgs e)
    48         {
    49             StartServer();
    50 
    51             if (Properties.Settings.Default.DisplayConnectOnStartup)
    52             {
    53                 iDisplay.Open();
    54                 UpdateStatus();
    55             }
    56         }
    57 
    58 
    59         private void buttonFont_Click(object sender, EventArgs e)
    60         {
    61             //fontDialog.ShowColor = true;
    62             //fontDialog.ShowApply = true;
    63             fontDialog.ShowEffects = true;
    64             fontDialog.Font = marqueeLabelTop.Font;
    65             //fontDialog.ShowHelp = true;
    66 
    67             //fontDlg.MaxSize = 40;
    68             //fontDlg.MinSize = 22;
    69 
    70             //fontDialog.Parent = this;
    71             //fontDialog.StartPosition = FormStartPosition.CenterParent;
    72 
    73             //DlgBox.ShowDialog(fontDialog);
    74 
    75             //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    76             if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    77             {
    78 
    79                 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    80 
    81                 //MessageBox.Show("Ok");
    82                 marqueeLabelTop.Font = fontDialog.Font;
    83                 marqueeLabelBottom.Font = fontDialog.Font;
    84                 Properties.Settings.Default.DisplayFont = fontDialog.Font;
    85                 Properties.Settings.Default.Save();
    86                 //label1.Font = fontDlg.Font;
    87                 //textBox1.BackColor = fontDlg.Color;
    88                 //label1.ForeColor = fontDlg.Color;
    89             }
    90         }
    91 
    92         private void buttonCapture_Click(object sender, EventArgs e)
    93         {
    94             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    95             tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    96             //Bitmap bmpToSave = new Bitmap(bmp);
    97             bmp.Save("D:\\capture.png");
    98 
    99             marqueeLabelTop.Text = "Sweet";
   100 
   101             /*
   102             string outputFileName = "d:\\capture.png";
   103             using (MemoryStream memory = new MemoryStream())
   104             {
   105                 using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
   106                 {
   107                     bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
   108                     byte[] bytes = memory.ToArray();
   109                     fs.Write(bytes, 0, bytes.Length);
   110                 }
   111             }
   112              */
   113 
   114         }
   115 
   116         private void CheckForRequestResults()
   117         {
   118             if (iDisplay.IsRequestPending())
   119             {
   120                 switch (iDisplay.AttemptRequestCompletion())
   121                 {
   122                     case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
   123                         if (iDisplay.PowerSupplyStatus())
   124                         {
   125                             toolStripStatusLabelPower.Text = "ON";
   126                         }
   127                         else
   128                         {
   129                             toolStripStatusLabelPower.Text = "OFF";
   130                         }
   131                         //Issue next request then
   132                         iDisplay.RequestDeviceId();
   133                         break;
   134 
   135                     case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
   136                         toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
   137                         //Issue next request then
   138                         iDisplay.RequestFirmwareRevision();
   139                         break;
   140 
   141                     case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
   142                         toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
   143                         //No more request to issue
   144                         break;
   145                 }
   146             }
   147         }
   148 
   149 
   150         public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
   151 
   152 
   153         public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
   154         {
   155             return aBmp.Width - aX - 1;
   156         }
   157 
   158         public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
   159         {
   160             return iBmp.Height - aY - 1;
   161         }
   162 
   163         public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
   164         {
   165             return aX;
   166         }
   167 
   168         public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
   169         {
   170             return aY;
   171         }
   172 
   173 
   174         //This is our timer tick responsible to perform our render
   175         private void timer_Tick(object sender, EventArgs e)
   176         {
   177             //Update our animations
   178             DateTime NewTickTime = DateTime.Now;
   179 
   180             marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
   181             marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
   182 
   183             //Update our display
   184             if (iDisplay.IsOpen())
   185             {
   186                 CheckForRequestResults();
   187 
   188                 //Draw to bitmap                
   189                 if (iCreateBitmap)
   190                 {
   191                     iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   192                 }
   193                 tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
   194                 //iBmp.Save("D:\\capture.png");
   195 
   196                 //Select proper coordinate translation functions
   197                 //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
   198                 CoordinateTranslationDelegate screenX;
   199                 CoordinateTranslationDelegate screenY;
   200 
   201                 if (Properties.Settings.Default.DisplayReverseScreen)
   202                 {
   203                     screenX = ScreenReversedX;
   204                     screenY = ScreenReversedY;
   205                 }
   206                 else
   207                 {
   208                     screenX = ScreenX;
   209                     screenY = ScreenY;
   210                 }
   211                 
   212                 //Send it to our display
   213                 for (int i = 0; i < iBmp.Width; i++)
   214                 {
   215                     for (int j = 0; j < iBmp.Height; j++)
   216                     {
   217                         unchecked
   218                         {
   219                             uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
   220                             //For some reason when the app is minimized in the task bar only the alpha of our color is set.
   221                             //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
   222                             iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000)));
   223                         }
   224                     }
   225                 }
   226 
   227                 iDisplay.SwapBuffers();
   228 
   229             }
   230 
   231             //Compute instant FPS
   232             toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   233 
   234             LastTickTime = NewTickTime;
   235 
   236         }
   237 
   238         private void buttonOpen_Click(object sender, EventArgs e)
   239         {
   240             if (iDisplay.Open())
   241             {
   242                 UpdateStatus();
   243                 iDisplay.RequestPowerSupplyStatus();
   244             }
   245             else
   246             {
   247                 UpdateStatus();
   248                 toolStripStatusLabelConnect.Text = "Connection error";
   249             }
   250 
   251         }
   252 
   253         private void buttonClose_Click(object sender, EventArgs e)
   254         {
   255             iDisplay.Close();
   256             UpdateStatus();
   257         }
   258 
   259         private void buttonClear_Click(object sender, EventArgs e)
   260         {
   261             iDisplay.Clear();
   262             iDisplay.SwapBuffers();
   263         }
   264 
   265         private void buttonFill_Click(object sender, EventArgs e)
   266         {
   267             iDisplay.Fill();
   268             iDisplay.SwapBuffers();
   269         }
   270 
   271         private void trackBarBrightness_Scroll(object sender, EventArgs e)
   272         {
   273             Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
   274             Properties.Settings.Default.Save();
   275             iDisplay.SetBrightness(trackBarBrightness.Value);
   276 
   277         }
   278 
   279         private void UpdateStatus()
   280         {
   281             if (iDisplay.IsOpen())
   282             {
   283                 buttonFill.Enabled = true;
   284                 buttonClear.Enabled = true;
   285                 buttonOpen.Enabled = false;
   286                 buttonClose.Enabled = true;
   287                 trackBarBrightness.Enabled = true;
   288                 trackBarBrightness.Minimum = iDisplay.MinBrightness();
   289                 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
   290                 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
   291                 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
   292                 trackBarBrightness.SmallChange = 1;
   293                 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
   294 
   295                 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
   296                 //+ " - " + iDisplay.SerialNumber();
   297             }
   298             else
   299             {
   300                 buttonFill.Enabled = false;
   301                 buttonClear.Enabled = false;
   302                 buttonOpen.Enabled = true;
   303                 buttonClose.Enabled = false;
   304                 trackBarBrightness.Enabled = false;
   305                 toolStripStatusLabelConnect.Text = "Disconnected";
   306             }
   307         }
   308 
   309 
   310 
   311         private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
   312         {
   313             //Save our show borders setting
   314             tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
   315             Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
   316             Properties.Settings.Default.Save();
   317         }
   318 
   319         private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
   320         {
   321             //Save our connect on startup setting
   322             Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
   323             Properties.Settings.Default.Save();
   324         }
   325 
   326         private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
   327         {
   328             //Save our reverse screen setting
   329             Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked;
   330             Properties.Settings.Default.Save();
   331         }
   332 
   333         private void MainForm_Resize(object sender, EventArgs e)
   334         {
   335             if (WindowState == FormWindowState.Minimized)
   336             {
   337                 // Do some stuff
   338                 //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   339                 iCreateBitmap = true;
   340             }
   341         }
   342 
   343         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   344         {
   345             StopServer();
   346         }
   347 
   348         public void StartServer()
   349         {
   350             iServiceHost = new ServiceHost
   351                 (
   352                     typeof(DisplayServer),
   353                     new Uri[] { new Uri("net.pipe://localhost") }
   354                 );
   355 
   356             iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetNamedPipeBinding(), "DisplayService");
   357             iServiceHost.Open();
   358         }
   359 
   360         public void StopServer()
   361         {
   362             //Tell connected client first? Is that possible?
   363             iServiceHost.Close();
   364         }
   365 
   366 
   367     }
   368 }