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