gpt4 book ai didi

c# - 从处理 NativeWindow 中的 WM_GETOBJECT 返回 IOleCommandTarget

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:21 25 4
gpt4 key购买 nike

我正在尝试从面板控制句柄中检索 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?

我们通过实现接口(interface) 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);
}

有什么想法吗?

最佳答案

事实证明我需要使用我在 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);
}

关于c# - 从处理 NativeWindow 中的 WM_GETOBJECT 返回 IOleCommandTarget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6459778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com