MainForm.cs
author sl
Sat, 12 Jul 2014 22:18:34 +0200
changeset 14 d1fa9de444b0
parent 13 745d9ee1a2c0
child 16 985ca4b6e099
permissions -rw-r--r--
Fixing issues with render when app is minimized.
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@14
    10
using System.IO;
sl@0
    11
using CodeProject.Dialog;
sl@14
    12
using System.Drawing.Imaging;
sl@14
    13
sl@0
    14
sl@0
    15
namespace SharpDisplayManager
sl@0
    16
{
sl@0
    17
    public partial class MainForm : Form
sl@0
    18
    {
sl@2
    19
        DateTime LastTickTime;
sl@3
    20
        Display iDisplay;
sl@14
    21
        System.Drawing.Bitmap iBmp;
sl@14
    22
        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
sl@2
    23
sl@0
    24
        public MainForm()
sl@0
    25
        {
sl@2
    26
            LastTickTime = DateTime.Now;
sl@3
    27
            iDisplay = new Display();
sl@2
    28
sl@0
    29
            InitializeComponent();
sl@7
    30
            UpdateStatus();
sl@13
    31
sl@8
    32
            //Load settings
sl@8
    33
            marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
sl@8
    34
            marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
sl@9
    35
            checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
sl@13
    36
            checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
sl@13
    37
            //
sl@13
    38
            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@14
    39
            //We have a bug when drawing minimized and reusing our bitmap
sl@14
    40
            iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
    41
            iCreateBitmap = false;
sl@0
    42
        }
sl@0
    43
sl@13
    44
        private void MainForm_Load(object sender, EventArgs e)
sl@13
    45
        {
sl@13
    46
            if (Properties.Settings.Default.DisplayConnectOnStartup)
sl@13
    47
            {
sl@13
    48
                iDisplay.Open();
sl@13
    49
                UpdateStatus();
sl@13
    50
            }
sl@13
    51
        }
sl@13
    52
sl@13
    53
sl@0
    54
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
    55
        {
sl@0
    56
            //fontDialog.ShowColor = true;
sl@0
    57
            //fontDialog.ShowApply = true;
sl@0
    58
            fontDialog.ShowEffects = true;
sl@11
    59
            fontDialog.Font = marqueeLabelTop.Font;
sl@0
    60
            //fontDialog.ShowHelp = true;
sl@0
    61
sl@0
    62
            //fontDlg.MaxSize = 40;
sl@0
    63
            //fontDlg.MinSize = 22;
sl@0
    64
sl@0
    65
            //fontDialog.Parent = this;
sl@0
    66
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
    67
sl@0
    68
            //DlgBox.ShowDialog(fontDialog);
sl@0
    69
sl@0
    70
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
    71
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
    72
            {
sl@0
    73
sl@4
    74
                //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
sl@0
    75
sl@0
    76
                //MessageBox.Show("Ok");
sl@4
    77
                marqueeLabelTop.Font = fontDialog.Font;
sl@4
    78
                marqueeLabelBottom.Font = fontDialog.Font;
sl@8
    79
                Properties.Settings.Default.DisplayFont = fontDialog.Font;
sl@8
    80
                Properties.Settings.Default.Save();
sl@0
    81
                //label1.Font = fontDlg.Font;
sl@0
    82
                //textBox1.BackColor = fontDlg.Color;
sl@0
    83
                //label1.ForeColor = fontDlg.Color;
sl@0
    84
            }
sl@0
    85
        }
sl@0
    86
sl@0
    87
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
    88
        {
sl@0
    89
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
sl@0
    90
            tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
sl@14
    91
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
    92
            bmp.Save("D:\\capture.png");
sl@14
    93
sl@14
    94
            /*
sl@14
    95
            string outputFileName = "d:\\capture.png";
sl@14
    96
            using (MemoryStream memory = new MemoryStream())
sl@14
    97
            {
sl@14
    98
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
    99
                {
sl@14
   100
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
   101
                    byte[] bytes = memory.ToArray();
sl@14
   102
                    fs.Write(bytes, 0, bytes.Length);
sl@14
   103
                }
sl@14
   104
            }
sl@14
   105
             */
sl@14
   106
sl@0
   107
        }
sl@2
   108
sl@12
   109
        private void CheckForRequestResults()
sl@12
   110
        {
sl@12
   111
            if (iDisplay.IsRequestPending())
sl@12
   112
            {
sl@12
   113
                switch (iDisplay.AttemptRequestCompletion())
sl@12
   114
                {
sl@12
   115
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
sl@12
   116
                        if (iDisplay.PowerSupplyStatus())
sl@12
   117
                        {
sl@12
   118
                            toolStripStatusLabelPower.Text = "ON";
sl@12
   119
                        }
sl@12
   120
                        else
sl@12
   121
                        {
sl@12
   122
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
   123
                        }
sl@12
   124
                        //Issue next request then
sl@12
   125
                        iDisplay.RequestDeviceId();
sl@12
   126
                        break;
sl@12
   127
sl@12
   128
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
sl@12
   129
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
   130
                        //Issue next request then
sl@12
   131
                        iDisplay.RequestFirmwareRevision();
sl@12
   132
                        break;
sl@12
   133
sl@12
   134
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
sl@12
   135
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@12
   136
                        //No more request to issue
sl@12
   137
                        break;
sl@12
   138
                }
sl@12
   139
            }
sl@12
   140
        }
sl@12
   141
sl@2
   142
        private void timer_Tick(object sender, EventArgs e)
sl@14
   143
        {
sl@2
   144
            //Update our animations
sl@2
   145
            DateTime NewTickTime = DateTime.Now;
sl@2
   146
sl@2
   147
            marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   148
            marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   149
sl@4
   150
            //Update our display
sl@4
   151
            if (iDisplay.IsOpen())
sl@4
   152
            {
sl@12
   153
                CheckForRequestResults();
sl@12
   154
sl@14
   155
                //Draw to bitmap                
sl@14
   156
                if (iCreateBitmap)
sl@14
   157
                {
sl@14
   158
                    iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   159
                }
sl@14
   160
                tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
sl@14
   161
                //iBmp.Save("D:\\capture.png");
sl@14
   162
                
sl@14
   163
                //iBmp.
sl@14
   164
sl@7
   165
                //Send it to our display
sl@14
   166
                for (int i = 0; i < iBmp.Width; i++)
sl@4
   167
                {
sl@14
   168
                    for (int j = 0; j < iBmp.Height; j++)
sl@4
   169
                    {
sl@4
   170
                        unchecked
sl@4
   171
                        {
sl@14
   172
                            uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
sl@14
   173
                            //For some reason when the app is minimized in the task bar only the alpha of our color is set.
sl@14
   174
                            //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
sl@14
   175
                            iDisplay.SetPixel(i, j, Convert.ToInt32(!(color != 0xFF000000)));
sl@4
   176
                        }
sl@4
   177
                    }
sl@4
   178
                }
sl@4
   179
sl@4
   180
                iDisplay.SwapBuffers();
sl@4
   181
sl@4
   182
            }
sl@8
   183
sl@8
   184
            //Compute instant FPS
sl@8
   185
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
sl@8
   186
sl@8
   187
            LastTickTime = NewTickTime;
sl@8
   188
sl@2
   189
        }
sl@3
   190
sl@3
   191
        private void buttonOpen_Click(object sender, EventArgs e)
sl@3
   192
        {
sl@3
   193
            if (iDisplay.Open())
sl@3
   194
            {
sl@7
   195
                UpdateStatus();
sl@12
   196
                iDisplay.RequestPowerSupplyStatus();
sl@3
   197
            }
sl@7
   198
            else
sl@7
   199
            {
sl@7
   200
                UpdateStatus();
sl@7
   201
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
   202
            }
sl@7
   203
sl@3
   204
        }
sl@3
   205
sl@3
   206
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
   207
        {
sl@3
   208
            iDisplay.Close();
sl@9
   209
            UpdateStatus();
sl@3
   210
        }
sl@3
   211
sl@3
   212
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
   213
        {
sl@3
   214
            iDisplay.Clear();
sl@3
   215
            iDisplay.SwapBuffers();
sl@3
   216
        }
sl@3
   217
sl@3
   218
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
   219
        {
sl@3
   220
            iDisplay.Fill();
sl@3
   221
            iDisplay.SwapBuffers();
sl@3
   222
        }
sl@3
   223
sl@3
   224
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
   225
        {
sl@9
   226
            Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
sl@9
   227
            Properties.Settings.Default.Save();
sl@3
   228
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
   229
sl@3
   230
        }
sl@7
   231
sl@7
   232
        private void UpdateStatus()
sl@7
   233
        {
sl@7
   234
            if (iDisplay.IsOpen())
sl@7
   235
            {
sl@7
   236
                buttonFill.Enabled = true;
sl@7
   237
                buttonClear.Enabled = true;
sl@7
   238
                buttonOpen.Enabled = false;
sl@7
   239
                buttonClose.Enabled = true;
sl@7
   240
                trackBarBrightness.Enabled = true;
sl@7
   241
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@11
   242
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
sl@9
   243
                trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
sl@9
   244
                trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@9
   245
                trackBarBrightness.SmallChange = 1;
sl@9
   246
                iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
sl@9
   247
sl@10
   248
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
   249
                //+ " - " + iDisplay.SerialNumber();
sl@7
   250
            }
sl@7
   251
            else
sl@7
   252
            {
sl@7
   253
                buttonFill.Enabled = false;
sl@7
   254
                buttonClear.Enabled = false;
sl@7
   255
                buttonOpen.Enabled = true;
sl@7
   256
                buttonClose.Enabled = false;
sl@7
   257
                trackBarBrightness.Enabled = false;
sl@9
   258
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@7
   259
            }
sl@7
   260
        }
sl@9
   261
sl@13
   262
sl@13
   263
sl@9
   264
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
   265
        {
sl@13
   266
            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@9
   267
            Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
sl@9
   268
            Properties.Settings.Default.Save();
sl@9
   269
        }
sl@13
   270
sl@13
   271
        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
sl@13
   272
        {
sl@13
   273
            Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
sl@13
   274
            Properties.Settings.Default.Save();
sl@13
   275
        }
sl@13
   276
sl@14
   277
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
   278
        {
sl@14
   279
            if (WindowState == FormWindowState.Minimized)
sl@14
   280
            {
sl@14
   281
                // Do some stuff
sl@14
   282
                //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   283
                iCreateBitmap = true;
sl@14
   284
            }
sl@14
   285
        }
sl@14
   286
sl@0
   287
    }
sl@0
   288
}