no idea why LWA_COLORKEY is much more performance expensive than DwmExtendFrameIntoClientArea.

So going to use DwmExtendFrameIntoClientArea.
master
Zaafar Ahmed 7 years ago
parent 3b7b31217a
commit d046a5c326
  1. 7
      ClickableTransparentOverlay/Overlay.cs
  2. 42
      ClickableTransparentOverlay/WinApi.cs

@ -40,10 +40,10 @@
_future_size = Vector2.Zero;
_future_pos = Vector2.Zero;
_window = new Sdl2Window("Overlay", x, x, width, height, SDL_WindowFlags.Borderless | SDL_WindowFlags.AlwaysOnTop | SDL_WindowFlags.Resizable | SDL_WindowFlags.SkipTaskbar, true);
_window = new Sdl2Window("Overlay", x, x, width, height, SDL_WindowFlags.Borderless | SDL_WindowFlags.AlwaysOnTop | SDL_WindowFlags.SkipTaskbar, true);
// TODO: Create a new branch for Non-Veldrid dependent version. Ideally, we can directly use SDL2Window.
_gd = VeldridStartup.CreateGraphicsDevice(_window, new GraphicsDeviceOptions(true, null, true), GraphicsBackend.Direct3D11);
WinApi.EnableTransparent(_window.Handle);
WinApi.EnableTransparent(_window.Handle, new System.Drawing.Rectangle(_window.X , _window.Y, _window.Width, _window.Height));
_window.Resized += () =>
{
_gd.MainSwapchain.Resize((uint)_window.Width, (uint)_window.Height);
@ -89,8 +89,9 @@
_future_pos.Y = y;
_future_size.X = width;
_future_size.Y = height;
// TODO: move it to _window.Moved
// TODO: move following two lines to _window.Moved
_hook_controller.UpdateWindowPosition(x, y);
WinApi.EnableTransparent(_window.Handle, new System.Drawing.Rectangle(x, y, width, height));
_require_resize = true;
}

@ -14,13 +14,48 @@
private const int SW_HIDE = 0x00;
private const int SW_SHOW = 0x05;
public static void EnableTransparent(IntPtr handle)
public static void EnableTransparent(IntPtr handle, System.Drawing.Rectangle size)
{
int windowLong = GetWindowLong(handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT;
SetWindowLong(handle, GWL_EXSTYLE, new IntPtr(windowLong));
SetLayeredWindowAttributes(handle, 0, 255, LWA_ALPHA | LWA_COLORKEY);
//SetLayeredWindowAttributes(handle, 0, 255, LWA_ALPHA /*| LWA_COLORKEY*/ );
Margins margins = Margins.FromRectangle(size);
DwmExtendFrameIntoClientArea(handle, ref margins);
}
#region Structures
[StructLayout(LayoutKind.Sequential)]
private struct Margins
{
private int left, right, top, bottom;
public static Margins FromRectangle(System.Drawing.Rectangle rectangle)
{
var margins = new Margins
{
left = rectangle.Left,
right = rectangle.Right,
top = rectangle.Top,
bottom = rectangle.Bottom
};
return margins;
}
}
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
private readonly int left, top, right, bottom;
public System.Drawing.Rectangle ToRectangle(System.Drawing.Point point)
{
return new System.Drawing.Rectangle(point.X, point.Y, right - left, bottom - top);
}
}
#endregion Structures
public static void HideConsoleWindow()
{
var handle = GetConsoleWindow();
@ -33,6 +68,9 @@
ShowWindow(handle, SW_SHOW);
}
[DllImport("dwmapi.dll")]
private static extern IntPtr DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

Loading…
Cancel
Save