Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue where a blank window appears in Alt+Tab in Windows #1456

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading