Skip to content

Commit

Permalink
Fixed issue where a blank window appears in Alt+Tab in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
veler committed Nov 17, 2024
1 parent 0cb94b7 commit 0caee9c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Windows;
using System.Windows.Interop;
using DevToys.Windows.Native;
using Windows.Win32.Foundation;

namespace DevToys.Windows.Controls;

Expand All @@ -18,6 +20,9 @@ protected override void OnSourceInitialized(EventArgs e)
var hwndSource = HwndSource.FromHwnd(windowHandle);
hwndSource.AddHook(new HwndSourceHook(WndProc));

// Hide window from Alt+Tab.
NativeMethods.HideFromAltTab(new HWND(windowHandle));

base.OnSourceInitialized(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
internal static class HwndSourceMessages
{
internal const int WS_CHILD = 0x40000000;
internal const int WS_EX_TOOLWINDOW = 0x00000080;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,52 @@ namespace DevToys.Windows.Native;

internal static partial class NativeMethods
{
#pragma warning disable CA1416 // Validate platform compatibility
internal static unsafe HRESULT SetWindowAttribute(HWND windowHandle, DWMWINDOWATTRIBUTE attribute, ref int parameter)
{
#pragma warning disable CA1416 // Validate platform compatibility
fixed (void* value = &parameter)
{
return PInvoke.DwmSetWindowAttribute(windowHandle, attribute, value, (uint)Marshal.SizeOf<int>());
}
#pragma warning restore CA1416 // Validate platform compatibility
}

internal static int ExtendFrame(HWND windowHandle, MARGINS margins)
{
#pragma warning disable CA1416 // Validate platform compatibility
return PInvoke.DwmExtendFrameIntoClientArea(windowHandle, in margins);
#pragma warning restore CA1416 // Validate platform compatibility
}

internal static void HideAllWindowButton(HWND windowHandle)
{
#pragma warning disable CA1416 // Validate platform compatibility
_ = PInvoke.SetWindowLong(windowHandle, WINDOW_LONG_PTR_INDEX.GWL_STYLE, PInvoke.GetWindowLong(windowHandle, WINDOW_LONG_PTR_INDEX.GWL_STYLE) & ~HwndButtons.WS_SYSMENU);
#pragma warning restore CA1416 // Validate platform compatibility
}

internal static void SetWindowAsChildOf(HWND windowHandleChild, HWND windowHandleParent)
{
#pragma warning disable CA1416 // Validate platform compatibility
PInvoke.SetWindowLong(windowHandleChild, WINDOW_LONG_PTR_INDEX.GWL_STYLE, HwndSourceMessages.WS_CHILD);
PInvoke.SetParent(windowHandleChild, windowHandleParent);
#pragma warning restore CA1416 // Validate platform compatibility
}

internal static void DisableMinimizeAndMaximizeCapabilities(HWND windowHandle)
{
#pragma warning disable CA1416 // Validate platform compatibility
HMENU systemMenuHandle = PInvoke.GetSystemMenu(windowHandle, false);
PInvoke.EnableMenuItem(systemMenuHandle, (uint)PInvoke.SC_MAXIMIZE, MENU_ITEM_FLAGS.MF_BYCOMMAND | MENU_ITEM_FLAGS.MF_GRAYED);
PInvoke.EnableMenuItem(systemMenuHandle, (uint)PInvoke.SC_MINIMIZE, MENU_ITEM_FLAGS.MF_BYCOMMAND | MENU_ITEM_FLAGS.MF_GRAYED);
#pragma warning restore CA1416 // Validate platform compatibility
}

internal static void EnableMinimizeAndMaximizeCapabilities(HWND windowHandle)
{
#pragma warning disable CA1416 // Validate platform compatibility
HMENU systemMenuHandle = PInvoke.GetSystemMenu(windowHandle, false);
PInvoke.EnableMenuItem(systemMenuHandle, (uint)PInvoke.SC_MAXIMIZE, MENU_ITEM_FLAGS.MF_BYCOMMAND | MENU_ITEM_FLAGS.MF_ENABLED);
PInvoke.EnableMenuItem(systemMenuHandle, (uint)PInvoke.SC_MINIMIZE, MENU_ITEM_FLAGS.MF_BYCOMMAND | MENU_ITEM_FLAGS.MF_ENABLED);
#pragma warning restore CA1416 // Validate platform compatibility
}

internal static void HideFromAltTab(HWND windowHandle)
{
int currentStyle = PInvoke.GetWindowLong(windowHandle, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
PInvoke.SetWindowLong(windowHandle, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, currentStyle | HwndSourceMessages.WS_EX_TOOLWINDOW);
}

#pragma warning restore CA1416 // Validate platform compatibility
/// <summary>
/// Sets various information regarding DWM window attributes
/// </summary>
Expand Down

0 comments on commit 0caee9c

Please sign in to comment.