2023-05-10 09:33:56 +12:00
|
|
|
|
// <copyright file="Window.cs" company="PlaceholderCompany">
|
2022-10-23 11:02:31 +13:00
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
//
|
2023-05-10 09:33:56 +12:00
|
|
|
|
// Copyright (c) 2022-2023 Peter Kirmeier
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.DllImports
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// wraps the methodcalls to native windows dll's.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static partial class NativeMethods
|
|
|
|
|
{
|
|
|
|
|
private const int WS_EX_TOOLWINDOW = 0x00000080;
|
|
|
|
|
private const int GWL_EXSTYLE = -20;
|
|
|
|
|
|
|
|
|
|
internal static void HideFromAltTab(Window window)
|
|
|
|
|
{
|
|
|
|
|
WindowInteropHelper wndHelper = new WindowInteropHelper(window);
|
|
|
|
|
|
2022-11-13 07:31:47 +13:00
|
|
|
|
if (Environment.Is64BitProcess)
|
|
|
|
|
{
|
|
|
|
|
long exStyle = (long)GetWindowLongPtr(wndHelper.Handle, GWL_EXSTYLE);
|
|
|
|
|
exStyle |= WS_EX_TOOLWINDOW; // do not show when user presses alt + tab
|
|
|
|
|
SetWindowLongPtr(wndHelper.Handle, GWL_EXSTYLE, (IntPtr)exStyle);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int exStyle = (int)GetWindowLong(wndHelper.Handle, GWL_EXSTYLE);
|
|
|
|
|
exStyle |= WS_EX_TOOLWINDOW; // do not show when user presses alt + tab
|
|
|
|
|
SetWindowLong(wndHelper.Handle, GWL_EXSTYLE, (IntPtr)exStyle);
|
|
|
|
|
}
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 09:33:56 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindoww .
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="lpClassName">The class name or a class atom.</param>
|
|
|
|
|
/// <param name="lpWindowName">The window name.</param>
|
|
|
|
|
/// <returns>Handle to the window or NULL on failure.</returns>
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
|
|
|
|
|
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
|
|
|
|
|
internal static extern IntPtr User32FindWindow(string? lpClassName, string? lpWindowName);
|
|
|
|
|
|
2022-11-13 07:31:47 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongw .
|
|
|
|
|
/// </summary>
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
|
2023-05-10 09:33:56 +12:00
|
|
|
|
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
|
2022-11-13 07:31:47 +13:00
|
|
|
|
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptrw .
|
|
|
|
|
/// </summary>
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
2022-11-13 07:31:47 +13:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
|
2023-05-10 09:33:56 +12:00
|
|
|
|
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
|
2022-10-23 11:02:31 +13:00
|
|
|
|
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
|
|
|
|
|
|
2022-11-13 07:31:47 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongw .
|
|
|
|
|
/// </summary>
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
|
2023-05-10 09:33:56 +12:00
|
|
|
|
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
|
2022-11-13 07:31:47 +13:00
|
|
|
|
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptrw .
|
|
|
|
|
/// </summary>
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
2022-11-13 07:31:47 +13:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
|
2023-05-10 09:33:56 +12:00
|
|
|
|
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
|
2022-10-23 11:02:31 +13:00
|
|
|
|
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
|
|
|
|
|
}
|
|
|
|
|
}
|