Removing older solution file.
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;
14 using SharpDisplayInterface;
17 namespace SharpDisplayManager
19 public partial class MainForm : Form
21 DateTime LastTickTime;
23 System.Drawing.Bitmap iBmp;
24 bool iCreateBitmap; //Workaround render to bitmap issues when minimized
25 ServiceHost iServiceHost;
27 /// Our collection of clients
29 public Dictionary<string, IDisplayServiceCallback> iClients;
33 LastTickTime = DateTime.Now;
34 iDisplay = new Display();
35 iClients = new Dictionary<string, IDisplayServiceCallback>();
37 InitializeComponent();
41 marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
42 marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
43 checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
44 checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
45 checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen;
47 tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
48 //We have a bug when drawing minimized and reusing our bitmap
49 iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
50 iCreateBitmap = false;
53 private void MainForm_Load(object sender, EventArgs e)
57 if (Properties.Settings.Default.DisplayConnectOnStartup)
65 private void buttonFont_Click(object sender, EventArgs e)
67 //fontDialog.ShowColor = true;
68 //fontDialog.ShowApply = true;
69 fontDialog.ShowEffects = true;
70 fontDialog.Font = marqueeLabelTop.Font;
71 //fontDialog.ShowHelp = true;
73 //fontDlg.MaxSize = 40;
74 //fontDlg.MinSize = 22;
76 //fontDialog.Parent = this;
77 //fontDialog.StartPosition = FormStartPosition.CenterParent;
79 //DlgBox.ShowDialog(fontDialog);
81 //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
82 if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
85 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
87 //MessageBox.Show("Ok");
88 marqueeLabelTop.Font = fontDialog.Font;
89 marqueeLabelBottom.Font = fontDialog.Font;
90 Properties.Settings.Default.DisplayFont = fontDialog.Font;
91 Properties.Settings.Default.Save();
92 //label1.Font = fontDlg.Font;
93 //textBox1.BackColor = fontDlg.Color;
94 //label1.ForeColor = fontDlg.Color;
98 private void buttonCapture_Click(object sender, EventArgs e)
100 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
101 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
102 //Bitmap bmpToSave = new Bitmap(bmp);
103 bmp.Save("D:\\capture.png");
105 marqueeLabelTop.Text = "Sweet";
108 string outputFileName = "d:\\capture.png";
109 using (MemoryStream memory = new MemoryStream())
111 using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
113 bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
114 byte[] bytes = memory.ToArray();
115 fs.Write(bytes, 0, bytes.Length);
122 private void CheckForRequestResults()
124 if (iDisplay.IsRequestPending())
126 switch (iDisplay.AttemptRequestCompletion())
128 case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
129 if (iDisplay.PowerSupplyStatus())
131 toolStripStatusLabelPower.Text = "ON";
135 toolStripStatusLabelPower.Text = "OFF";
137 //Issue next request then
138 iDisplay.RequestDeviceId();
141 case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
142 toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
143 //Issue next request then
144 iDisplay.RequestFirmwareRevision();
147 case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
148 toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
149 //No more request to issue
156 public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
159 public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
161 return aBmp.Width - aX - 1;
164 public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
166 return iBmp.Height - aY - 1;
169 public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
174 public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
180 //This is our timer tick responsible to perform our render
181 private void timer_Tick(object sender, EventArgs e)
183 //Update our animations
184 DateTime NewTickTime = DateTime.Now;
186 marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
187 marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
190 if (iDisplay.IsOpen())
192 CheckForRequestResults();
197 iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
199 tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
200 //iBmp.Save("D:\\capture.png");
202 //Select proper coordinate translation functions
203 //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
204 CoordinateTranslationDelegate screenX;
205 CoordinateTranslationDelegate screenY;
207 if (Properties.Settings.Default.DisplayReverseScreen)
209 screenX = ScreenReversedX;
210 screenY = ScreenReversedY;
218 //Send it to our display
219 for (int i = 0; i < iBmp.Width; i++)
221 for (int j = 0; j < iBmp.Height; j++)
225 uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
226 //For some reason when the app is minimized in the task bar only the alpha of our color is set.
227 //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
228 iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000)));
233 iDisplay.SwapBuffers();
237 //Compute instant FPS
238 toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
240 LastTickTime = NewTickTime;
244 private void buttonOpen_Click(object sender, EventArgs e)
249 iDisplay.RequestPowerSupplyStatus();
254 toolStripStatusLabelConnect.Text = "Connection error";
259 private void buttonClose_Click(object sender, EventArgs e)
265 private void buttonClear_Click(object sender, EventArgs e)
268 iDisplay.SwapBuffers();
271 private void buttonFill_Click(object sender, EventArgs e)
274 iDisplay.SwapBuffers();
277 private void trackBarBrightness_Scroll(object sender, EventArgs e)
279 Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
280 Properties.Settings.Default.Save();
281 iDisplay.SetBrightness(trackBarBrightness.Value);
285 private void UpdateStatus()
287 if (iDisplay.IsOpen())
289 buttonFill.Enabled = true;
290 buttonClear.Enabled = true;
291 buttonOpen.Enabled = false;
292 buttonClose.Enabled = true;
293 trackBarBrightness.Enabled = true;
294 trackBarBrightness.Minimum = iDisplay.MinBrightness();
295 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
296 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
297 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
298 trackBarBrightness.SmallChange = 1;
299 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
301 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
302 //+ " - " + iDisplay.SerialNumber();
306 buttonFill.Enabled = false;
307 buttonClear.Enabled = false;
308 buttonOpen.Enabled = true;
309 buttonClose.Enabled = false;
310 trackBarBrightness.Enabled = false;
311 toolStripStatusLabelConnect.Text = "Disconnected";
317 private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
319 //Save our show borders setting
320 tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
321 Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
322 Properties.Settings.Default.Save();
325 private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
327 //Save our connect on startup setting
328 Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
329 Properties.Settings.Default.Save();
332 private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
334 //Save our reverse screen setting
335 Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked;
336 Properties.Settings.Default.Save();
339 private void MainForm_Resize(object sender, EventArgs e)
341 if (WindowState == FormWindowState.Minimized)
344 //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
345 iCreateBitmap = true;
349 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
354 public void StartServer()
356 iServiceHost = new ServiceHost
358 typeof(DisplayServer),
359 new Uri[] { new Uri("net.tcp://localhost:8001/") }
362 iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetTcpBinding(), "DisplayService");
366 public void StopServer()
368 //Tell connected client first? Is that possible?
369 BroadcastCloseEvent();
370 iServiceHost.Close();
373 public void BroadcastCloseEvent()
375 var inactiveClients = new List<string>();
376 foreach (var client in iClients)
378 //if (client.Key != eventData.ClientName)
382 client.Value.OnServerClosing(/*eventData*/);
386 inactiveClients.Add(client.Key);
391 if (inactiveClients.Count > 0)
393 foreach (var client in inactiveClients)
395 iClients.Remove(client);