C# WPF - 应用程序图标 +在任务栏显示 = False

发布于 2024-08-23 13:34:21 字数 385 浏览 10 评论 0原文

我创建了一个具有以下属性的自定义分层 WPF 窗口:

  1. AllowsTransparency = True
  2. ShowInTaskbar = False
  3. 背景 = 透明
  4. 最上面 = True
  5. Icon = "Icon.ico"

我已在“项目属性”->“应用程序”下添加了 Icon.ico “选项卡。

如果 ShowInTaskBar 为 false,则该图标将显示为默认的 WPF 窗口图标,但如果 ShowInTaskbar 为 true,则该图标将正确显示。

我们希望图标在 Alt+Tab 菜单中正确显示。我们怎样才能实现这一点并让 ShowInTaskbar = False?

I've created a custom layered WPF window with the following properties:

  1. AllowsTransparency = True
  2. ShowInTaskbar = False
  3. Background = Transparent
  4. Topmost = True
  5. Icon = "Icon.ico"

I've added Icon.ico under "Project Properties"->"Application" tab.

The icon displays as the default WPF window icon if ShowInTaskBar is false, but displays correctly if ShowInTaskbar is true.

We want the icon to show up correctly in the Alt+Tab menu. How can we achieve this and have ShowInTaskbar = False?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2024-08-30 13:34:21

这里有几个问题。首先,当 ShowInTaskbar 属性设置为 false 时,将创建一个不可见窗口并将其指定为当前窗口的父窗口。在窗口之间切换时会显示该不可见窗口的图标。

您可以使用 Interop 捕获该窗口并设置其图标,如下所示:

private void Window_Loaded(object sender, RoutedEventArgs e) {
    SetParentIcon();
}

private void SetParentIcon() {
    WindowInteropHelper ih = new WindowInteropHelper(this);
    if(this.Owner == null && ih.Owner != IntPtr.Zero) { //We've found the invisible window
        System.Drawing.Icon icon = new System.Drawing.Icon("ApplicationIcon.ico");
        SendMessage(ih.Owner, 0x80 /*WM_SETICON*/, (IntPtr)1 /*ICON_LARGE*/, icon.Handle); //Change invisible window's icon
    }
}

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

您需要考虑的其他问题是:

  1. 找出 ShowInTaskbar 属性在运行时更改时会发生什么;
  2. 从窗口而不是文件中提取图标;

There are several problems here. First of all, when ShowInTaskbar property is set to false, an invisible window gets created and assigned as a parent of current window. This invisible window's icon is displayed when switching between windows.

You can catch that window with Interop and set it's icon like this:

private void Window_Loaded(object sender, RoutedEventArgs e) {
    SetParentIcon();
}

private void SetParentIcon() {
    WindowInteropHelper ih = new WindowInteropHelper(this);
    if(this.Owner == null && ih.Owner != IntPtr.Zero) { //We've found the invisible window
        System.Drawing.Icon icon = new System.Drawing.Icon("ApplicationIcon.ico");
        SendMessage(ih.Owner, 0x80 /*WM_SETICON*/, (IntPtr)1 /*ICON_LARGE*/, icon.Handle); //Change invisible window's icon
    }
}

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

The other problems for you to think about would be:

  1. Find out what happens when ShowInTaskbar property changes at runtime;
  2. Extract an icon from your window rather than from file;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文