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