Server/MainForm.cs
changeset 18 7acec5059fa6
parent 17 19c1aaf900dc
child 20 e3d394dd0388
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Server/MainForm.cs	Tue Aug 12 20:55:50 2014 +0200
     1.3 @@ -0,0 +1,368 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.ComponentModel;
     1.7 +using System.Data;
     1.8 +using System.Drawing;
     1.9 +using System.Linq;
    1.10 +using System.Text;
    1.11 +using System.Threading.Tasks;
    1.12 +using System.Windows.Forms;
    1.13 +using System.IO;
    1.14 +using CodeProject.Dialog;
    1.15 +using System.Drawing.Imaging;
    1.16 +using System.ServiceModel;
    1.17 +
    1.18 +
    1.19 +namespace SharpDisplayManager
    1.20 +{
    1.21 +    public partial class MainForm : Form
    1.22 +    {
    1.23 +        DateTime LastTickTime;
    1.24 +        Display iDisplay;
    1.25 +        System.Drawing.Bitmap iBmp;
    1.26 +        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
    1.27 +        ServiceHost iServiceHost;
    1.28 +
    1.29 +        public MainForm()
    1.30 +        {
    1.31 +            LastTickTime = DateTime.Now;
    1.32 +            iDisplay = new Display();
    1.33 +
    1.34 +            InitializeComponent();
    1.35 +            UpdateStatus();
    1.36 +
    1.37 +            //Load settings
    1.38 +            marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
    1.39 +            marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
    1.40 +            checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
    1.41 +            checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
    1.42 +            checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen;
    1.43 +            //
    1.44 +            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
    1.45 +            //We have a bug when drawing minimized and reusing our bitmap
    1.46 +            iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
    1.47 +            iCreateBitmap = false;
    1.48 +        }
    1.49 +
    1.50 +        private void MainForm_Load(object sender, EventArgs e)
    1.51 +        {
    1.52 +            StartServer();
    1.53 +
    1.54 +            if (Properties.Settings.Default.DisplayConnectOnStartup)
    1.55 +            {
    1.56 +                iDisplay.Open();
    1.57 +                UpdateStatus();
    1.58 +            }
    1.59 +        }
    1.60 +
    1.61 +
    1.62 +        private void buttonFont_Click(object sender, EventArgs e)
    1.63 +        {
    1.64 +            //fontDialog.ShowColor = true;
    1.65 +            //fontDialog.ShowApply = true;
    1.66 +            fontDialog.ShowEffects = true;
    1.67 +            fontDialog.Font = marqueeLabelTop.Font;
    1.68 +            //fontDialog.ShowHelp = true;
    1.69 +
    1.70 +            //fontDlg.MaxSize = 40;
    1.71 +            //fontDlg.MinSize = 22;
    1.72 +
    1.73 +            //fontDialog.Parent = this;
    1.74 +            //fontDialog.StartPosition = FormStartPosition.CenterParent;
    1.75 +
    1.76 +            //DlgBox.ShowDialog(fontDialog);
    1.77 +
    1.78 +            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    1.79 +            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    1.80 +            {
    1.81 +
    1.82 +                //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    1.83 +
    1.84 +                //MessageBox.Show("Ok");
    1.85 +                marqueeLabelTop.Font = fontDialog.Font;
    1.86 +                marqueeLabelBottom.Font = fontDialog.Font;
    1.87 +                Properties.Settings.Default.DisplayFont = fontDialog.Font;
    1.88 +                Properties.Settings.Default.Save();
    1.89 +                //label1.Font = fontDlg.Font;
    1.90 +                //textBox1.BackColor = fontDlg.Color;
    1.91 +                //label1.ForeColor = fontDlg.Color;
    1.92 +            }
    1.93 +        }
    1.94 +
    1.95 +        private void buttonCapture_Click(object sender, EventArgs e)
    1.96 +        {
    1.97 +            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    1.98 +            tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    1.99 +            //Bitmap bmpToSave = new Bitmap(bmp);
   1.100 +            bmp.Save("D:\\capture.png");
   1.101 +
   1.102 +            marqueeLabelTop.Text = "Sweet";
   1.103 +
   1.104 +            /*
   1.105 +            string outputFileName = "d:\\capture.png";
   1.106 +            using (MemoryStream memory = new MemoryStream())
   1.107 +            {
   1.108 +                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
   1.109 +                {
   1.110 +                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
   1.111 +                    byte[] bytes = memory.ToArray();
   1.112 +                    fs.Write(bytes, 0, bytes.Length);
   1.113 +                }
   1.114 +            }
   1.115 +             */
   1.116 +
   1.117 +        }
   1.118 +
   1.119 +        private void CheckForRequestResults()
   1.120 +        {
   1.121 +            if (iDisplay.IsRequestPending())
   1.122 +            {
   1.123 +                switch (iDisplay.AttemptRequestCompletion())
   1.124 +                {
   1.125 +                    case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
   1.126 +                        if (iDisplay.PowerSupplyStatus())
   1.127 +                        {
   1.128 +                            toolStripStatusLabelPower.Text = "ON";
   1.129 +                        }
   1.130 +                        else
   1.131 +                        {
   1.132 +                            toolStripStatusLabelPower.Text = "OFF";
   1.133 +                        }
   1.134 +                        //Issue next request then
   1.135 +                        iDisplay.RequestDeviceId();
   1.136 +                        break;
   1.137 +
   1.138 +                    case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
   1.139 +                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
   1.140 +                        //Issue next request then
   1.141 +                        iDisplay.RequestFirmwareRevision();
   1.142 +                        break;
   1.143 +
   1.144 +                    case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
   1.145 +                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
   1.146 +                        //No more request to issue
   1.147 +                        break;
   1.148 +                }
   1.149 +            }
   1.150 +        }
   1.151 +
   1.152 +
   1.153 +        public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
   1.154 +
   1.155 +
   1.156 +        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
   1.157 +        {
   1.158 +            return aBmp.Width - aX - 1;
   1.159 +        }
   1.160 +
   1.161 +        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
   1.162 +        {
   1.163 +            return iBmp.Height - aY - 1;
   1.164 +        }
   1.165 +
   1.166 +        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
   1.167 +        {
   1.168 +            return aX;
   1.169 +        }
   1.170 +
   1.171 +        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
   1.172 +        {
   1.173 +            return aY;
   1.174 +        }
   1.175 +
   1.176 +
   1.177 +        //This is our timer tick responsible to perform our render
   1.178 +        private void timer_Tick(object sender, EventArgs e)
   1.179 +        {
   1.180 +            //Update our animations
   1.181 +            DateTime NewTickTime = DateTime.Now;
   1.182 +
   1.183 +            marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
   1.184 +            marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
   1.185 +
   1.186 +            //Update our display
   1.187 +            if (iDisplay.IsOpen())
   1.188 +            {
   1.189 +                CheckForRequestResults();
   1.190 +
   1.191 +                //Draw to bitmap                
   1.192 +                if (iCreateBitmap)
   1.193 +                {
   1.194 +                    iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   1.195 +                }
   1.196 +                tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
   1.197 +                //iBmp.Save("D:\\capture.png");
   1.198 +
   1.199 +                //Select proper coordinate translation functions
   1.200 +                //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
   1.201 +                CoordinateTranslationDelegate screenX;
   1.202 +                CoordinateTranslationDelegate screenY;
   1.203 +
   1.204 +                if (Properties.Settings.Default.DisplayReverseScreen)
   1.205 +                {
   1.206 +                    screenX = ScreenReversedX;
   1.207 +                    screenY = ScreenReversedY;
   1.208 +                }
   1.209 +                else
   1.210 +                {
   1.211 +                    screenX = ScreenX;
   1.212 +                    screenY = ScreenY;
   1.213 +                }
   1.214 +                
   1.215 +                //Send it to our display
   1.216 +                for (int i = 0; i < iBmp.Width; i++)
   1.217 +                {
   1.218 +                    for (int j = 0; j < iBmp.Height; j++)
   1.219 +                    {
   1.220 +                        unchecked
   1.221 +                        {
   1.222 +                            uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
   1.223 +                            //For some reason when the app is minimized in the task bar only the alpha of our color is set.
   1.224 +                            //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
   1.225 +                            iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000)));
   1.226 +                        }
   1.227 +                    }
   1.228 +                }
   1.229 +
   1.230 +                iDisplay.SwapBuffers();
   1.231 +
   1.232 +            }
   1.233 +
   1.234 +            //Compute instant FPS
   1.235 +            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   1.236 +
   1.237 +            LastTickTime = NewTickTime;
   1.238 +
   1.239 +        }
   1.240 +
   1.241 +        private void buttonOpen_Click(object sender, EventArgs e)
   1.242 +        {
   1.243 +            if (iDisplay.Open())
   1.244 +            {
   1.245 +                UpdateStatus();
   1.246 +                iDisplay.RequestPowerSupplyStatus();
   1.247 +            }
   1.248 +            else
   1.249 +            {
   1.250 +                UpdateStatus();
   1.251 +                toolStripStatusLabelConnect.Text = "Connection error";
   1.252 +            }
   1.253 +
   1.254 +        }
   1.255 +
   1.256 +        private void buttonClose_Click(object sender, EventArgs e)
   1.257 +        {
   1.258 +            iDisplay.Close();
   1.259 +            UpdateStatus();
   1.260 +        }
   1.261 +
   1.262 +        private void buttonClear_Click(object sender, EventArgs e)
   1.263 +        {
   1.264 +            iDisplay.Clear();
   1.265 +            iDisplay.SwapBuffers();
   1.266 +        }
   1.267 +
   1.268 +        private void buttonFill_Click(object sender, EventArgs e)
   1.269 +        {
   1.270 +            iDisplay.Fill();
   1.271 +            iDisplay.SwapBuffers();
   1.272 +        }
   1.273 +
   1.274 +        private void trackBarBrightness_Scroll(object sender, EventArgs e)
   1.275 +        {
   1.276 +            Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
   1.277 +            Properties.Settings.Default.Save();
   1.278 +            iDisplay.SetBrightness(trackBarBrightness.Value);
   1.279 +
   1.280 +        }
   1.281 +
   1.282 +        private void UpdateStatus()
   1.283 +        {
   1.284 +            if (iDisplay.IsOpen())
   1.285 +            {
   1.286 +                buttonFill.Enabled = true;
   1.287 +                buttonClear.Enabled = true;
   1.288 +                buttonOpen.Enabled = false;
   1.289 +                buttonClose.Enabled = true;
   1.290 +                trackBarBrightness.Enabled = true;
   1.291 +                trackBarBrightness.Minimum = iDisplay.MinBrightness();
   1.292 +                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
   1.293 +                trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
   1.294 +                trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
   1.295 +                trackBarBrightness.SmallChange = 1;
   1.296 +                iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
   1.297 +
   1.298 +                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
   1.299 +                //+ " - " + iDisplay.SerialNumber();
   1.300 +            }
   1.301 +            else
   1.302 +            {
   1.303 +                buttonFill.Enabled = false;
   1.304 +                buttonClear.Enabled = false;
   1.305 +                buttonOpen.Enabled = true;
   1.306 +                buttonClose.Enabled = false;
   1.307 +                trackBarBrightness.Enabled = false;
   1.308 +                toolStripStatusLabelConnect.Text = "Disconnected";
   1.309 +            }
   1.310 +        }
   1.311 +
   1.312 +
   1.313 +
   1.314 +        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
   1.315 +        {
   1.316 +            //Save our show borders setting
   1.317 +            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
   1.318 +            Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
   1.319 +            Properties.Settings.Default.Save();
   1.320 +        }
   1.321 +
   1.322 +        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
   1.323 +        {
   1.324 +            //Save our connect on startup setting
   1.325 +            Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
   1.326 +            Properties.Settings.Default.Save();
   1.327 +        }
   1.328 +
   1.329 +        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
   1.330 +        {
   1.331 +            //Save our reverse screen setting
   1.332 +            Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked;
   1.333 +            Properties.Settings.Default.Save();
   1.334 +        }
   1.335 +
   1.336 +        private void MainForm_Resize(object sender, EventArgs e)
   1.337 +        {
   1.338 +            if (WindowState == FormWindowState.Minimized)
   1.339 +            {
   1.340 +                // Do some stuff
   1.341 +                //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   1.342 +                iCreateBitmap = true;
   1.343 +            }
   1.344 +        }
   1.345 +
   1.346 +        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   1.347 +        {
   1.348 +            StopServer();
   1.349 +        }
   1.350 +
   1.351 +        public void StartServer()
   1.352 +        {
   1.353 +            iServiceHost = new ServiceHost
   1.354 +                (
   1.355 +                    typeof(DisplayServer),
   1.356 +                    new Uri[] { new Uri("net.pipe://localhost") }
   1.357 +                );
   1.358 +
   1.359 +            iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetNamedPipeBinding(), "DisplayService");
   1.360 +            iServiceHost.Open();
   1.361 +        }
   1.362 +
   1.363 +        public void StopServer()
   1.364 +        {
   1.365 +            //Tell connected client first? Is that possible?
   1.366 +            iServiceHost.Close();
   1.367 +        }
   1.368 +
   1.369 +
   1.370 +    }
   1.371 +}