MainForm.cs
author sl
Mon, 11 Aug 2014 23:18:45 +0200
changeset 16 985ca4b6e099
parent 14 d1fa9de444b0
child 17 19c1aaf900dc
permissions -rw-r--r--
Adding option to reverse screen. That's useful to avoid having to set our
hardware the correct way up.
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@16
    37
            checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen;
sl@13
    38
            //
sl@13
    39
            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@14
    40
            //We have a bug when drawing minimized and reusing our bitmap
sl@14
    41
            iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
    42
            iCreateBitmap = false;
sl@0
    43
        }
sl@0
    44
sl@13
    45
        private void MainForm_Load(object sender, EventArgs e)
sl@13
    46
        {
sl@13
    47
            if (Properties.Settings.Default.DisplayConnectOnStartup)
sl@13
    48
            {
sl@13
    49
                iDisplay.Open();
sl@13
    50
                UpdateStatus();
sl@13
    51
            }
sl@13
    52
        }
sl@13
    53
sl@13
    54
sl@0
    55
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
    56
        {
sl@0
    57
            //fontDialog.ShowColor = true;
sl@0
    58
            //fontDialog.ShowApply = true;
sl@0
    59
            fontDialog.ShowEffects = true;
sl@11
    60
            fontDialog.Font = marqueeLabelTop.Font;
sl@0
    61
            //fontDialog.ShowHelp = true;
sl@0
    62
sl@0
    63
            //fontDlg.MaxSize = 40;
sl@0
    64
            //fontDlg.MinSize = 22;
sl@0
    65
sl@0
    66
            //fontDialog.Parent = this;
sl@0
    67
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
    68
sl@0
    69
            //DlgBox.ShowDialog(fontDialog);
sl@0
    70
sl@0
    71
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
    72
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
    73
            {
sl@0
    74
sl@4
    75
                //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
sl@0
    76
sl@0
    77
                //MessageBox.Show("Ok");
sl@4
    78
                marqueeLabelTop.Font = fontDialog.Font;
sl@4
    79
                marqueeLabelBottom.Font = fontDialog.Font;
sl@8
    80
                Properties.Settings.Default.DisplayFont = fontDialog.Font;
sl@8
    81
                Properties.Settings.Default.Save();
sl@0
    82
                //label1.Font = fontDlg.Font;
sl@0
    83
                //textBox1.BackColor = fontDlg.Color;
sl@0
    84
                //label1.ForeColor = fontDlg.Color;
sl@0
    85
            }
sl@0
    86
        }
sl@0
    87
sl@0
    88
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
    89
        {
sl@0
    90
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
sl@0
    91
            tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
sl@14
    92
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
    93
            bmp.Save("D:\\capture.png");
sl@14
    94
sl@14
    95
            /*
sl@14
    96
            string outputFileName = "d:\\capture.png";
sl@14
    97
            using (MemoryStream memory = new MemoryStream())
sl@14
    98
            {
sl@14
    99
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
   100
                {
sl@14
   101
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
   102
                    byte[] bytes = memory.ToArray();
sl@14
   103
                    fs.Write(bytes, 0, bytes.Length);
sl@14
   104
                }
sl@14
   105
            }
sl@14
   106
             */
sl@14
   107
sl@0
   108
        }
sl@2
   109
sl@12
   110
        private void CheckForRequestResults()
sl@12
   111
        {
sl@12
   112
            if (iDisplay.IsRequestPending())
sl@12
   113
            {
sl@12
   114
                switch (iDisplay.AttemptRequestCompletion())
sl@12
   115
                {
sl@12
   116
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
sl@12
   117
                        if (iDisplay.PowerSupplyStatus())
sl@12
   118
                        {
sl@12
   119
                            toolStripStatusLabelPower.Text = "ON";
sl@12
   120
                        }
sl@12
   121
                        else
sl@12
   122
                        {
sl@12
   123
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
   124
                        }
sl@12
   125
                        //Issue next request then
sl@12
   126
                        iDisplay.RequestDeviceId();
sl@12
   127
                        break;
sl@12
   128
sl@12
   129
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
sl@12
   130
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
   131
                        //Issue next request then
sl@12
   132
                        iDisplay.RequestFirmwareRevision();
sl@12
   133
                        break;
sl@12
   134
sl@12
   135
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
sl@12
   136
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@12
   137
                        //No more request to issue
sl@12
   138
                        break;
sl@12
   139
                }
sl@12
   140
            }
sl@12
   141
        }
sl@12
   142
sl@16
   143
sl@16
   144
        public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
sl@16
   145
sl@16
   146
sl@16
   147
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   148
        {
sl@16
   149
            return aBmp.Width - aX - 1;
sl@16
   150
        }
sl@16
   151
sl@16
   152
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   153
        {
sl@16
   154
            return iBmp.Height - aY - 1;
sl@16
   155
        }
sl@16
   156
sl@16
   157
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   158
        {
sl@16
   159
            return aX;
sl@16
   160
        }
sl@16
   161
sl@16
   162
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   163
        {
sl@16
   164
            return aY;
sl@16
   165
        }
sl@16
   166
sl@16
   167
sl@16
   168
        //This is our timer tick responsible to perform our render
sl@2
   169
        private void timer_Tick(object sender, EventArgs e)
sl@14
   170
        {
sl@2
   171
            //Update our animations
sl@2
   172
            DateTime NewTickTime = DateTime.Now;
sl@2
   173
sl@2
   174
            marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   175
            marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   176
sl@4
   177
            //Update our display
sl@4
   178
            if (iDisplay.IsOpen())
sl@4
   179
            {
sl@12
   180
                CheckForRequestResults();
sl@12
   181
sl@14
   182
                //Draw to bitmap                
sl@14
   183
                if (iCreateBitmap)
sl@14
   184
                {
sl@14
   185
                    iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   186
                }
sl@14
   187
                tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
sl@14
   188
                //iBmp.Save("D:\\capture.png");
sl@16
   189
sl@16
   190
                //Select proper coordinate translation functions
sl@16
   191
                //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@16
   192
                CoordinateTranslationDelegate screenX;
sl@16
   193
                CoordinateTranslationDelegate screenY;
sl@16
   194
sl@16
   195
                if (Properties.Settings.Default.DisplayReverseScreen)
sl@16
   196
                {
sl@16
   197
                    screenX = ScreenReversedX;
sl@16
   198
                    screenY = ScreenReversedY;
sl@16
   199
                }
sl@16
   200
                else
sl@16
   201
                {
sl@16
   202
                    screenX = ScreenX;
sl@16
   203
                    screenY = ScreenY;
sl@16
   204
                }
sl@14
   205
                
sl@7
   206
                //Send it to our display
sl@14
   207
                for (int i = 0; i < iBmp.Width; i++)
sl@4
   208
                {
sl@14
   209
                    for (int j = 0; j < iBmp.Height; j++)
sl@4
   210
                    {
sl@4
   211
                        unchecked
sl@4
   212
                        {
sl@14
   213
                            uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
sl@14
   214
                            //For some reason when the app is minimized in the task bar only the alpha of our color is set.
sl@14
   215
                            //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
sl@16
   216
                            iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000)));
sl@4
   217
                        }
sl@4
   218
                    }
sl@4
   219
                }
sl@4
   220
sl@4
   221
                iDisplay.SwapBuffers();
sl@4
   222
sl@4
   223
            }
sl@8
   224
sl@8
   225
            //Compute instant FPS
sl@8
   226
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
sl@8
   227
sl@8
   228
            LastTickTime = NewTickTime;
sl@8
   229
sl@2
   230
        }
sl@3
   231
sl@3
   232
        private void buttonOpen_Click(object sender, EventArgs e)
sl@3
   233
        {
sl@3
   234
            if (iDisplay.Open())
sl@3
   235
            {
sl@7
   236
                UpdateStatus();
sl@12
   237
                iDisplay.RequestPowerSupplyStatus();
sl@3
   238
            }
sl@7
   239
            else
sl@7
   240
            {
sl@7
   241
                UpdateStatus();
sl@7
   242
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
   243
            }
sl@7
   244
sl@3
   245
        }
sl@3
   246
sl@3
   247
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
   248
        {
sl@3
   249
            iDisplay.Close();
sl@9
   250
            UpdateStatus();
sl@3
   251
        }
sl@3
   252
sl@3
   253
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
   254
        {
sl@3
   255
            iDisplay.Clear();
sl@3
   256
            iDisplay.SwapBuffers();
sl@3
   257
        }
sl@3
   258
sl@3
   259
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
   260
        {
sl@3
   261
            iDisplay.Fill();
sl@3
   262
            iDisplay.SwapBuffers();
sl@3
   263
        }
sl@3
   264
sl@3
   265
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
   266
        {
sl@9
   267
            Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
sl@9
   268
            Properties.Settings.Default.Save();
sl@3
   269
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
   270
sl@3
   271
        }
sl@7
   272
sl@7
   273
        private void UpdateStatus()
sl@7
   274
        {
sl@7
   275
            if (iDisplay.IsOpen())
sl@7
   276
            {
sl@7
   277
                buttonFill.Enabled = true;
sl@7
   278
                buttonClear.Enabled = true;
sl@7
   279
                buttonOpen.Enabled = false;
sl@7
   280
                buttonClose.Enabled = true;
sl@7
   281
                trackBarBrightness.Enabled = true;
sl@7
   282
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@11
   283
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
sl@9
   284
                trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
sl@9
   285
                trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@9
   286
                trackBarBrightness.SmallChange = 1;
sl@9
   287
                iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
sl@9
   288
sl@10
   289
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
   290
                //+ " - " + iDisplay.SerialNumber();
sl@7
   291
            }
sl@7
   292
            else
sl@7
   293
            {
sl@7
   294
                buttonFill.Enabled = false;
sl@7
   295
                buttonClear.Enabled = false;
sl@7
   296
                buttonOpen.Enabled = true;
sl@7
   297
                buttonClose.Enabled = false;
sl@7
   298
                trackBarBrightness.Enabled = false;
sl@9
   299
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@7
   300
            }
sl@7
   301
        }
sl@9
   302
sl@13
   303
sl@13
   304
sl@9
   305
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
   306
        {
sl@16
   307
            //Save our show borders setting
sl@13
   308
            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@9
   309
            Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
sl@9
   310
            Properties.Settings.Default.Save();
sl@9
   311
        }
sl@13
   312
sl@13
   313
        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
sl@13
   314
        {
sl@16
   315
            //Save our connect on startup setting
sl@13
   316
            Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
sl@13
   317
            Properties.Settings.Default.Save();
sl@13
   318
        }
sl@13
   319
sl@16
   320
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
   321
        {
sl@16
   322
            //Save our reverse screen setting
sl@16
   323
            Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked;
sl@16
   324
            Properties.Settings.Default.Save();
sl@16
   325
        }
sl@16
   326
sl@14
   327
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
   328
        {
sl@14
   329
            if (WindowState == FormWindowState.Minimized)
sl@14
   330
            {
sl@14
   331
                // Do some stuff
sl@14
   332
                //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   333
                iCreateBitmap = true;
sl@14
   334
            }
sl@14
   335
        }
sl@14
   336
sl@0
   337
    }
sl@0
   338
}