public static void RotateColors(IntPtr hwnd) { SafeDCHandle deviceContext = GetDCEx( hwnd, IntPtr.Zero, DeviceContextValues.DCX_LOCKWINDOWUPDATE | DeviceContextValues.DCX_CACHE | DeviceContextValues.DCX_WINDOW); if (deviceContext == SafeDCHandle.Null) { throw new Exception("Failed to get a device context"); } bool rectResult = GetWindowRect(hwnd, out RECT rect); if (!rectResult) { ThrowLoastWin32(); } Console.WriteLine("Press Q to quit"); using (Graphics gr = Graphics.FromHdc(deviceContext.DangerousGetHandle())) { const int numSteps = 512; const double max = 2 * Math.PI; const double stepSize = max / numSteps; const double center = 255f / 2; const double rPhase = 0; const double gPhase = 2 * Math.PI / 3; const double bPhase = 2 * Math.PI / 3 * 2; bool keepLooping = true; while (keepLooping) { for (double theta = 0; theta <= max && keepLooping; theta += stepSize) { byte r = (byte)(Math.Sin(theta + rPhase) * center + center); byte g = (byte)(Math.Sin(theta + gPhase) * center + center); byte b = (byte)(Math.Sin(theta + bPhase) * center + center); gr.FillRectangle(new SolidBrush(Color.FromArgb(r, g, b)), rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); Thread.Sleep(10); while (Console.KeyAvailable) { var key = Console.ReadKey(true); if (key.Key == ConsoleKey.Q) { Console.WriteLine("Exiting..."); keepLooping = false; break; } } } } } _ = ReleaseDC(hwnd, deviceContext.HWnd); }
/// <summary>Draws the image indicated by the given index on the specified <see cref="Graphics"/> at the specified location.</summary> /// <param name="imageList">The image list.</param> /// <param name="g">The <see cref="Graphics"/> to draw on.</param> /// <param name="bounds">The bounds in which to draw the image. Set width and height to 0 to draw image at full size.</param> /// <param name="index">The index of the image in the ImageList to draw.</param> /// <param name="bgColor"> /// The background color of the image. This parameter can be a <see cref="Color"/> value or <see cref="COLORREF.None"/> so the image is drawn /// transparently or <see cref="COLORREF.Default"/> so the image is drawn using the background color of the image list. /// </param> /// <param name="fgColor"> /// The foreground color of the image. This parameter can be a <see cref="Color"/> value or <see cref="COLORREF.None"/> so the image is blended /// with the color of the destination device context or <see cref="COLORREF.Default"/> so the image is drawn using the system highlight color /// as the foreground color. /// </param> /// <param name="style">The drawing style.</param> /// <param name="overlayImageIndex">Optional index of an overlay image.</param> /// <exception cref="System.ComponentModel.Win32Exception">Unable to draw the image with defined parameters.</exception> public static void Draw(this ImageList imageList, Graphics g, Rectangle bounds, int index, COLORREF bgColor, COLORREF fgColor, IMAGELISTDRAWFLAGS style = IMAGELISTDRAWFLAGS.ILD_NORMAL, int overlayImageIndex = 0) { if (index < 0 || index >= imageList.Images.Count) { throw new ArgumentOutOfRangeException(nameof(index)); } if (overlayImageIndex < 0 || overlayImageIndex > imageList.GetOverlayCount()) { throw new ArgumentOutOfRangeException(nameof(overlayImageIndex)); } using (var hg = new SafeDCHandle(g)) { var p = new IMAGELISTDRAWPARAMS(hg.DangerousGetHandle(), bounds, index, bgColor, style | (IMAGELISTDRAWFLAGS)INDEXTOOVERLAYMASK(overlayImageIndex)) { rgbFg = fgColor }; imageList.GetIImageList().Draw(p); } }