简体   繁体   English

Windows窗体中的主机IDeskBand

[英]Host IDeskBand in a Windows Form

I'm trying to display the Address toolbar from the Windows Taskbar in my own WinForm. 我正在尝试从我自己的WinForm中的Windows任务栏显示地址工具栏。 I can get the CLSID of the Address toobar ( {01E04581-4EEE-11d0-BFE9-00AA005B4383} ), and I can get an IDeskBand reference to it. 我可以得到地址{01E04581-4EEE-11d0-BFE9-00AA005B4383}的CLSID( {01E04581-4EEE-11d0-BFE9-00AA005B4383} ),我可以得到一个IDeskBand引用。 But... then what? 但是......那又怎样?

Guid bandCLSID = new Guid("{01E04581-4EEE-11d0-BFE9-00AA005B4383}");
Type bandType = Type.GetTypeFromCLSID(bandCLSID);
IDeskBand deskband = (IDeskBand)Activator.CreateInstance(bandType);

I've tried hosting it in an AxHost , but the Address toolbar is not an ActiveX control. 我试过在AxHost托管它,但地址工具栏不是ActiveX控件。 I've tried calling 我试过打电话

(deskband as IOleObjectWithSite).SetSite(various interfaces);

or 要么

(deskband as IDockingWindow).ShowDW(true);

as well as various other interfaces and their methods, but nothing I do seems to get me anywhere. 以及各种其他接口和他们的方法,但我做的任何事情似乎都没有把我带到任何地方。 I'd be overjoyed if I could actually see that toolbar appear anywhere. 如果我真的能看到工具栏出现在任何地方,我会高兴的。 But I can't seem to bridge the gap between having the IDeskBand reference and plugging it into my Windows Form. 但我似乎无法弥合IDeskBand引用和将其插入我的Windows窗体之间的差距。

Has anybody attempted this before, and gotten further than I have? 有没有人试过这个,并且比我更进一步?

I don't think this is supported, as a DeskBand is supposed to be hosted by Explorer, but here is a sample Form code that demonstrates how to do it and should help to get you started. 我不认为这是支持的,因为DeskBand应该由资源管理器托管,但这里有一个示例表单代码,演示如何执行它并应该有助于您入门。

The idea is you need to be the "Site", instead of Explorer. 想法是你需要成为“网站”,而不是资源管理器。 If you look at the documentation here Creating Custom Explorer Bars, Tool Bands, and Desk Bands , you need to ensure your code behave like Explorer behaves. 如果您查看此处的文档创建自定义资源管理器栏,工具栏和桌面乐队 ,您需要确保您的代码行为与资源管理器一样。 So, the fist thing to do is to give a "Site" implementation to the desk band object, and the first interface this implementation needs to provide is IOleWindow. 因此,首先要做的是给桌面带对象提供“站点”实现,而这个实现需要提供的第一个接口是IOleWindow。 The desk band object will ask your "Site" what is the parent windows handle. 桌面带对象将询问您的“站点”父窗口句柄是什么。 Just give the form's handle (for example) and the desk band will display itself as a Form's child: 只需提供表单的句柄(例如),桌面乐队将自己显示为Form的子句:

在此输入图像描述

NOTE: You can't use any Form or Control class as the IOleWindow implementer because it's already implementing it behind the scene (Winforms implementation), and this implementation is very specific, so you'll need a custom site as demonstrated here. 注意:您不能使用任何Form或Control类作为IOleWindow实现者,因为它已经在场景后面实现它(Winforms实现),并且此实现非常具体,因此您需要一个自定义站点,如此处所示。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private IObjectWithSite _band = (IObjectWithSite)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("{01E04581-4EEE-11d0-BFE9-00AA005B4383}")));
        private BandSite _site;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void CreateHandle()
        {
            base.CreateHandle();
            if (_site == null)
            {
                _site = new BandSite(Handle);
                _band.SetSite(_site);
            }
        }

        private class BandSite : IOleWindow
        {
            private IntPtr _hwnd;

            public BandSite(IntPtr hwnd)
            {
                _hwnd = hwnd;
            }

            void IOleWindow.GetWindow(out IntPtr hwnd)
            {
                hwnd = _hwnd;
            }

            void IOleWindow.ContextSensitiveHelp(int fEnterMode)
            {
                throw new NotImplementedException();
            }
        }
    }

    [ComImport, Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectWithSite
    {
        void SetSite([MarshalAs(UnmanagedType.IUnknown)] object pUnkSite);

        [return: MarshalAs(UnmanagedType.IUnknown)]
        object GetSite(ref Guid riid);
    }

    [ComImport, Guid("00000114-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleWindow
    {
        void GetWindow(out IntPtr hwnd);
        void ContextSensitiveHelp(int fEnterMode);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM