Prevent window from stealing focus

This commit is contained in:
TheXamlGuy
2024-01-16 08:31:03 +00:00
parent 99855e77b9
commit 86603a6567
5 changed files with 67 additions and 17 deletions
@@ -0,0 +1,33 @@
namespace Hyperbar.Windows.Interop;
[Flags]
public enum ExtendedWindowStyle
{
AcceptFiles = 0x10,
AppWindow = 0x40000,
ClientEdge = 0x200,
Composited = 0x2000000,
ContextHelp = 0x400,
ControlParent = 0x10000,
DlgModalFrame = 1,
Layered = 0x80000,
LayoutRtl = 0x400000,
Left = 0,
LeftScrollBar = 0x4000,
LtrReading = 0,
MdiChild = 0x40,
NoActivate = 0x8000000,
NoInheritLayout = 0x100000,
NoParentNotify = 4,
NoRedirectionBitmap = 0x200000,
OverlappedWindow = 0x300,
PaletteWindow = 0x188,
Right = 0x1000,
RightScrollBar = 0,
RtlReading = 0x2000,
StaticEdge = 0x20000,
ToolWindow = 0x80,
TopMost = 8,
Transparent = 0x20,
WindowEdge = 0x100
}
+16 -2
View File
@@ -15,8 +15,23 @@ public static class HwndExtensions
WS_EX_LAYERED = 0x80000
}
public static void SetExtendedWindowStyle(this IntPtr hwnd, ExtendedWindowStyle newStyle)
{
int windowLong = PInvoke.GetWindowLong(new(hwnd), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
if (PInvoke.SetWindowLong(new(hwnd), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newStyle) != windowLong)
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
PInvoke.SetWindowPos(new(hwnd), new HWND(0), 0, 0, 0, 0, SET_WINDOW_POS_FLAGS.SWP_DRAWFRAME |
SET_WINDOW_POS_FLAGS.SWP_NOMOVE |
SET_WINDOW_POS_FLAGS.SWP_NOOWNERZORDER |
SET_WINDOW_POS_FLAGS.SWP_NOSIZE |
SET_WINDOW_POS_FLAGS.SWP_NOZORDER);
}
public static void SetWindowOpacity(this IntPtr hWnd,
byte value)
byte value)
{
HWND hWND = new(hWnd);
WindowStyles windowLong = (WindowStyles)PInvoke.GetWindowLong(hWND, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
@@ -30,7 +45,6 @@ public static class HwndExtensions
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
public static void SetWindowStyle(this IntPtr hwnd,
WindowStyle newStyle)
{
+1 -1
View File
@@ -30,4 +30,4 @@ public enum WindowStyle
TiledWindow = 0xCF0000,
Visible = 0x10000000,
VScroll = 0x200000
}
}