private void UpdateStateTimer_Tick(object sender, EventArgs e) { #region Set static leds. foreach (int button in staticButtons) { int oldy = button / 8; int oldx = button - (oldy * 8); arduinome.setLed((byte)oldx, (byte)oldy, true); } #endregion #region Flash blinking leds. foreach (int button in blinkingButtons) { int oldy = button / 8; int oldx = button - (oldy * 8); arduinome.setLed((byte)oldx, (byte)oldy, blinkState); } blinkState = !blinkState; #endregion #region Check for laserOS application. laserOSWindow = IntPtr.Zero; foreach (string item in EnumDesktopWindows.GetDesktopWindowsCaptions()) { if (item.StartsWith("LaserOS ") && item.EndsWith("Visualizer")) { laserOSWindow = FindWindow(null, item); break; } } if (laserOSWindow != IntPtr.Zero) { isConnectedToLaserOS.Checked = true; } else { isConnectedToLaserOS.Checked = false; } #endregion #region Get LaserOS status. if (laserOSWindow != IntPtr.Zero) { LaserOSStatus stat = checkLaserOSStatus(); laserOn.Checked = stat.LaserOn; simulatorActive.Checked = stat.SimulatorOn; runningAutoRandom.Checked = stat.AutoRandomMode; } #endregion }
private LaserOSStatus checkLaserOSStatus() { LaserOSStatus retStats = new LaserOSStatus() { LaserOn = false, AutoRandomMode = false, SimulatorOn = false }; if (laserOSWindow != IntPtr.Zero) { Color c; var bmp = new Bitmap(561, 310, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(bmp); /* * IntPtr oldWindow = GetForegroundWindow(); * while (GetForegroundWindow() != laserOSWindow) * SetForegroundWindow(laserOSWindow); */ RECT rct = new RECT(); GetWindowRect(laserOSWindow, ref rct); graphics.CopyFromScreen(rct.Left, rct.Top, 0, 0, new Size((rct.Right - rct.Left), (rct.Bottom - rct.Top)), CopyPixelOperation.SourceCopy); c = bmp.GetPixel(68, 73); if (c.R == 151 && c.G == 255 && c.B == 15) // ARGB=(255, 248, 108, 134) == Laser off || ARGB=(255, 151, 255, 15) == Laser on { retStats.LaserOn = true; } c = bmp.GetPixel(433, 78); if (c.R == 29 && c.G == 78 && c.B == 97) // ARGB=(255, 34, 59, 74) == Simulator off || ARGB=(255, 29, 78, 97) == Simulator on { retStats.SimulatorOn = true; } c = bmp.GetPixel(215, 245); if (c.R == 153 && c.G == 159 && c.B == 171) // ARGB=(255, 153, 159, 171) == Random on || ARGB=(255, 51, 63, 88) || ARGB=(255, 92, 101, 121) == Random off { retStats.AutoRandomMode = true; } /* * while (GetForegroundWindow() != oldWindow) * SetForegroundWindow(oldWindow); */ } return(retStats); }