本文介绍了WPF - 从模态窗口中删除系统菜单图标,但不是主应用程序窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试(在WPF中):

I'm trying to do (in WPF):


  1. 有一个.exe文件,显示系统菜单图标(图标在窗口的左上角)正常情况

  2. 此应用程序调用的模态窗口中没有显示此图标

我在这里尝试了解决方案:

I tried the solution here:Removing Icon from a WPF window

这很有效。可以下载相同内容的样本:

And this worked. There's a downloadable sample of the same thing at:http://blogs.msdn.com/b/wpfsdk/archive/2007/08/02/a-wpf-window-without-an-window-icon-the-thing-you-click-to-get-the-system-menu.aspx

但是,如果我将.ico文件添加到.exe的项目属性(属性 - >应用程序 - >图标和清单),它将停止工作。您可以使用可下载的示例尝试此操作。

However, it stops working if I add an .ico file to the .exe's project properties (Properties -> Application -> Icon and Manifest). You can try this with the downloadable sample.

似乎.exe中的图标也用于模态窗口(我们在.dll文件中也是如此)甚至如果.dll的属性显示默认图标。它必须从.exe传递下来。那么,有没有办法在主窗口上显示图标,而不是在子窗口上显示?

It seems that the icon from the .exe is used in the modal windows too (which we have in .dll files) even if the properties of that .dll says "default icon". It must get passed down from the .exe. So, is there a way to show the icon on the main window, but not on a child window?

可能更简单的问一个方法是:是否可能即使在项目的属性中指定了.ico文件,也要删除图标?

Possibly, an easier way of asking this is: Is it possible to remove the icon even though there's an .ico file specifies in the project's properties?

我发现的唯一工作就是设置模态窗口的WindowStye到ToolWindow。这给了我几乎我想要的东西:没有图标和关闭按钮(右上角的x)仍然存在。然而,x是超小的。这是最好的吗?

The only thing I've found to work is to set the WindowStye of the modal window to "ToolWindow". This gives me almost what I want: no icon and the "Close" button ("x" in upper right) is still there. Yet, the x is super small. Is this the best there is?

感谢您的帮助。

推荐答案

WS_EX_DLGMODALFRAME 仅在WPF窗口的本机Win32窗口没有与之关联的图标时删除图标。 WPF(方便地)使用应用程序的图标作为所有窗口的默认图标,而没有明确设置的图标。通常情况下,这不会导致任何问题,并且可以省去在每个窗口上手动设置应用程序图标的麻烦。但是,当我们尝试删除图标时,它会给我们带来问题。

I had this same problem. It appears that WS_EX_DLGMODALFRAME only removes the icon when the WPF window's native Win32 window does not have an icon associated with it. WPF (conveniently) uses the application's icon as the default icon for all windows without an explicitly set icon. Normally, that doesn't cause any problems and saves us the trouble of manually setting the application's icon on each window; however, it causes a problem for us when we try to remove the icon.

由于问题是WPF自动为我们设置了窗口的图标,我们可以发送到Win32窗口重置它的图标我们是应用 WS_EX_DLGMODALFRAME

Since the problem is that WPF automatically sets the window's icon for us, we can send WM_SETICON to the Win32 window to reset its icon when we are applying 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);

编辑:哦,看起来这只适用于app在Visual Studio中运行外部

Oh, and it looks like this works only when the app is run outside of Visual Studio.

这篇关于WPF - 从模态窗口中删除系统菜单图标,但不是主应用程序窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:36
查看更多