我做了一点挖掘;有一个StackOverflow question 可以解决您的问题。具有讽刺意味的是,此修复仅适用于 Visual Studio 之外。
答案的相关部分(Zach Johnson):
似乎WS_EX_DLGMODALFRAME 仅在 WPF 窗口的本机 Win32 窗口没有与之关联的图标时才删除该图标。 WPF(方便地)使用应用程序的图标作为所有窗口的默认图标,而无需明确设置图标。通常,这不会造成任何问题,并且省去了我们在每个窗口上手动设置应用程序图标的麻烦;但是,当我们尝试删除图标时,它会给我们带来问题。
由于问题是WPF会自动为我们设置窗口的图标,所以我们可以在应用WS_EX_DLGMODALFRAME时发送WM_SETICON到Win32窗口重置其图标。
const int WM_SETICON = 0x0080;
const int ICON_SMALL = 0;
const int ICON_BIG = 1;
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SendMessage(
IntPtr hWnd,
int msg,
IntPtr wParam,
IntPtr lParam);
删除图标的代码:
IntPtr hWnd = new WindowInteropHelper(window).Handle;
int currentStyle = NativeMethods.GetWindowLongPtr(hWnd, GWL_EXSTYLE);
SetWindowLongPtr(
hWnd,
GWL_EXSTYLE,
currentStyle | WS_EX_DLGMODALFRAME);
// reset the icon, both calls important
SendMessage(hWnd, WM_SETICON, (IntPtr)ICON_SMALL, IntPtr.Zero);
SendMessage(hWnd, WM_SETICON, (IntPtr)ICON_BIG, IntPtr.Zero);
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
仅当应用程序在 Visual Studio 之外运行时才有效。