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