我进行了一些调查; 有一个
StackOverflow问题解决了您的问题。具有讽刺意味的是,这个修复只在Visual Studio之外起作用。
答案的相关部分(由
Zach Johnson提供):
似乎
WS_EX_DLGMODALFRAME
只有在WPF窗口的本机Win32窗口没有与其关联的图标时才会删除该图标。 WPF(方便地)使用应用程序的图标作为所有未明确设置图标的窗口的默认图标。 通常,这不会造成任何问题,并节省我们手动在每个窗口上设置应用程序的图标的麻烦; 然而,当我们尝试删除图标时,它会给我们带来问题。
由于问题在于WPF自动为我们设置窗口的图标,因此我们可以发送
WM_SETICON到Win32窗口,以在应用
WS_EX_DLGMODALFRAME
时重置其图标。
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之外运行时才起作用。