以编程方式添加DeskBand时出错

时间:2014-02-10 14:46:45

标签: c# .net user-interface windows-8 deskband

我正在尝试使用以下代码以编程方式将DeskBand对象添加到系统托盘:

[ComImport, Guid("6D67E846-5B9C-4db8-9CBC-DDE12F4254F1"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITrayDeskband
{
    [PreserveSig]
    int ShowDeskBand([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid);
    [PreserveSig]
    int HideDeskBand([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid);
    [PreserveSig]
    int IsDeskBandShown([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid);
    [PreserveSig]
    int DeskBandRegistrationChanged();
} 

private void ShowDeskBand()
{
    ITrayDeskband obj = null;
    Type trayDeskbandType = System.Type.GetTypeFromCLSID(new Guid("E6442437-6C68-4f52-94DD-2CFED267EFB9"));
    try
    {
        Guid deskbandGuid = new Guid("FE0829F8-EDFA-46B6-87E0-636C8D953E33"); // My Deskband | TestToolbar

        obj = (ITrayDeskband)Activator.CreateInstance(trayDeskbandType);
        obj.DeskBandRegistrationChanged();

        int hr = obj.ShowDeskBand(ref deskbandGuid);

        if (hr != 0)
        {
            throw new Exception("Error while trying to show deskband: " + hr);
        }

        obj.DeskBandRegistrationChanged();
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    finally
    {
        if (obj != null && Marshal.IsComObject(obj))
            Marshal.ReleaseComObject(obj);
    }
}

来源:http://www.pinvoke.net/default.aspx/Interfaces/ITrayDeskband.html

我的DeskBand对象是从此示例的“BandObjectsLib”创建的:http://www.codeproject.com/Articles/14141/Band-Objects-NET-2-0-Redux

看起来像这样:

[Guid("FE0829F8-EDFA-46B6-87E0-636C8D953E33")]
[BandObject("TestToolbar", BandObjectStyle.Horizontal | BandObjectStyle.TaskbarToolBar, HelpText = "Testing this toolbar")]
public class TestToolbar : BandObject
{
    public TestToolbar()
    {
        ProgressBar pb = new ProgressBar();
        pb.Maximum = 100;
        pb.Minimum = 0;
        pb.Value = 50;

        this.Controls.AddRange(new System.Windows.Forms.Control[] { pb });
    }
}

然而,当我尝试添加DeskBand对象时,我得到的是我的异常,告诉我错误代码: -2147467259 此错误代码转换为0x80004005,并根据MSDN即E_FAIL:http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137.aspx

我基本上都被困在这里,因为这不是我的专业领域。我希望有人可能更有经验,可以帮助我吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

您必须注册DLL(安装MSI或使用regasm)才能使代码正常工作。