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