Fixing issues with render when app is minimized.
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;
15 namespace SharpDisplayManager
17 public partial class MainForm : Form
19 DateTime LastTickTime;
21 System.Drawing.Bitmap iBmp;
22 bool iCreateBitmap; //Workaround render to bitmap issues when minimized
26 LastTickTime = DateTime.Now;
27 iDisplay = new Display();
29 InitializeComponent();
33 marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
34 marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
35 checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
36 checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
38 tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
39 //We have a bug when drawing minimized and reusing our bitmap
40 iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
41 iCreateBitmap = false;
44 private void MainForm_Load(object sender, EventArgs e)
46 if (Properties.Settings.Default.DisplayConnectOnStartup)
54 private void buttonFont_Click(object sender, EventArgs e)
56 //fontDialog.ShowColor = true;
57 //fontDialog.ShowApply = true;
58 fontDialog.ShowEffects = true;
59 fontDialog.Font = marqueeLabelTop.Font;
60 //fontDialog.ShowHelp = true;
62 //fontDlg.MaxSize = 40;
63 //fontDlg.MinSize = 22;
65 //fontDialog.Parent = this;
66 //fontDialog.StartPosition = FormStartPosition.CenterParent;
68 //DlgBox.ShowDialog(fontDialog);
70 //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
71 if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
74 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
76 //MessageBox.Show("Ok");
77 marqueeLabelTop.Font = fontDialog.Font;
78 marqueeLabelBottom.Font = fontDialog.Font;
79 Properties.Settings.Default.DisplayFont = fontDialog.Font;
80 Properties.Settings.Default.Save();
81 //label1.Font = fontDlg.Font;
82 //textBox1.BackColor = fontDlg.Color;
83 //label1.ForeColor = fontDlg.Color;
87 private void buttonCapture_Click(object sender, EventArgs e)
89 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
90 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
91 //Bitmap bmpToSave = new Bitmap(bmp);
92 bmp.Save("D:\\capture.png");
95 string outputFileName = "d:\\capture.png";
96 using (MemoryStream memory = new MemoryStream())
98 using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
100 bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
101 byte[] bytes = memory.ToArray();
102 fs.Write(bytes, 0, bytes.Length);
109 private void CheckForRequestResults()
111 if (iDisplay.IsRequestPending())
113 switch (iDisplay.AttemptRequestCompletion())
115 case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
116 if (iDisplay.PowerSupplyStatus())
118 toolStripStatusLabelPower.Text = "ON";
122 toolStripStatusLabelPower.Text = "OFF";
124 //Issue next request then
125 iDisplay.RequestDeviceId();
128 case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
129 toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
130 //Issue next request then
131 iDisplay.RequestFirmwareRevision();
134 case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
135 toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
136 //No more request to issue
142 private void timer_Tick(object sender, EventArgs e)
144 //Update our animations
145 DateTime NewTickTime = DateTime.Now;
147 marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
148 marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
151 if (iDisplay.IsOpen())
153 CheckForRequestResults();
158 iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
160 tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
161 //iBmp.Save("D:\\capture.png");
165 //Send it to our display
166 for (int i = 0; i < iBmp.Width; i++)
168 for (int j = 0; j < iBmp.Height; j++)
172 uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
173 //For some reason when the app is minimized in the task bar only the alpha of our color is set.
174 //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
175 iDisplay.SetPixel(i, j, Convert.ToInt32(!(color != 0xFF000000)));
180 iDisplay.SwapBuffers();
184 //Compute instant FPS
185 toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
187 LastTickTime = NewTickTime;
191 private void buttonOpen_Click(object sender, EventArgs e)
196 iDisplay.RequestPowerSupplyStatus();
201 toolStripStatusLabelConnect.Text = "Connection error";
206 private void buttonClose_Click(object sender, EventArgs e)
212 private void buttonClear_Click(object sender, EventArgs e)
215 iDisplay.SwapBuffers();
218 private void buttonFill_Click(object sender, EventArgs e)
221 iDisplay.SwapBuffers();
224 private void trackBarBrightness_Scroll(object sender, EventArgs e)
226 Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
227 Properties.Settings.Default.Save();
228 iDisplay.SetBrightness(trackBarBrightness.Value);
232 private void UpdateStatus()
234 if (iDisplay.IsOpen())
236 buttonFill.Enabled = true;
237 buttonClear.Enabled = true;
238 buttonOpen.Enabled = false;
239 buttonClose.Enabled = true;
240 trackBarBrightness.Enabled = true;
241 trackBarBrightness.Minimum = iDisplay.MinBrightness();
242 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
243 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
244 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
245 trackBarBrightness.SmallChange = 1;
246 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
248 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
249 //+ " - " + iDisplay.SerialNumber();
253 buttonFill.Enabled = false;
254 buttonClear.Enabled = false;
255 buttonOpen.Enabled = true;
256 buttonClose.Enabled = false;
257 trackBarBrightness.Enabled = false;
258 toolStripStatusLabelConnect.Text = "Disconnected";
264 private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
266 tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
267 Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
268 Properties.Settings.Default.Save();
271 private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
273 Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
274 Properties.Settings.Default.Save();
277 private void MainForm_Resize(object sender, EventArgs e)
279 if (WindowState == FormWindowState.Minimized)
282 //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
283 iCreateBitmap = true;