从处理NativeWindow中的WM_GETOBJECT返回IOleCommandTarget

时间:2011-06-23 19:49:44

标签: c# com wndproc native-methods nativewindow

我正在尝试从面板控件句柄中检索IOleCommandTarget引用,以便我可以在其上调用IOleCommandTarget.Exec()。

NativeMethods.IOleCommandTarget target = null;
if (GetObjectFromHandle<NativeMethods.IOleCommandTarget>(panel.Handle, out target))
{
    Guid guidCmdGroup = commandID.Guid;
    handled = (target.Exec(ref guidCmdGroup, commandID.ID, 0, null, 0) == NativeMethods.S_OK);
}

private static bool GetObjectFromHandle<T>(IntPtr hwnd, out T value)
{
    Guid guid = typeof(T).GUID;
    object obj = null;
    var hr = NativeMethods.AccessibleObjectFromWindow(hwnd, 0, ref guid, ref obj);
    if (hr == NativeMethods.S_OK)
    {
        value = (T)obj;
        return true;
    }
    value = default(T);
    return false;
}

在我的NativeMethods.cs中:

[ComImport]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("B722BCCB-4E68-101B-A2BC-00AA00404770")]
public interface IOleCommandTarget
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int QueryStatus(ref Guid pguidCmdGroup, int cCmds, [In, Out] NativeMethods.OLECMD prgCmds, [In, Out] IntPtr pCmdText);
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int Exec(ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt, [In, MarshalAs(UnmanagedType.LPArray)] object[] pvaIn, int pvaOut);
}

[DllImport("oleacc.dll")]
public static extern int AccessibleObjectFromWindow(
        IntPtr hwnd,
        uint id,
        ref Guid iid,
        [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);

为了能够返回IOleCommandTarget引用,我创建了一个实现NativeWindow和IOleCommandTarget的CommandTargetWindow类,我正在重写WndProc以拦截AccessibleObjectFromWindow()发送的WM_GETOBJECT消息:

public sealed class CommandTargetWindow : NativeWindow,
    NativeMethods.IOleCommandTarget,
    IDisposable
{
    private IWin32Window _parent;

    public CommandTargetWindow(IWin32Window parent)
    {
        _parent = parent;
        base.AssignHandle(parent.Handle);
    }

    [PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (m.Msg == NativeMethods.WM_GETOBJECT)
        {
            //How do I pass back an IOleCommandTarget through the message?
        }
        base.WndProc(ref m);
    }
}

问题是,如上面的评论所示,如何通过邮件传回IOleCommandTarget?

我们通过实现接口IRawElementProviderSimple(而不是IOleCommandTarget)并使用定义的静态方法AutomationInteropProvider.ReturnRawElementProvider()执行其他地方的自动化操作:

[PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if ((m.Msg == NativeMethods.WM_GETOBJECT) && (m.LParam.ToInt32() == AutomationInteropProvider.RootObjectId))
    {
        m.Result = AutomationInteropProvider.ReturnRawElementProvider(
            Handle, m.WParam, m.LParam, (IRawElementProviderSimple)this);

        return;
    }
    base.WndProc(ref m);
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

事实证明我需要使用我在NativeMethods.cs中定义的COM方法LresultFromObject作为

[DllImport("oleacc.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr LresultFromObject(ref Guid refiid, IntPtr wParam, IntPtr pAcc);

现在在我的WndProc中,我调用LresultFromObject来返回m.Result中的IOleCommandTarget句柄:

[PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == (int)NativeMethods.WindowMessage.GETOBJECT)
    {
        if (m.LParam.ToInt32() == AutomationInteropProvider.RootObjectId)
        {
            m.Result = AutomationInteropProvider.ReturnRawElementProvider(
                Handle, m.WParam, m.LParam, (IRawElementProviderSimple)this);

            return;
        }
        else if (m.LParam == (IntPtr)NativeMethods.OBJID_NATIVEOM)
        {
            IntPtr handle = Marshal.GetComInterfaceForObject(this, typeof(NativeMethods.IOleCommandTarget));
            Guid unknownGuid = typeof(NativeMethods.IUnknown).GUID;
            m.Result = NativeMethods.LresultFromObject(ref unknownGuid, m.WParam, handle);
            return;
        }
    }
    base.WndProc(ref m);
}