private IntPtr GetRegion() { IntPtr hShadowReg = IntPtr.Zero; IntPtr hOwnerReg = IntPtr.Zero; IntPtr hRegion = IntPtr.Zero; try { var rect = new RECT(); User32.GetWindowRect(_handle, ref rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; hShadowReg = Gdi32.CreateRectRgn(0, 0, width, height); hOwnerReg = Gdi32.CreateRectRgn(GetRegionRect()); hRegion = CombineRgn(hShadowReg, hOwnerReg, 4); } finally { if (hShadowReg != IntPtr.Zero) { Gdi32.DeleteObject(hShadowReg); } if (hOwnerReg != IntPtr.Zero) { Gdi32.DeleteObject(hOwnerReg); } } return(hRegion); }
private void ExcludeRegion() { if (!ShouldExlcudeRegion) { return; } IntPtr hRegion = IntPtr.Zero; try { hRegion = GetRegion(); if (hRegion != IntPtr.Zero) { Gdi32.SetWindowRgn(Handle, hRegion, false); } if (Region != IntPtr.Zero) { Gdi32.DeleteObject(Region); } Region = hRegion; } finally { if (hRegion != IntPtr.Zero) { Gdi32.DeleteObject(hRegion); } } }
internal void Close() { if (Region != IntPtr.Zero) { Gdi32.SetWindowRgn(Handle, IntPtr.Zero, false); Gdi32.DeleteObject(Region); } User32.CloseWindow(_handle); User32.SetParent((int)_handle, 0); User32.DestroyWindow(_handle); }
private void DrawToLayeredWindow() { RECT rect = new RECT(); User32.GetWindowRect(_handle, ref rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; if (width == 0 || height == 0) { return; } POINT newLocation = new POINT(rect.left, rect.top); SIZE newSize = new SIZE(width, height); IntPtr screenDc = User32.GetDC(IntPtr.Zero); IntPtr memDc = Gdi32.CreateCompatibleDC(screenDc); using (Bitmap bmp = GetBitmap(width, height)) { IntPtr hBitmap = bmp.GetHbitmap(_transparent); IntPtr hOldBitmap = Gdi32.SelectObject(memDc, hBitmap); User32.UpdateLayeredWindow(_handle, screenDc, ref newLocation, ref newSize, memDc, ref _ptZero, 0, ref _blend, 0x02); User32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Gdi32.SelectObject(memDc, hOldBitmap); Gdi32.DeleteObject(hBitmap); } } Gdi32.DeleteDC(memDc); GC.Collect(); }