来自WPF应用程序的Windows 7任务栏SetOverlayIcon不起作用

时间:2009-06-21 21:20:56

标签: wpf

我正在尝试使用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方法。

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您的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);
}
相关问题