MainForm.cs
author sl
Sat, 12 Jul 2014 22:18:34 +0200
changeset 14 d1fa9de444b0
parent 13 745d9ee1a2c0
child 16 985ca4b6e099
permissions -rw-r--r--
Fixing issues with render when app is minimized.
     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 
    14 
    15 namespace SharpDisplayManager
    16 {
    17     public partial class MainForm : Form
    18     {
    19         DateTime LastTickTime;
    20         Display iDisplay;
    21         System.Drawing.Bitmap iBmp;
    22         bool iCreateBitmap; //Workaround render to bitmap issues when minimized
    23 
    24         public MainForm()
    25         {
    26             LastTickTime = DateTime.Now;
    27             iDisplay = new Display();
    28 
    29             InitializeComponent();
    30             UpdateStatus();
    31 
    32             //Load settings
    33             marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
    34             marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
    35             checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
    36             checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
    37             //
    38             tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
    39             //We have a bug when drawing minimized and reusing our bitmap
    40             iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
    41             iCreateBitmap = false;
    42         }
    43 
    44         private void MainForm_Load(object sender, EventArgs e)
    45         {
    46             if (Properties.Settings.Default.DisplayConnectOnStartup)
    47             {
    48                 iDisplay.Open();
    49                 UpdateStatus();
    50             }
    51         }
    52 
    53 
    54         private void buttonFont_Click(object sender, EventArgs e)
    55         {
    56             //fontDialog.ShowColor = true;
    57             //fontDialog.ShowApply = true;
    58             fontDialog.ShowEffects = true;
    59             fontDialog.Font = marqueeLabelTop.Font;
    60             //fontDialog.ShowHelp = true;
    61 
    62             //fontDlg.MaxSize = 40;
    63             //fontDlg.MinSize = 22;
    64 
    65             //fontDialog.Parent = this;
    66             //fontDialog.StartPosition = FormStartPosition.CenterParent;
    67 
    68             //DlgBox.ShowDialog(fontDialog);
    69 
    70             //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    71             if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    72             {
    73 
    74                 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    75 
    76                 //MessageBox.Show("Ok");
    77                 marqueeLabelTop.Font = fontDialog.Font;
    78                 marqueeLabelBottom.Font = fontDialog.Font;
    79                 Properties.Settings.Default.DisplayFont = fontDialog.Font;
    80                 Properties.Settings.Default.Save();
    81                 //label1.Font = fontDlg.Font;
    82                 //textBox1.BackColor = fontDlg.Color;
    83                 //label1.ForeColor = fontDlg.Color;
    84             }
    85         }
    86 
    87         private void buttonCapture_Click(object sender, EventArgs e)
    88         {
    89             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    90             tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    91             //Bitmap bmpToSave = new Bitmap(bmp);
    92             bmp.Save("D:\\capture.png");
    93 
    94             /*
    95             string outputFileName = "d:\\capture.png";
    96             using (MemoryStream memory = new MemoryStream())
    97             {
    98                 using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    99                 {
   100                     bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
   101                     byte[] bytes = memory.ToArray();
   102                     fs.Write(bytes, 0, bytes.Length);
   103                 }
   104             }
   105              */
   106 
   107         }
   108 
   109         private void CheckForRequestResults()
   110         {
   111             if (iDisplay.IsRequestPending())
   112             {
   113                 switch (iDisplay.AttemptRequestCompletion())
   114                 {
   115                     case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
   116                         if (iDisplay.PowerSupplyStatus())
   117                         {
   118                             toolStripStatusLabelPower.Text = "ON";
   119                         }
   120                         else
   121                         {
   122                             toolStripStatusLabelPower.Text = "OFF";
   123                         }
   124                         //Issue next request then
   125                         iDisplay.RequestDeviceId();
   126                         break;
   127 
   128                     case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
   129                         toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
   130                         //Issue next request then
   131                         iDisplay.RequestFirmwareRevision();
   132                         break;
   133 
   134                     case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
   135                         toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
   136                         //No more request to issue
   137                         break;
   138                 }
   139             }
   140         }
   141 
   142         private void timer_Tick(object sender, EventArgs e)
   143         {
   144             //Update our animations
   145             DateTime NewTickTime = DateTime.Now;
   146 
   147             marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
   148             marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
   149 
   150             //Update our display
   151             if (iDisplay.IsOpen())
   152             {
   153                 CheckForRequestResults();
   154 
   155                 //Draw to bitmap                
   156                 if (iCreateBitmap)
   157                 {
   158                     iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   159                 }
   160                 tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
   161                 //iBmp.Save("D:\\capture.png");
   162                 
   163                 //iBmp.
   164 
   165                 //Send it to our display
   166                 for (int i = 0; i < iBmp.Width; i++)
   167                 {
   168                     for (int j = 0; j < iBmp.Height; j++)
   169                     {
   170                         unchecked
   171                         {
   172                             uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
   173                             //For some reason when the app is minimized in the task bar only the alpha of our color is set.
   174                             //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
   175                             iDisplay.SetPixel(i, j, Convert.ToInt32(!(color != 0xFF000000)));
   176                         }
   177                     }
   178                 }
   179 
   180                 iDisplay.SwapBuffers();
   181 
   182             }
   183 
   184             //Compute instant FPS
   185             toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   186 
   187             LastTickTime = NewTickTime;
   188 
   189         }
   190 
   191         private void buttonOpen_Click(object sender, EventArgs e)
   192         {
   193             if (iDisplay.Open())
   194             {
   195                 UpdateStatus();
   196                 iDisplay.RequestPowerSupplyStatus();
   197             }
   198             else
   199             {
   200                 UpdateStatus();
   201                 toolStripStatusLabelConnect.Text = "Connection error";
   202             }
   203 
   204         }
   205 
   206         private void buttonClose_Click(object sender, EventArgs e)
   207         {
   208             iDisplay.Close();
   209             UpdateStatus();
   210         }
   211 
   212         private void buttonClear_Click(object sender, EventArgs e)
   213         {
   214             iDisplay.Clear();
   215             iDisplay.SwapBuffers();
   216         }
   217 
   218         private void buttonFill_Click(object sender, EventArgs e)
   219         {
   220             iDisplay.Fill();
   221             iDisplay.SwapBuffers();
   222         }
   223 
   224         private void trackBarBrightness_Scroll(object sender, EventArgs e)
   225         {
   226             Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
   227             Properties.Settings.Default.Save();
   228             iDisplay.SetBrightness(trackBarBrightness.Value);
   229 
   230         }
   231 
   232         private void UpdateStatus()
   233         {
   234             if (iDisplay.IsOpen())
   235             {
   236                 buttonFill.Enabled = true;
   237                 buttonClear.Enabled = true;
   238                 buttonOpen.Enabled = false;
   239                 buttonClose.Enabled = true;
   240                 trackBarBrightness.Enabled = true;
   241                 trackBarBrightness.Minimum = iDisplay.MinBrightness();
   242                 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
   243                 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
   244                 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
   245                 trackBarBrightness.SmallChange = 1;
   246                 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
   247 
   248                 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
   249                 //+ " - " + iDisplay.SerialNumber();
   250             }
   251             else
   252             {
   253                 buttonFill.Enabled = false;
   254                 buttonClear.Enabled = false;
   255                 buttonOpen.Enabled = true;
   256                 buttonClose.Enabled = false;
   257                 trackBarBrightness.Enabled = false;
   258                 toolStripStatusLabelConnect.Text = "Disconnected";
   259             }
   260         }
   261 
   262 
   263 
   264         private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
   265         {
   266             tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
   267             Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
   268             Properties.Settings.Default.Save();
   269         }
   270 
   271         private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
   272         {
   273             Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
   274             Properties.Settings.Default.Save();
   275         }
   276 
   277         private void MainForm_Resize(object sender, EventArgs e)
   278         {
   279             if (WindowState == FormWindowState.Minimized)
   280             {
   281                 // Do some stuff
   282                 //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   283                 iCreateBitmap = true;
   284             }
   285         }
   286 
   287     }
   288 }