MainForm.cs
author sl
Tue, 08 Jul 2014 18:59:07 +0200
changeset 8 5129c03ab7ba
parent 7 69815d19796c
child 9 7c363a7a975e
permissions -rw-r--r--
Adding persistent settings and marquee separator.
     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         }
    30 
    31         private void buttonFont_Click(object sender, EventArgs e)
    32         {
    33             //fontDialog.ShowColor = true;
    34             //fontDialog.ShowApply = true;
    35             fontDialog.ShowEffects = true;
    36             //fontDialog.ShowHelp = true;
    37 
    38             //fontDlg.MaxSize = 40;
    39             //fontDlg.MinSize = 22;
    40 
    41             //fontDialog.Parent = this;
    42             //fontDialog.StartPosition = FormStartPosition.CenterParent;
    43 
    44             //DlgBox.ShowDialog(fontDialog);
    45 
    46             //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    47             if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    48             {
    49 
    50                 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    51 
    52                 //MessageBox.Show("Ok");
    53                 marqueeLabelTop.Font = fontDialog.Font;
    54                 marqueeLabelBottom.Font = fontDialog.Font;
    55                 Properties.Settings.Default.DisplayFont = fontDialog.Font;
    56                 Properties.Settings.Default.Save();
    57                 //label1.Font = fontDlg.Font;
    58                 //textBox1.BackColor = fontDlg.Color;
    59                 //label1.ForeColor = fontDlg.Color;
    60             }
    61         }
    62 
    63         private void buttonCapture_Click(object sender, EventArgs e)
    64         {
    65             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    66             tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    67             bmp.Save("c:\\capture.png");
    68         }
    69 
    70         private void timer_Tick(object sender, EventArgs e)
    71         {
    72             //Update our animations
    73             DateTime NewTickTime = DateTime.Now;
    74 
    75             marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
    76             marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
    77 
    78             //Update our display
    79             if (iDisplay.IsOpen())
    80             {
    81                 //Draw to bitmap
    82                 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    83                 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    84                 //Send it to our display
    85                 for (int i = 0; i < bmp.Width; i++)
    86                 {
    87                     for (int j = 0; j < bmp.Height; j++)
    88                     {
    89                         unchecked
    90                         {
    91                         uint color=(uint)bmp.GetPixel(i, j).ToArgb();
    92                         iDisplay.SetPixel(i, j, Convert.ToInt32(color!=0xFFFFFFFF));
    93                         }
    94                     }
    95                 }
    96 
    97                 iDisplay.SwapBuffers();
    98 
    99             }
   100 
   101             //Compute instant FPS
   102             toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   103 
   104             LastTickTime = NewTickTime;
   105 
   106         }
   107 
   108         private void buttonOpen_Click(object sender, EventArgs e)
   109         {
   110             if (iDisplay.Open())
   111             {
   112                 UpdateStatus();
   113             }
   114             else
   115             {
   116                 UpdateStatus();
   117                 toolStripStatusLabelConnect.Text = "Connection error";
   118             }
   119 
   120         }
   121 
   122         private void buttonClose_Click(object sender, EventArgs e)
   123         {
   124             iDisplay.Close();
   125         }
   126 
   127         private void buttonClear_Click(object sender, EventArgs e)
   128         {
   129             iDisplay.Clear();
   130             iDisplay.SwapBuffers();
   131         }
   132 
   133         private void buttonFill_Click(object sender, EventArgs e)
   134         {
   135             iDisplay.Fill();
   136             iDisplay.SwapBuffers();
   137         }
   138 
   139         private void trackBarBrightness_Scroll(object sender, EventArgs e)
   140         {
   141             iDisplay.SetBrightness(trackBarBrightness.Value);
   142         }
   143 
   144         private void UpdateStatus()
   145         {
   146             if (iDisplay.IsOpen())
   147             {
   148                 buttonFill.Enabled = true;
   149                 buttonClear.Enabled = true;
   150                 buttonOpen.Enabled = false;
   151                 buttonClose.Enabled = true;
   152                 trackBarBrightness.Enabled = true;
   153                 trackBarBrightness.Minimum = iDisplay.MinBrightness();
   154                 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
   155                 toolStripStatusLabelConnect.Text = "Connected";
   156             }
   157             else
   158             {
   159                 buttonFill.Enabled = false;
   160                 buttonClear.Enabled = false;
   161                 buttonOpen.Enabled = true;
   162                 buttonClose.Enabled = false;
   163                 trackBarBrightness.Enabled = false;
   164                 toolStripStatusLabelConnect.Text = "Not connected";
   165             }
   166         }
   167     }
   168 }