Adding WCF server implementation.
2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
11 using CodeProject.Dialog;
12 using System.Drawing.Imaging;
13 using System.ServiceModel;
16 namespace SharpDisplayManager
18 public partial class MainForm : Form
20 DateTime LastTickTime;
22 System.Drawing.Bitmap iBmp;
23 bool iCreateBitmap; //Workaround render to bitmap issues when minimized
24 ServiceHost iServiceHost;
28 LastTickTime = DateTime.Now;
29 iDisplay = new Display();
31 InitializeComponent();
35 marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
36 marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
37 checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
38 checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
39 checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen;
41 tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
42 //We have a bug when drawing minimized and reusing our bitmap
43 iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
44 iCreateBitmap = false;
47 private void MainForm_Load(object sender, EventArgs e)
51 if (Properties.Settings.Default.DisplayConnectOnStartup)
59 private void buttonFont_Click(object sender, EventArgs e)
61 //fontDialog.ShowColor = true;
62 //fontDialog.ShowApply = true;
63 fontDialog.ShowEffects = true;
64 fontDialog.Font = marqueeLabelTop.Font;
65 //fontDialog.ShowHelp = true;
67 //fontDlg.MaxSize = 40;
68 //fontDlg.MinSize = 22;
70 //fontDialog.Parent = this;
71 //fontDialog.StartPosition = FormStartPosition.CenterParent;
73 //DlgBox.ShowDialog(fontDialog);
75 //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
76 if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
79 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
81 //MessageBox.Show("Ok");
82 marqueeLabelTop.Font = fontDialog.Font;
83 marqueeLabelBottom.Font = fontDialog.Font;
84 Properties.Settings.Default.DisplayFont = fontDialog.Font;
85 Properties.Settings.Default.Save();
86 //label1.Font = fontDlg.Font;
87 //textBox1.BackColor = fontDlg.Color;
88 //label1.ForeColor = fontDlg.Color;
92 private void buttonCapture_Click(object sender, EventArgs e)
94 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
95 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
96 //Bitmap bmpToSave = new Bitmap(bmp);
97 bmp.Save("D:\\capture.png");
99 marqueeLabelTop.Text = "Sweet";
102 string outputFileName = "d:\\capture.png";
103 using (MemoryStream memory = new MemoryStream())
105 using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
107 bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
108 byte[] bytes = memory.ToArray();
109 fs.Write(bytes, 0, bytes.Length);
116 private void CheckForRequestResults()
118 if (iDisplay.IsRequestPending())
120 switch (iDisplay.AttemptRequestCompletion())
122 case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
123 if (iDisplay.PowerSupplyStatus())
125 toolStripStatusLabelPower.Text = "ON";
129 toolStripStatusLabelPower.Text = "OFF";
131 //Issue next request then
132 iDisplay.RequestDeviceId();
135 case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
136 toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
137 //Issue next request then
138 iDisplay.RequestFirmwareRevision();
141 case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
142 toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
143 //No more request to issue
150 public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
153 public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
155 return aBmp.Width - aX - 1;
158 public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
160 return iBmp.Height - aY - 1;
163 public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
168 public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
174 //This is our timer tick responsible to perform our render
175 private void timer_Tick(object sender, EventArgs e)
177 //Update our animations
178 DateTime NewTickTime = DateTime.Now;
180 marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
181 marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
184 if (iDisplay.IsOpen())
186 CheckForRequestResults();
191 iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
193 tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
194 //iBmp.Save("D:\\capture.png");
196 //Select proper coordinate translation functions
197 //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
198 CoordinateTranslationDelegate screenX;
199 CoordinateTranslationDelegate screenY;
201 if (Properties.Settings.Default.DisplayReverseScreen)
203 screenX = ScreenReversedX;
204 screenY = ScreenReversedY;
212 //Send it to our display
213 for (int i = 0; i < iBmp.Width; i++)
215 for (int j = 0; j < iBmp.Height; j++)
219 uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
220 //For some reason when the app is minimized in the task bar only the alpha of our color is set.
221 //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
222 iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000)));
227 iDisplay.SwapBuffers();
231 //Compute instant FPS
232 toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
234 LastTickTime = NewTickTime;
238 private void buttonOpen_Click(object sender, EventArgs e)
243 iDisplay.RequestPowerSupplyStatus();
248 toolStripStatusLabelConnect.Text = "Connection error";
253 private void buttonClose_Click(object sender, EventArgs e)
259 private void buttonClear_Click(object sender, EventArgs e)
262 iDisplay.SwapBuffers();
265 private void buttonFill_Click(object sender, EventArgs e)
268 iDisplay.SwapBuffers();
271 private void trackBarBrightness_Scroll(object sender, EventArgs e)
273 Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
274 Properties.Settings.Default.Save();
275 iDisplay.SetBrightness(trackBarBrightness.Value);
279 private void UpdateStatus()
281 if (iDisplay.IsOpen())
283 buttonFill.Enabled = true;
284 buttonClear.Enabled = true;
285 buttonOpen.Enabled = false;
286 buttonClose.Enabled = true;
287 trackBarBrightness.Enabled = true;
288 trackBarBrightness.Minimum = iDisplay.MinBrightness();
289 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
290 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
291 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
292 trackBarBrightness.SmallChange = 1;
293 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
295 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
296 //+ " - " + iDisplay.SerialNumber();
300 buttonFill.Enabled = false;
301 buttonClear.Enabled = false;
302 buttonOpen.Enabled = true;
303 buttonClose.Enabled = false;
304 trackBarBrightness.Enabled = false;
305 toolStripStatusLabelConnect.Text = "Disconnected";
311 private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
313 //Save our show borders setting
314 tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
315 Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
316 Properties.Settings.Default.Save();
319 private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
321 //Save our connect on startup setting
322 Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
323 Properties.Settings.Default.Save();
326 private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
328 //Save our reverse screen setting
329 Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked;
330 Properties.Settings.Default.Save();
333 private void MainForm_Resize(object sender, EventArgs e)
335 if (WindowState == FormWindowState.Minimized)
338 //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
339 iCreateBitmap = true;
343 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
348 public void StartServer()
350 iServiceHost = new ServiceHost
352 typeof(DisplayServer),
353 new Uri[] { new Uri("net.pipe://localhost") }
356 iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetNamedPipeBinding(), "DisplayService");
360 public void StopServer()
362 //Tell connected client first? Is that possible?
363 iServiceHost.Close();