private static void DrawOnHDC(IntPtr compatHdc, string text, Font font, Padding internalBounds, Rectangle bounds, Color color, TextFormatFlags formatFlags, AeroLabelMode mode, IThemeTextOption[] options) { if (SystemSupports.IsAeroSupport && mode == AeroLabelMode.Aero) { IntPtr hFont = font.ToHfont(); NativeMethod.SelectObject(compatHdc, hFont); VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = default(DTTOPTS); dttOpts.dwSize = Marshal.SizeOf(dttOpts); dttOpts.dwFlags = (DTTOPSFlags)8193; dttOpts.crText = ColorTranslator.ToWin32(color); for (int i = 0; i < options.Length; i++) { IThemeTextOption op = options[i]; op.Apply(ref dttOpts); } RECT RECT = new RECT(internalBounds.Left, internalBounds.Top, bounds.Width - internalBounds.Right, bounds.Height - internalBounds.Bottom); int ret = NativeMethod.DrawThemeTextEx(renderer.Handle, compatHdc, 0, 0, text, -1, (int)formatFlags, ref RECT, ref dttOpts); if (ret != 0) { Marshal.ThrowExceptionForHR(ret); } NativeMethod.DeleteObject(hFont); } else { Graphics gc = Graphics.FromHdc(compatHdc); gc.TextRenderingHint = TextRenderingHint.AntiAlias; using (SolidBrush sb = new SolidBrush(Color.FromArgb(254, color))) gc.DrawString(text, font, sb, bounds); } }
public void Update(Graphics g, string text, Font font, Padding internalBounds, Rectangle bounds, Color color, Color backColor, TextFormatFlags formatFlags, IThemeTextOption[] options) { IntPtr compatHdc = this._TextHDC; if (bounds.Equals(this._TextHDCBounds)) { IntPtr hClearBrush = NativeMethod.CreateSolidBrush(ColorTranslator.ToWin32(backColor)); RECT cleanRect = new RECT(bounds); NativeMethod.FillRect(compatHdc, ref cleanRect, hClearBrush); NativeMethod.DeleteObject(hClearBrush); } else { IntPtr outputHdc = g.GetHdc(); IntPtr DIB; compatHdc = AeroText.CreateNewHDC(outputHdc, bounds, out DIB); this._TextHDC = compatHdc; this._TextHDCBounds = bounds; AeroText.CleanUpHDC(DIB); g.ReleaseHdc(outputHdc); } AeroText.DrawOnHDC(compatHdc, text, font, internalBounds, bounds, color, formatFlags, this._mode, options); }
private static void CleanUpHDC(IntPtr DIB) { NativeMethod.DeleteObject(DIB); }
public static byte[] CaptureRectangle(Rectangle r) { IntPtr wndHWND, wndHDC, capHDC, capBMP, prvHDC; wndHWND = wndHDC = capHDC = capBMP = prvHDC = IntPtr.Zero; byte[] buffer = null; Point cursorPos = Point.Empty; try { wndHWND = NativeMethod.GetDesktopWindow(); // window handle for desktop int x, y, width, height; x = r.X; y = r.Y; width = r.Width; height = r.Height; wndHDC = NativeMethod.GetDC(wndHWND); // get context for window // create compatibile capture context and bitmap capHDC = NativeMethod.CreateCompatibleDC(wndHDC); capBMP = NativeMethod.CreateCompatibleBitmap(wndHDC, width, height); // make sure bitmap non-zero if (capBMP == IntPtr.Zero) // if no compatible bitmap { NativeMethod.ReleaseDC(wndHWND, wndHDC); // release window context NativeMethod.DeleteDC(capHDC); // delete capture context throw new Exception("Not create compatible bitmap."); } // select compatible bitmap in compatible context // copy window context to compatible context // select previous bitmap back into compatible context prvHDC = (IntPtr)NativeMethod.SelectObject(capHDC, capBMP); //NativeMethod.BitBlt(capHDC, 0, 0, width, height, wndHDC, x, y, RasterOp.SRCCOPY | RasterOp.CAPTUREBLT); NativeMethod.BitBlt(capHDC, 0, 0, width, height, wndHDC, x, y, RasterOp.SRCCOPY); NativeMethod.GetCursorPos(out cursorPos); cursorPos.X -= r.Left; cursorPos.Y -= r.Top; // Draw the cursor // Always wait cursor, why? So use arrow cursor forever. //IntPtr hcur = GetCursor(); IntPtr hcur = NativeMethod.GetCurrentCursorHandle(); IconInfo iconInfo = new IconInfo(); int hr = NativeMethod.GetIconInfo(hcur, out iconInfo); if (hr != 0) { cursorPos.X -= iconInfo.xHotspot; cursorPos.Y -= iconInfo.yHotspot; if (iconInfo.hbmMask != IntPtr.Zero) { NativeMethod.DeleteObject(iconInfo.hbmMask); } if (iconInfo.hbmColor != IntPtr.Zero) { NativeMethod.DeleteObject(iconInfo.hbmColor); } } NativeMethod.DrawIcon(capHDC, cursorPos.X, cursorPos.Y, hcur); NativeMethod.SelectObject(capHDC, prvHDC); // create GDI+ bitmap for window Bitmap bitmap = Image.FromHbitmap(capBMP); Bitmap b = ImageUtil.AnyBitmapToBitmap24(bitmap); bitmap.Dispose(); bitmap = b; buffer = ImageUtil.BitmapToByte(bitmap); //buffer = ImageUtil.BitmapToByteEx(bitmap); bitmap.Dispose(); } catch (Exception) { } finally { // release window and capture resources NativeMethod.DeleteObject(capBMP); // delete capture bitmap NativeMethod.DeleteDC(capHDC); // delete capture context NativeMethod.ReleaseDC(wndHWND, wndHDC); // release window context } return(buffer); }