我知道有很多关于隐藏或删除WPF窗口左上角图标的问题,这是系统菜单所在的位置.我尝试了很多但没有效果.这是我的要求:
可用的答案基本上使用Windows API函数GetWindowLong,SetWindowLong有时还SetWindowPos添加扩展窗口样式WS_EX_DLGMODALFRAME和调用SWP_FRAMECHANGED.有时,其他样式也会设置或取消设置.
不幸的是,这根本不起作用.我可以没有没有关闭按钮的图标,或者两者都在那里.但同样值得注意的是,所有这些内容都来自2010年或预告片.它似乎是针对早期的.NET或Windows版本而失败了.
我已经将系统对话框(来自资源管理器)和我的WPF窗口的窗口样式与Microsoft Spy ++(包含在Visual Studio中)进行了比较.但我可以尝试将所有标志设置为相同,图标不会消失.就像黑魔法一样,它会推翻所有其他API函数或物理.
有没有人能够在今天和指定环境中使用仍然有效的解决方案?
She*_*dan 25
如果您刚刚将标题中的单词放入搜索引擎而不是像我刚才那样,那么您会发现比这些更多的结果.你可以在下面找到你的答案:
你对此的最新评论不适用于大规模的应用程序让我感到惊讶.因此,我随后将代码添加到大型应用程序中,并再一次正常工作.但是,我继续测试这一点,你必须使用一个RibbonWindow在你的应用程序,因为当我测试了一个大规模应用该代码RibbonWindow的代码并没有工作.
如果您正在使用法线,请Window尝试使用此代码(来自@ MichalCiechan对第一个链接帖子的回答):
首先添加这个类:
public static class IconHelper
{
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x,
int y, int width, int height, uint flags);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr
lParam);
const int GWL_EXSTYLE = -20;
const int WS_EX_DLGMODALFRAME = 0x0001;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOZORDER = 0x0004;
const int SWP_FRAMECHANGED = 0x0020;
const uint WM_SETICON = 0x0080;
public static void RemoveIcon(Window window)
{
// Get this window's handle
IntPtr hwnd = new WindowInteropHelper(window).Handle;
// Change the extended window style to not show a window icon
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
// Update the window's non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
Run Code Online (Sandbox Code Playgroud)
然后将其添加到MainWindow.xaml.cs:
protected override void OnSourceInitialized(EventArgs e)
{
IconHelper.RemoveIcon(this);
}
Run Code Online (Sandbox Code Playgroud)
哦......另外还有一件事需要注意......如果你设置了Window.Icon属性,它就行不通了,但是如果你不想出现一个图标,我猜你还没有这样做.
小智 12
从具有图标的WPF应用程序创建对话框窗口时,上述操作无效.但是,添加以下两行时,图标会从对话框窗口中正确消失:
SendMessage(hwnd, WM_SETICON, new IntPtr(1), IntPtr.Zero);
SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)