Prevent window from stealing focus
This commit is contained in:
@@ -4,7 +4,7 @@ using Hyperbar.Windows.UI;
|
|||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
using WinUIEx;
|
using WinUIEx;
|
||||||
using WindowStyle = Hyperbar.Windows.Interop.WindowStyle;
|
using WindowStyle = Hyperbar.Windows.Interop.WindowStyle;
|
||||||
using System.Diagnostics;
|
using ExtendedWindowStyle = Hyperbar.Windows.Interop.ExtendedWindowStyle;
|
||||||
|
|
||||||
namespace Hyperbar.Windows.Controls;
|
namespace Hyperbar.Windows.Controls;
|
||||||
|
|
||||||
@@ -16,19 +16,25 @@ internal class DesktopFlyoutHost : Window
|
|||||||
|
|
||||||
public DesktopFlyoutHost(DesktopFlyoutPresenter presenter)
|
public DesktopFlyoutHost(DesktopFlyoutPresenter presenter)
|
||||||
{
|
{
|
||||||
|
SystemBackdrop = new TransparentTintBackdrop();
|
||||||
|
|
||||||
|
this.SetOpacity(0);
|
||||||
|
this.Snap(WindowPlacement.Top, 0, 0);
|
||||||
|
|
||||||
|
this.SetStyle(WindowStyle.SysMenu | WindowStyle.Visible);
|
||||||
|
this.SetStyle(ExtendedWindowStyle.NoActivate);
|
||||||
|
|
||||||
|
this.SetTopMost(true);
|
||||||
|
this.SetIsAvailableInSwitchers(false);
|
||||||
|
|
||||||
Border root = new();
|
Border root = new();
|
||||||
root.Loaded += OnLoaded;
|
root.Loaded += OnLoaded;
|
||||||
Content = root;
|
Content = root;
|
||||||
|
|
||||||
this.presenter = presenter;
|
this.presenter = presenter;
|
||||||
|
|
||||||
this.SetOpacity(0);
|
|
||||||
this.SetStyle(WindowStyle.SysMenu | WindowStyle.Visible);
|
|
||||||
this.SetTopMost(true);
|
|
||||||
this.SetIsShownInSwitchers2(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async void UpdatePlacement(DesktopFlyoutPlacement placement)
|
internal void UpdatePlacement(DesktopFlyoutPlacement placement)
|
||||||
{
|
{
|
||||||
this.placement = placement;
|
this.placement = placement;
|
||||||
|
|
||||||
@@ -43,9 +49,6 @@ internal class DesktopFlyoutHost : Window
|
|||||||
double height = presenter.DesiredSize.Height;
|
double height = presenter.DesiredSize.Height;
|
||||||
double width = presenter.DesiredSize.Width;
|
double width = presenter.DesiredSize.Width;
|
||||||
|
|
||||||
Debug.WriteLine(height);
|
|
||||||
Debug.WriteLine(width);
|
|
||||||
|
|
||||||
switch (placement)
|
switch (placement)
|
||||||
{
|
{
|
||||||
case DesktopFlyoutPlacement.Left:
|
case DesktopFlyoutPlacement.Left:
|
||||||
@@ -74,8 +77,6 @@ internal class DesktopFlyoutHost : Window
|
|||||||
presenter.TemplateSettings.SetValue(DesktopFlyoutPresenterTemplateSettings.NegativeHeightProperty, -height);
|
presenter.TemplateSettings.SetValue(DesktopFlyoutPresenterTemplateSettings.NegativeHeightProperty, -height);
|
||||||
presenter.TemplateSettings.SetValue(DesktopFlyoutPresenterTemplateSettings.NegativeWidthProperty, -width);
|
presenter.TemplateSettings.SetValue(DesktopFlyoutPresenterTemplateSettings.NegativeWidthProperty, -width);
|
||||||
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(4));
|
|
||||||
|
|
||||||
presenter.UpdatePlacementState(placement);
|
presenter.UpdatePlacementState(placement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +86,6 @@ internal class DesktopFlyoutHost : Window
|
|||||||
private void OnLoaded(object sender,
|
private void OnLoaded(object sender,
|
||||||
RoutedEventArgs args)
|
RoutedEventArgs args)
|
||||||
{
|
{
|
||||||
SystemBackdrop = new TransparentTintBackdrop();
|
|
||||||
this.SetOpacity(255);
|
this.SetOpacity(255);
|
||||||
|
|
||||||
if (Content is Border border)
|
if (Content is Border border)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -15,8 +15,23 @@ public static class HwndExtensions
|
|||||||
WS_EX_LAYERED = 0x80000
|
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,
|
public static void SetWindowOpacity(this IntPtr hWnd,
|
||||||
byte value)
|
byte value)
|
||||||
{
|
{
|
||||||
HWND hWND = new(hWnd);
|
HWND hWND = new(hWnd);
|
||||||
WindowStyles windowLong = (WindowStyles)PInvoke.GetWindowLong(hWND, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
|
WindowStyles windowLong = (WindowStyles)PInvoke.GetWindowLong(hWND, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
|
||||||
@@ -30,7 +45,6 @@ public static class HwndExtensions
|
|||||||
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
|
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetWindowStyle(this IntPtr hwnd,
|
public static void SetWindowStyle(this IntPtr hwnd,
|
||||||
WindowStyle newStyle)
|
WindowStyle newStyle)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,4 +30,4 @@ public enum WindowStyle
|
|||||||
TiledWindow = 0xCF0000,
|
TiledWindow = 0xCF0000,
|
||||||
Visible = 0x10000000,
|
Visible = 0x10000000,
|
||||||
VScroll = 0x200000
|
VScroll = 0x200000
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public static class WindowExtensions
|
|||||||
public static IntPtr GetHandle(this Window window) =>
|
public static IntPtr GetHandle(this Window window) =>
|
||||||
window is not null ? WindowNative.GetWindowHandle(window) : default;
|
window is not null ? WindowNative.GetWindowHandle(window) : default;
|
||||||
|
|
||||||
public static void SetIsShownInSwitchers2(this Window window,
|
public static void SetIsAvailableInSwitchers(this Window window,
|
||||||
bool value) => window.AppWindow.IsShownInSwitchers = value;
|
bool value) => window.AppWindow.IsShownInSwitchers = value;
|
||||||
|
|
||||||
public static void SetOpacity(this Window window,
|
public static void SetOpacity(this Window window,
|
||||||
@@ -19,6 +19,9 @@ public static class WindowExtensions
|
|||||||
public static void SetStyle(this Window window,
|
public static void SetStyle(this Window window,
|
||||||
WindowStyle style) => window.GetHandle().SetWindowStyle(style);
|
WindowStyle style) => window.GetHandle().SetWindowStyle(style);
|
||||||
|
|
||||||
|
public static void SetStyle(this Window window,
|
||||||
|
ExtendedWindowStyle style) => window.GetHandle().SetExtendedWindowStyle(style);
|
||||||
|
|
||||||
public static void SetTopMost(this Window window,
|
public static void SetTopMost(this Window window,
|
||||||
bool value)
|
bool value)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user