sl@0: using System; sl@0: using System.Collections.Generic; sl@0: using System.ComponentModel; sl@0: using System.Data; sl@0: using System.Drawing; sl@0: using System.Linq; sl@0: using System.Text; sl@0: using System.Threading.Tasks; sl@0: using System.Windows.Forms; sl@14: using System.IO; sl@0: using CodeProject.Dialog; sl@14: using System.Drawing.Imaging; sl@14: sl@0: sl@0: namespace SharpDisplayManager sl@0: { sl@0: public partial class MainForm : Form sl@0: { sl@2: DateTime LastTickTime; sl@3: Display iDisplay; sl@14: System.Drawing.Bitmap iBmp; sl@14: bool iCreateBitmap; //Workaround render to bitmap issues when minimized sl@2: sl@0: public MainForm() sl@0: { sl@2: LastTickTime = DateTime.Now; sl@3: iDisplay = new Display(); sl@2: sl@0: InitializeComponent(); sl@7: UpdateStatus(); sl@13: sl@8: //Load settings sl@8: marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont; sl@8: marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont; sl@9: checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders; sl@13: checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup; sl@16: checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen; sl@13: // sl@13: tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None); sl@14: //We have a bug when drawing minimized and reusing our bitmap sl@14: iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb); sl@14: iCreateBitmap = false; sl@0: } sl@0: sl@13: private void MainForm_Load(object sender, EventArgs e) sl@13: { sl@13: if (Properties.Settings.Default.DisplayConnectOnStartup) sl@13: { sl@13: iDisplay.Open(); sl@13: UpdateStatus(); sl@13: } sl@13: } sl@13: sl@13: sl@0: private void buttonFont_Click(object sender, EventArgs e) sl@0: { sl@0: //fontDialog.ShowColor = true; sl@0: //fontDialog.ShowApply = true; sl@0: fontDialog.ShowEffects = true; sl@11: fontDialog.Font = marqueeLabelTop.Font; sl@0: //fontDialog.ShowHelp = true; sl@0: sl@0: //fontDlg.MaxSize = 40; sl@0: //fontDlg.MinSize = 22; sl@0: sl@0: //fontDialog.Parent = this; sl@0: //fontDialog.StartPosition = FormStartPosition.CenterParent; sl@0: sl@0: //DlgBox.ShowDialog(fontDialog); sl@0: sl@0: //if (fontDialog.ShowDialog(this) != DialogResult.Cancel) sl@0: if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel) sl@0: { sl@0: sl@4: //MsgBox.Show("MessageBox MsgBox", "MsgBox caption"); sl@0: sl@0: //MessageBox.Show("Ok"); sl@4: marqueeLabelTop.Font = fontDialog.Font; sl@4: marqueeLabelBottom.Font = fontDialog.Font; sl@8: Properties.Settings.Default.DisplayFont = fontDialog.Font; sl@8: Properties.Settings.Default.Save(); sl@0: //label1.Font = fontDlg.Font; sl@0: //textBox1.BackColor = fontDlg.Color; sl@0: //label1.ForeColor = fontDlg.Color; sl@0: } sl@0: } sl@0: sl@0: private void buttonCapture_Click(object sender, EventArgs e) sl@0: { sl@0: System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height); sl@0: tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle); sl@14: //Bitmap bmpToSave = new Bitmap(bmp); sl@14: bmp.Save("D:\\capture.png"); sl@14: sl@14: /* sl@14: string outputFileName = "d:\\capture.png"; sl@14: using (MemoryStream memory = new MemoryStream()) sl@14: { sl@14: using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) sl@14: { sl@14: bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png); sl@14: byte[] bytes = memory.ToArray(); sl@14: fs.Write(bytes, 0, bytes.Length); sl@14: } sl@14: } sl@14: */ sl@14: sl@0: } sl@2: sl@12: private void CheckForRequestResults() sl@12: { sl@12: if (iDisplay.IsRequestPending()) sl@12: { sl@12: switch (iDisplay.AttemptRequestCompletion()) sl@12: { sl@12: case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus: sl@12: if (iDisplay.PowerSupplyStatus()) sl@12: { sl@12: toolStripStatusLabelPower.Text = "ON"; sl@12: } sl@12: else sl@12: { sl@12: toolStripStatusLabelPower.Text = "OFF"; sl@12: } sl@12: //Issue next request then sl@12: iDisplay.RequestDeviceId(); sl@12: break; sl@12: sl@12: case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId: sl@12: toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId(); sl@12: //Issue next request then sl@12: iDisplay.RequestFirmwareRevision(); sl@12: break; sl@12: sl@12: case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision: sl@12: toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision(); sl@12: //No more request to issue sl@12: break; sl@12: } sl@12: } sl@12: } sl@12: sl@16: sl@16: public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt); sl@16: sl@16: sl@16: public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX) sl@16: { sl@16: return aBmp.Width - aX - 1; sl@16: } sl@16: sl@16: public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY) sl@16: { sl@16: return iBmp.Height - aY - 1; sl@16: } sl@16: sl@16: public int ScreenX(System.Drawing.Bitmap aBmp, int aX) sl@16: { sl@16: return aX; sl@16: } sl@16: sl@16: public int ScreenY(System.Drawing.Bitmap aBmp, int aY) sl@16: { sl@16: return aY; sl@16: } sl@16: sl@16: sl@16: //This is our timer tick responsible to perform our render sl@2: private void timer_Tick(object sender, EventArgs e) sl@14: { sl@2: //Update our animations sl@2: DateTime NewTickTime = DateTime.Now; sl@2: sl@2: marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime); sl@2: marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime); sl@2: sl@4: //Update our display sl@4: if (iDisplay.IsOpen()) sl@4: { sl@12: CheckForRequestResults(); sl@12: sl@14: //Draw to bitmap sl@14: if (iCreateBitmap) sl@14: { sl@14: iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb); sl@14: } sl@14: tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle); sl@14: //iBmp.Save("D:\\capture.png"); sl@16: sl@16: //Select proper coordinate translation functions sl@16: //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels sl@16: CoordinateTranslationDelegate screenX; sl@16: CoordinateTranslationDelegate screenY; sl@16: sl@16: if (Properties.Settings.Default.DisplayReverseScreen) sl@16: { sl@16: screenX = ScreenReversedX; sl@16: screenY = ScreenReversedY; sl@16: } sl@16: else sl@16: { sl@16: screenX = ScreenX; sl@16: screenY = ScreenY; sl@16: } sl@14: sl@7: //Send it to our display sl@14: for (int i = 0; i < iBmp.Width; i++) sl@4: { sl@14: for (int j = 0; j < iBmp.Height; j++) sl@4: { sl@4: unchecked sl@4: { sl@14: uint color = (uint)iBmp.GetPixel(i, j).ToArgb(); sl@14: //For some reason when the app is minimized in the task bar only the alpha of our color is set. sl@14: //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't. sl@16: iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000))); sl@4: } sl@4: } sl@4: } sl@4: sl@4: iDisplay.SwapBuffers(); sl@4: sl@4: } sl@8: sl@8: //Compute instant FPS sl@8: toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS"; sl@8: sl@8: LastTickTime = NewTickTime; sl@8: sl@2: } sl@3: sl@3: private void buttonOpen_Click(object sender, EventArgs e) sl@3: { sl@3: if (iDisplay.Open()) sl@3: { sl@7: UpdateStatus(); sl@12: iDisplay.RequestPowerSupplyStatus(); sl@3: } sl@7: else sl@7: { sl@7: UpdateStatus(); sl@7: toolStripStatusLabelConnect.Text = "Connection error"; sl@7: } sl@7: sl@3: } sl@3: sl@3: private void buttonClose_Click(object sender, EventArgs e) sl@3: { sl@3: iDisplay.Close(); sl@9: UpdateStatus(); sl@3: } sl@3: sl@3: private void buttonClear_Click(object sender, EventArgs e) sl@3: { sl@3: iDisplay.Clear(); sl@3: iDisplay.SwapBuffers(); sl@3: } sl@3: sl@3: private void buttonFill_Click(object sender, EventArgs e) sl@3: { sl@3: iDisplay.Fill(); sl@3: iDisplay.SwapBuffers(); sl@3: } sl@3: sl@3: private void trackBarBrightness_Scroll(object sender, EventArgs e) sl@3: { sl@9: Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value; sl@9: Properties.Settings.Default.Save(); sl@3: iDisplay.SetBrightness(trackBarBrightness.Value); sl@9: sl@3: } sl@7: sl@7: private void UpdateStatus() sl@7: { sl@7: if (iDisplay.IsOpen()) sl@7: { sl@7: buttonFill.Enabled = true; sl@7: buttonClear.Enabled = true; sl@7: buttonOpen.Enabled = false; sl@7: buttonClose.Enabled = true; sl@7: trackBarBrightness.Enabled = true; sl@7: trackBarBrightness.Minimum = iDisplay.MinBrightness(); sl@11: trackBarBrightness.Maximum = iDisplay.MaxBrightness(); sl@9: trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness; sl@9: trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5); sl@9: trackBarBrightness.SmallChange = 1; sl@9: iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness); sl@9: sl@10: toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product(); sl@10: //+ " - " + iDisplay.SerialNumber(); sl@7: } sl@7: else sl@7: { sl@7: buttonFill.Enabled = false; sl@7: buttonClear.Enabled = false; sl@7: buttonOpen.Enabled = true; sl@7: buttonClose.Enabled = false; sl@7: trackBarBrightness.Enabled = false; sl@9: toolStripStatusLabelConnect.Text = "Disconnected"; sl@7: } sl@7: } sl@9: sl@13: sl@13: sl@9: private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e) sl@9: { sl@16: //Save our show borders setting sl@13: tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None); sl@9: Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked; sl@9: Properties.Settings.Default.Save(); sl@9: } sl@13: sl@13: private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e) sl@13: { sl@16: //Save our connect on startup setting sl@13: Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked; sl@13: Properties.Settings.Default.Save(); sl@13: } sl@13: sl@16: private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e) sl@16: { sl@16: //Save our reverse screen setting sl@16: Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked; sl@16: Properties.Settings.Default.Save(); sl@16: } sl@16: sl@14: private void MainForm_Resize(object sender, EventArgs e) sl@14: { sl@14: if (WindowState == FormWindowState.Minimized) sl@14: { sl@14: // Do some stuff sl@14: //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb); sl@14: iCreateBitmap = true; sl@14: } sl@14: } sl@14: sl@0: } sl@0: }