/// <summary> /// Looks-up a HTheme handle. /// </summary> /// <param name="windowClass"></param> /// <returns></returns> private IntPtr GetHTheme(string windowClass) { IntPtr hTheme; if (!themeHandles.TryGetValue(windowClass, out hTheme)) { if (vistaExplorerThemeOwner == null) { vistaExplorerThemeOwner = new Control(); vistaExplorerThemeOwner.CreateControl(); UXTheme.SetWindowTheme(vistaExplorerThemeOwner.Handle, Explorer, null); } //special case for TreeView and ListView classes //load Vista's Explorer theme if needed IntPtr handle = IntPtr.Zero; string classToLower = windowClass.ToLower(); if (classToLower == VisualStyleElement.TreeView.Item.Normal.ClassName.ToLower() || classToLower == VisualStyleElement.ListView.Item.Normal.ClassName.ToLower()) { handle = vistaExplorerThemeOwner.Handle; } hTheme = UXTheme.OpenThemeData(handle, windowClass); this.themeHandles.Add(windowClass, hTheme); } return(hTheme); }
/// <summary> /// Sets the specified element as the "Current" for painting. /// </summary> /// <param name="element"></param> /// <returns>True if the element may be painted (there is a theme part defined for it), false otherwise.</returns> public bool SetCurrentElement(VisualStyleElement element) { this.currentHTheme = IntPtr.Zero; this.currentElement = null; if (element == null) { return(false); } IntPtr hTheme = GetHTheme(element.ClassName); if (hTheme == IntPtr.Zero) { return(false); } if (!UXTheme.IsThemePartDefined(hTheme, element.Part, 0)) { return(false); } this.currentHTheme = hTheme; this.currentElement = element; return(true); }
public System.Drawing.Color GetThemeColor(ColorProperty color) { if (this.currentElement == null || this.currentHTheme == IntPtr.Zero) { return(System.Drawing.Color.Empty); } int pColor = 0; UXTheme.GetThemeColor(this.currentHTheme, this.currentElement.Part, this.currentElement.State, (int)color, ref pColor); return(ColorTranslator.FromWin32(pColor)); }
/// <summary> /// Gets the Color defined for the current element. /// </summary> /// <param name="color"></param> /// <returns></returns> public Color GetThemeColor(ColorProperty color) { if (this.currentElement == null || currentHTheme == IntPtr.Zero) { return(Color.Empty); } int themeColor = 0; UXTheme.GetThemeColor(this.currentHTheme, currentElement.Part, currentElement.State, (int)color, ref themeColor); return(ColorTranslator.FromWin32(themeColor)); }
public Size GetPartPreferredSize(Graphics g, Rectangle bounds, ThemeSizeType type) { if (this.currentElement == null || this.currentHTheme == IntPtr.Zero) { return(Size.Empty); } RadHdcWrapper radHdcWrapper = new RadHdcWrapper(g, false); IntPtr hdc = radHdcWrapper.GetHdc(); NativeMethods.SIZE psz = new NativeMethods.SIZE(); UXTheme.GetThemePartSize(this.currentHTheme, hdc, this.currentElement.Part, this.currentElement.State, IntPtr.Zero, type, psz); radHdcWrapper.Dispose(); return(new Size(psz.cx, psz.cy)); }
public static void DrawTextOnGlass(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, int rflags) { IntPtr primaryHdc = graphics.GetHdc(); IntPtr memoryHdc = NativeMethods.CreateCompatibleDC(new HandleRef(null, primaryHdc)); NativeMethods.BITMAPINFO info = new NativeMethods.BITMAPINFO(); info.bmiHeader_biSize = Marshal.SizeOf(info); info.bmiHeader_biWidth = bounds.Width; info.bmiHeader_biHeight = -bounds.Height; info.bmiHeader_biPlanes = 1; info.bmiHeader_biBitCount = 32; info.bmiHeader_biCompression = 0; IntPtr ppbBits = IntPtr.Zero; IntPtr dib = NativeMethods.CreateDIBSection(primaryHdc, info, (uint)0, out ppbBits, IntPtr.Zero, (uint)0); NativeMethods.SelectObject(new HandleRef(null, memoryHdc), new HandleRef(null, dib)); IntPtr fontHandle = font.ToHfont(); NativeMethods.SelectObject(new HandleRef(null, memoryHdc), new HandleRef(null, fontHandle)); System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active); DWMAPI.DTTOPTS dttOpts = new DWMAPI.DTTOPTS(); dttOpts.dwSize = Marshal.SizeOf(typeof(DWMAPI.DTTOPTS)); dttOpts.dwFlags = DWMAPI.DTT_COMPOSITED | rflags | DWMAPI.DTT_TEXTCOLOR; dttOpts.crText = ColorTranslator.ToWin32(color); dttOpts.iGlowSize = 10; NativeMethods.RECT textBounds = new NativeMethods.RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top); UXTheme.DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, -1, (int)flags, ref textBounds, ref dttOpts); const int SRCCOPY = 0x00CC0020; NativeMethods.BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY); NativeMethods.DeleteObject(new HandleRef(null, fontHandle)); NativeMethods.DeleteObject(new HandleRef(null, dib)); NativeMethods.DeleteDC(new HandleRef(null, memoryHdc)); graphics.ReleaseHdc(primaryHdc); }
public void PaintCurrentElement(Graphics g, Rectangle bounds) { if (this.currentHTheme == IntPtr.Zero) { return; } RadHdcWrapper radHdcWrapper = new RadHdcWrapper(g, true); IntPtr hdc = radHdcWrapper.GetHdc(); NativeMethods.RECT rect = new NativeMethods.RECT() { left = bounds.Left, top = bounds.Top }; rect.bottom = rect.top + bounds.Height; rect.right = rect.left + bounds.Width; UXTheme.DrawThemeBackground(this.currentHTheme, hdc, this.currentElement.Part, this.currentElement.State, ref rect, IntPtr.Zero); radHdcWrapper.Dispose(); }
private void CloseAllHandles() { try { foreach (KeyValuePair <string, IntPtr> themeHandle in this.themeHandles) { UXTheme.CloseThemeData(themeHandle.Value); } this.themeHandles.Clear(); if (this.vistaExplorerThemeOwner == null) { return; } this.vistaExplorerThemeOwner.Dispose(); this.vistaExplorerThemeOwner = (Control)null; } catch { } }
public static void DrawTextOnGlass( Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, int rflags) { IntPtr hdc = graphics.GetHdc(); IntPtr compatibleDc = NativeMethods.CreateCompatibleDC(new HandleRef((object)null, hdc)); NativeMethods.BITMAPINFO pbmi = new NativeMethods.BITMAPINFO(); pbmi.bmiHeader_biSize = Marshal.SizeOf((object)pbmi); pbmi.bmiHeader_biWidth = bounds.Width; pbmi.bmiHeader_biHeight = -bounds.Height; pbmi.bmiHeader_biPlanes = (short)1; pbmi.bmiHeader_biBitCount = (short)32; pbmi.bmiHeader_biCompression = 0; IntPtr ppvBits = IntPtr.Zero; IntPtr dibSection = NativeMethods.CreateDIBSection(hdc, pbmi, 0U, out ppvBits, IntPtr.Zero, 0U); NativeMethods.SelectObject(new HandleRef((object)null, compatibleDc), new HandleRef((object)null, dibSection)); IntPtr hfont = font.ToHfont(); NativeMethods.SelectObject(new HandleRef((object)null, compatibleDc), new HandleRef((object)null, hfont)); VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); DWMAPI.DTTOPTS pOptions = new DWMAPI.DTTOPTS(); pOptions.dwSize = Marshal.SizeOf(typeof(DWMAPI.DTTOPTS)); pOptions.dwFlags = 8192 | rflags | 1; pOptions.crText = ColorTranslator.ToWin32(color); pOptions.iGlowSize = 10; NativeMethods.RECT pRect = new NativeMethods.RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top); UXTheme.DrawThemeTextEx(visualStyleRenderer.Handle, compatibleDc, 0, 0, text, -1, (int)flags, ref pRect, ref pOptions); NativeMethods.BitBlt(hdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, compatibleDc, 0, 0, 13369376); NativeMethods.DeleteObject(new HandleRef((object)null, hfont)); NativeMethods.DeleteObject(new HandleRef((object)null, dibSection)); NativeMethods.DeleteDC(new HandleRef((object)null, compatibleDc)); graphics.ReleaseHdc(hdc); }
/// <summary> /// closes all currently opened HTheme handles /// </summary> private void CloseAllHandles() { try { //release all htheme handles foreach (KeyValuePair <string, IntPtr> pair in this.themeHandles) { UXTheme.CloseThemeData(pair.Value); } this.themeHandles.Clear(); if (this.vistaExplorerThemeOwner != null) { this.vistaExplorerThemeOwner.Dispose(); this.vistaExplorerThemeOwner = null; } } catch { } }
private IntPtr GetHTheme(string windowClass) { IntPtr num; if (!this.themeHandles.TryGetValue(windowClass, out num)) { if (this.vistaExplorerThemeOwner == null) { this.vistaExplorerThemeOwner = new Control(); this.vistaExplorerThemeOwner.CreateControl(); UXTheme.SetWindowTheme(this.vistaExplorerThemeOwner.Handle, "explorer", (string)null); } IntPtr hwnd = IntPtr.Zero; string lower = windowClass.ToLower(); if (lower == VisualStyleElement.TreeView.Item.Normal.ClassName.ToLower() || lower == VisualStyleElement.ListView.Item.Normal.ClassName.ToLower()) { hwnd = this.vistaExplorerThemeOwner.Handle; } num = UXTheme.OpenThemeData(hwnd, windowClass); this.themeHandles.Add(windowClass, num); } return(num); }