简体   繁体   English

在Visual Studio外部运行时从WPF窗口中删除图标

[英]Removing the icon from a WPF window when running outside of Visual Studio

I've used the code in Removing Icon from a WPF window to remove the icon from the window of an application (using the attached property answer) and this has worked a treat, when run via Visual Studio 2010 . 我已经使用了从WPF窗口删除图标中的代码来从应用程序的窗口中删除图标(使用附加的属性答案),这在通过Visual Studio 2010运行时起了作用 When the application is run normally, the icon still appears. 当应用程序正常运行时,仍会出现图标。

The window has no icon assigned to its Icon property, the application does, however, have an icon defined in its properties (Application > Resources > Icon) which is the one that's being shown as the window icon. 该窗口没有为其Icon属性分配Icon ,但应用程序确实在其属性(应用程序>资源>图标)中定义了一个图标,该图标显示为窗口图标。

How can I resolve this difference in behaviour so that the icon isn't shown when the application runs outside of Visual Studio 2010? 如何解决这种行为差异,以便在Visual Studio 2010外部运行应用程序时不显示图标?

I did a bit of digging; 我做了一些挖掘; there's a StackOverflow question that addresses your issue. 有一个StackOverflow问题可以解决您的问题。 Ironically, this fix only works outside of Visual Studio. 具有讽刺意味的是,此修复程序仅适用于Visual Studio。

Relevant Parts of the Answer (by Zach Johnson ): 答案的相关部分( Zach Johnson ):

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窗口的本机Win32窗口没有与之关联的图标时, WS_EX_DLGMODALFRAME似乎只删除图标。 WPF (conveniently) uses the application's icon as the default icon for all windows without an explicitly set icon. WPF(方便地)使用应用程序的图标作为所有窗口的默认图标,没有明确设置的图标。 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. 但是,当我们尝试删除图标时,它会给我们带来问题。

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 . 既然问题是,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);

Code to remove the icon: 用于删除图标的代码:

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);

It works only when the app is run outside of Visual Studio. 它仅在应用程序 Visual Studio 外部运行时才有效。

Maybe the Shell integration library is an option for you? 也许Shell集成库是您的选择? It contains this WindowChrome class to customize the non-client area, allowing you to leave out the icon. 它包含此WindowChrome类以自定义非客户区域,允许您省略图标。

也许这会帮助你隐藏窗口按钮 ,不仅是窗口的图标,而且如果你喜欢(最小化,恢复和关闭)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从外部运行Visual Studio Test项目 - Running Visual Studio Test project from outside 从Visual Studio启动时,WPF应用程序加载初始窗口非常慢 - WPF Application very slow to load initial window when launched from Visual Studio 当从Visual Studio(而不是ClickOnce发布版本)运行C#WPF应用程序时,PowerPoint文件打开 - PowerPoint file opens when C# WPF app is running from Visual Studio, but not from ClickOnce published version 从 Visual Studio 和直接从 exe 运行应用程序时,激活窗口行为是不同的 - Activating a window behavior is different when running the app from Visual Studio and directly from the exe 从 WebClient 继承时 Visual Studio 类图标更改 - Visual Studio Class Icon Changes When Inheriting from WebClient 在 WPF 中打开窗口时的新任务栏图标 - New taskbar icon when opening a window in WPF WPF:Visual Studio图标库中缺少最大化/还原图标。 工作环境是什么? - WPF : Maximize /Restore icons are missing from the Visual Studio Icon gallery. What are the work arounds? 在wpf中运行动画时,usercontrol从Window推出 - usercontrol push out from Window when animation running in wpf Visual Studio 2015未运行WPF或UWP应用 - Visual Studio 2015 not running WPF or UWP apps 在WPF中从dll设置窗口图标 - Set Window Icon from a dll in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM