【问题标题】:Windows 7 Taskbar SetOverlayIcon from WPF app doesn't workWPF 应用程序中的 Windows 7 任务栏 SetOverlayIcon 不起作用
【发布时间】:2010-11-04 17:13:36
【问题描述】:

我正在尝试使用 Windows 7 任务栏 API 来设置任务栏上应用程序图标的覆盖图标。

我从 Microsoft 下载了示例,但想提取执行此操作的特定代码片段,以避免添加对我不会使用的大部分代码的引用。

我在 Win 7 x64 上的 VS 2010 中有一个简单的 WPF 应用程序。

我的 Window1.xaml 的代码隐藏是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Interop;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private static ITaskbarList3 _taskbarList;
    internal static ITaskbarList3 TaskbarList
    {
        get
        {
            if (_taskbarList == null)
            {
                if (_taskbarList == null)
                {
                    _taskbarList = (ITaskbarList3)new CTaskbarList();
                    _taskbarList.HrInit();
                }
            }
            return _taskbarList;
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TaskbarList.SetOverlayIcon(new WindowInteropHelper(Application.Current.MainWindow).Handle, SystemIcons.Question.Handle, "Test");
    }
}

[ComImportAttribute()]
[GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITaskbarList3
{
    // ITaskbarList
    [PreserveSig]
    void HrInit();
    [PreserveSig]
    void AddTab(IntPtr hwnd);
    [PreserveSig]
    void DeleteTab(IntPtr hwnd);
    [PreserveSig]
    void ActivateTab(IntPtr hwnd);
    [PreserveSig]
    void SetActiveAlt(IntPtr hwnd);

    void SetOverlayIcon(
        IntPtr hwnd,
        IntPtr hIcon,
        [MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
}

[GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComImportAttribute()]
internal class CTaskbarList { }
  }

当我点击按钮时,什么也没有发生。没有错误。图标不变。

我想知道我是否将正确的句柄传递给 SetOverlayIcon 方法。

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您的 ITaskBarList3 接口错误。这是正确的:

    public enum TBPF
    {
        TBPF_NOPROGRESS = 0,
        TBPF_INDETERMINATE = 0x1,
        TBPF_NORMAL = 0x2,
        TBPF_ERROR = 0x4,
        TBPF_PAUSED = 0x8
    }
    
    public enum TBATF
    {
        TBATF_USEMDITHUMBNAIL = 0x1,
        TBATF_USEMDILIVEPREVIEW = 0x2
    }
    
    public enum THB : uint
    {
        THB_BITMAP = 0x1,
        THB_ICON = 0x2,
        THB_TOOLTIP = 0x4,
        THB_FLAGS = 0x8
    }
    
    public enum THBF : uint
    {
        THBF_ENABLED = 0,
        THBF_DISABLED = 0x1,
        THBF_DISMISSONCLICK = 0x2,
        THBF_NOBACKGROUND = 0x4,
        THBF_HIDDEN = 0x8
    }
    
    [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Auto)]
    public struct THUMBBUTTON
    {
        public THB dwMask;
        public uint iId;
        public uint iBitmap;
        public IntPtr hIcon;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = Windows.MAX_PATH)]
        public string szTip;
        public THBF dwFlags;
    }
    
    [ComImport]
    [Guid("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ITaskbarList3
    {
        // ITaskbarList
        void HrInit();
        void AddTab(IntPtr hwnd);
        void DeleteTab(IntPtr hwnd);
        void ActivateTab(IntPtr hwnd);
        void SetActiveAlt(IntPtr hwnd);
    
        // ITaskbarList2
        void MarkFullscreenWindow(
            IntPtr hwnd,
            [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
    
        // ITaskbarList3
        void SetProgressValue(IntPtr hwnd, ulong ullCompleted, ulong ullTotal);
        void SetProgressState(IntPtr hwnd, TBPF tbpFlags);
        void RegisterTab(IntPtr hwndTab, IntPtr hwndMDI);
        void UnregisterTab(IntPtr hwndTab);
        void SetTabOrder(IntPtr hwndTab, IntPtr hwndInsertBefore);
        void SetTabActive(IntPtr hwndTab, IntPtr hwndMDI, TBATF tbatFlags);
    
        void ThumbBarAddButtons(
            IntPtr hwnd,
            uint cButtons,
            [MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
    
        void ThumbBarUpdateButtons(
            IntPtr hwnd,
            uint cButtons,
            [MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
    
        void ThumbBarSetImageList(IntPtr hwnd, IntPtr himl);
    
        void SetOverlayIcon(
            IntPtr hwnd,
            IntPtr hIcon,
            [MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
    
        void SetThumbnailTooltip(
            IntPtr hwnd,
            [MarshalAs(UnmanagedType.LPWStr)] string pszTip);
    
        void SetThumbnailClip(
            IntPtr hwnd,
            [MarshalAs(UnmanagedType.LPStruct)] Rectangle prcClip);
    }
    

    【讨论】:

    • 我不得不做一些修改,但它解决了我的问题。谢谢。
    • 您做了哪些修改?这对我来说很有趣,因为我自己使用这段代码(但我主要将它用于任务栏进度条)。我只能看到一个有问题的地方 - 使用 THUMBBUTTON 的方法。
    猜你喜欢
    • 2010-11-30
    • 2011-03-13
    • 2011-02-12
    • 2014-03-22
    • 2011-08-21
    • 2010-12-22
    • 2013-01-26
    • 2012-05-05
    • 2012-07-05
    相关资源
    最近更新 更多