3

我正在尝试使用以下代码以编程方式将 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/库/windows/desktop/aa378137.aspx

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

4

1 回答 1

0

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

于 2016-11-26T23:06:21.490 回答