Sheas-Cealer/Utilities/IconRemover.cs
2024-04-17 18:10:43 +08:00

55 lines
2.2 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace Sheas_Cealer
{
//定义IconRemover
internal static partial class IconRemover
{
private const int GWL_EXSTYLE = -20;
private const int WS_EX_DLGMODALFRAME = 0x0001;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_FRAMECHANGED = 0x0020;
private const uint WM_SETICON = 0x0080;
[LibraryImport("user32.dll", EntryPoint = "GetWindowLongW")]
private static partial int GetWindowLong(IntPtr hwnd, int index);
[LibraryImport("user32.dll", EntryPoint = "SetWindowLongW")]
private static partial int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);
[LibraryImport("user32.dll", EntryPoint = "SendMessageW")]
private static partial IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
internal static void RemoveIcon(Window window)
{
//获取该窗口句柄
IntPtr hwnd = new WindowInteropHelper(window).Handle;
//将窗口更改为不显示窗口图标
_ = SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_DLGMODALFRAME);
//更新窗口的非客户区域来显示更改
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
//防止自定义图标生效
SendMessage(hwnd, WM_SETICON, new IntPtr(1), IntPtr.Zero);
SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero);
}
}
//使用IconRemover
public partial class MainWindow
{
protected override void OnSourceInitialized(EventArgs e) => IconRemover.RemoveIcon(this);
}
public partial class AboutWindow
{
protected override void OnSourceInitialized(EventArgs e) => IconRemover.RemoveIcon(this);
}
}