public void DwmEnableBlurBehindWindowTest() { var wnd = new Form { Size = new Size(50, 50), TopMost = true, ControlBox = false, FormBorderStyle = FormBorderStyle.None }; wnd.Show(); Thread.Sleep(1000); var bb = new DWM_BLURBEHIND(true); var err = DwmEnableBlurBehindWindow(wnd.Handle, bb); Assert.That(err.Succeeded); Thread.Sleep(1000); using (var g = wnd.CreateGraphics()) using (var hrgn = Gdi32.CreateRectRgnIndirect(new RECT(0, 0, 20, 20))) { bb.hRgnBlur = hrgn; bb.dwFlags |= DWM_BLURBEHIND_Mask.DWM_BB_BLURREGION; err = DwmEnableBlurBehindWindow(wnd.Handle, bb); Assert.That(err.Succeeded); Thread.Sleep(1000); } var bb2 = new DWM_BLURBEHIND(false); err = DwmEnableBlurBehindWindow(wnd.Handle, bb2); Assert.That(err.Succeeded); Thread.Sleep(1000); wnd.Hide(); }
/// <summary> /// Draw a themed background element. /// </summary> /// <param name="g">Graphics object reference.</param> /// <param name="draw">Rectangle for drawing.</param> /// <param name="exclude">Rectangle to exclude from drawing.</param> /// <param name="part">Theme part.</param> /// <param name="state">Theme state of part.</param> public void DrawThemeBackground(Graphics g, Rectangle draw, Rectangle exclude, int part, int state) { if (IsControlThemed) { // Create a Win32 version of the drawing Rectangle Win32.RECT drawWin32 = new Win32.RECT(); drawWin32.left = draw.X; drawWin32.top = draw.Y; drawWin32.right = draw.Right; drawWin32.bottom = draw.Bottom; // Create a Win32 version of the clipping Rectangle Win32.RECT excludeWin32 = new Win32.RECT(); excludeWin32.left = exclude.X; excludeWin32.top = exclude.Y; excludeWin32.right = exclude.Right; excludeWin32.bottom = exclude.Bottom; // Get access to the underlying HDC IntPtr hDC = g.GetHdc(); // Make a note of the original clipping region IntPtr hOldClipRgn = IntPtr.Zero; Gdi32.GetClipRgn(hDC, ref hOldClipRgn); // Create region that excludes area from drawing rectangle IntPtr hDrawRgn = Gdi32.CreateRectRgnIndirect(ref drawWin32); IntPtr hExcludeRgn = Gdi32.CreateRectRgnIndirect(ref excludeWin32); Gdi32.CombineRgn(hExcludeRgn, hDrawRgn, hExcludeRgn, (int)CombineFlags.RGN_DIFF); // Define required clipping rectangle Gdi32.SelectClipRgn(hDC, hExcludeRgn); // Perform actual drawing work Uxtheme.DrawThemeBackground(_hTheme, hDC, part, state, ref drawWin32, IntPtr.Zero); // Put clipping region back again Gdi32.SelectClipRgn(hDC, hOldClipRgn); // Delete objects no longer needed Gdi32.DeleteObject(hDrawRgn); Gdi32.DeleteObject(hExcludeRgn); // Must release the resource to prevent leaks! g.ReleaseHdc(hDC); } }
protected static IntPtr CreateRectangleRegion(Rectangle rect, int indent) { Win32.RECT newWinRect = new Win32.RECT(); newWinRect.left = rect.Left; newWinRect.top = rect.Top; newWinRect.right = rect.Right; newWinRect.bottom = rect.Bottom; // Create region for whole of the new rectangle IntPtr newOuter = Gdi32.CreateRectRgnIndirect(ref newWinRect); // If the rectangle is to small to make an inner object from, then just use the outer if ((indent <= 0) || (rect.Width <= indent) || (rect.Height <= indent)) { return(newOuter); } newWinRect.left += indent; newWinRect.top += indent; newWinRect.right -= indent; newWinRect.bottom -= indent; // Create region for the unwanted inside of the new rectangle IntPtr newInner = Gdi32.CreateRectRgnIndirect(ref newWinRect); Win32.RECT emptyWinRect = new Win32.RECT(); emptyWinRect.left = 0; emptyWinRect.top = 0; emptyWinRect.right = 0; emptyWinRect.bottom = 0; // Create a destination region IntPtr newRegion = Gdi32.CreateRectRgnIndirect(ref emptyWinRect); // Remove the intersection of the outer and inner Gdi32.CombineRgn(newRegion, newOuter, newInner, (int)Win32.CombineFlags.RGN_XOR); // Remove unwanted intermediate objects Gdi32.DeleteObject(newOuter); Gdi32.DeleteObject(newInner); // Return the resultant region object return(newRegion); }
private void ClipMaximizedRegion(IntPtr hwnd) { RECT winRC = GetMaxClientRect(hwnd); Debug.WriteLine("max win rect=" + winRC + ", visible=" + User32.IsWindowVisible(hwnd)); IntPtr rectRgnIndirect = IntPtr.Zero; try { rectRgnIndirect = Gdi32.CreateRectRgnIndirect(ref winRC); _hasClip = User32.SetWindowRgn(hwnd, rectRgnIndirect, User32.IsWindowVisible(hwnd)); } finally { if (rectRgnIndirect != IntPtr.Zero) { Gdi32.DeleteObject(rectRgnIndirect); } } }