MainForm.cs
author sl
Tue, 08 Jul 2014 21:49:30 +0200
changeset 10 54671755add3
parent 9 7c363a7a975e
child 11 de55741d90f0
permissions -rw-r--r--
Adding support for vendor, product and serial number.
Strating to add support for requests.
     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 CodeProject.Dialog;
    11 
    12 namespace SharpDisplayManager
    13 {
    14     public partial class MainForm : Form
    15     {
    16         DateTime LastTickTime;
    17         Display iDisplay;
    18 
    19         public MainForm()
    20         {
    21             LastTickTime = DateTime.Now;
    22             iDisplay = new Display();
    23 
    24             InitializeComponent();
    25             UpdateStatus();
    26             //Load settings
    27             marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
    28             marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
    29             checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
    30         }
    31 
    32         private void buttonFont_Click(object sender, EventArgs e)
    33         {
    34             //fontDialog.ShowColor = true;
    35             //fontDialog.ShowApply = true;
    36             fontDialog.ShowEffects = true;
    37             //fontDialog.ShowHelp = true;
    38 
    39             //fontDlg.MaxSize = 40;
    40             //fontDlg.MinSize = 22;
    41 
    42             //fontDialog.Parent = this;
    43             //fontDialog.StartPosition = FormStartPosition.CenterParent;
    44 
    45             //DlgBox.ShowDialog(fontDialog);
    46 
    47             //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    48             if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    49             {
    50 
    51                 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    52 
    53                 //MessageBox.Show("Ok");
    54                 marqueeLabelTop.Font = fontDialog.Font;
    55                 marqueeLabelBottom.Font = fontDialog.Font;
    56                 Properties.Settings.Default.DisplayFont = fontDialog.Font;
    57                 Properties.Settings.Default.Save();
    58                 //label1.Font = fontDlg.Font;
    59                 //textBox1.BackColor = fontDlg.Color;
    60                 //label1.ForeColor = fontDlg.Color;
    61             }
    62         }
    63 
    64         private void buttonCapture_Click(object sender, EventArgs e)
    65         {
    66             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    67             tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    68             bmp.Save("c:\\capture.png");
    69         }
    70 
    71         private void timer_Tick(object sender, EventArgs e)
    72         {
    73             //Update our animations
    74             DateTime NewTickTime = DateTime.Now;
    75 
    76             marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
    77             marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
    78 
    79             //Update our display
    80             if (iDisplay.IsOpen())
    81             {
    82                 //Draw to bitmap
    83                 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    84                 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    85                 //Send it to our display
    86                 for (int i = 0; i < bmp.Width; i++)
    87                 {
    88                     for (int j = 0; j < bmp.Height; j++)
    89                     {
    90                         unchecked
    91                         {
    92                         uint color=(uint)bmp.GetPixel(i, j).ToArgb();
    93                         iDisplay.SetPixel(i, j, Convert.ToInt32((checkBoxShowBorders.Checked?color!=0xFFFFFFFF:color == 0xFF000000)));
    94                         }
    95                     }
    96                 }
    97 
    98                 iDisplay.SwapBuffers();
    99 
   100             }
   101 
   102             //Compute instant FPS
   103             toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   104 
   105             LastTickTime = NewTickTime;
   106 
   107         }
   108 
   109         private void buttonOpen_Click(object sender, EventArgs e)
   110         {
   111             if (iDisplay.Open())
   112             {
   113                 UpdateStatus();
   114             }
   115             else
   116             {
   117                 UpdateStatus();
   118                 toolStripStatusLabelConnect.Text = "Connection error";
   119             }
   120 
   121         }
   122 
   123         private void buttonClose_Click(object sender, EventArgs e)
   124         {
   125             iDisplay.Close();
   126             UpdateStatus();
   127         }
   128 
   129         private void buttonClear_Click(object sender, EventArgs e)
   130         {
   131             iDisplay.Clear();
   132             iDisplay.SwapBuffers();
   133         }
   134 
   135         private void buttonFill_Click(object sender, EventArgs e)
   136         {
   137             iDisplay.Fill();
   138             iDisplay.SwapBuffers();
   139         }
   140 
   141         private void trackBarBrightness_Scroll(object sender, EventArgs e)
   142         {
   143             Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
   144             Properties.Settings.Default.Save();
   145             iDisplay.SetBrightness(trackBarBrightness.Value);
   146 
   147         }
   148 
   149         private void UpdateStatus()
   150         {
   151             if (iDisplay.IsOpen())
   152             {
   153                 buttonFill.Enabled = true;
   154                 buttonClear.Enabled = true;
   155                 buttonOpen.Enabled = false;
   156                 buttonClose.Enabled = true;
   157                 trackBarBrightness.Enabled = true;
   158                 trackBarBrightness.Minimum = iDisplay.MinBrightness();
   159                 trackBarBrightness.Maximum = iDisplay.MaxBrightness();                
   160                 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
   161                 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
   162                 trackBarBrightness.SmallChange = 1;
   163                 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
   164 
   165                 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
   166                 //+ " - " + iDisplay.SerialNumber();
   167             }
   168             else
   169             {
   170                 buttonFill.Enabled = false;
   171                 buttonClear.Enabled = false;
   172                 buttonOpen.Enabled = true;
   173                 buttonClose.Enabled = false;
   174                 trackBarBrightness.Enabled = false;
   175                 toolStripStatusLabelConnect.Text = "Disconnected";
   176             }
   177         }
   178 
   179         private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
   180         {
   181             Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
   182             Properties.Settings.Default.Save();
   183         }
   184     }
   185 }