在NativeWindow中处理WM_GETOBJECT时返回IOleCommandTarget



我正试图从面板控制句柄中检索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);
}

有什么想法吗?

原来我需要使用COM方法LresultFromObject,我在NativeMethods.cs中将其定义为

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

现在在我的WndProc中,我调用LresultFromObject以返回m中的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);
}

相关内容

  • 没有找到相关文章