4
我正在使用Win32 SetParent函数在我的应用程序中嵌入MS Word。 一切工作正常,但有一个要求从Word自定义 工具栏按钮回调到父应用程序。 Word实例嵌入在用户控件中,所以 父是this.Handle。双向沟通的MS Word自动化
的VBA代码如下:
Sub Submit()
Dim hwnd As Long
hwnd = FindWindow("Opusapp", vbNullString)
hwnd = GetAncestor(hwnd, GA_PARENT)
If hwnd = 0 Then
MsgBox "Failed to callback!"
Exit Sub
End If
OutputDebugString ("Parent window " + CStr(hwnd))
Dim id As Long
id = RegisterWindowMessage("__CALLBACK_FROM_WORD__")
If hwnd = 0 Then
MsgBox "Failed to callback. Message not registered"
Exit Sub
End If
OutputDebugString ("Message " + CStr(id))
End Sub
在C#代码是这样的:
protected override void OnHandleCreated(EventArgs e)
{
submitMessageId_ = RegisterWindowMessage("__CALLBACK_FROM_WORD__");
base.OnHandleCreated(e);
}
protected override void OnHandleDestroyed(EventArgs e)
{
base.OnHandleDestroyed(e);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == submitMessageId_)
{
Logger.Instance().Write("WndProc: Submit event");
return;
}
base.WndProc(ref m);
}
的问题似乎是,VBA无法找到正确的窗口句柄。 我尝试使用GetParent无济于事。
也许我可以使用SetWindowText为了将正确的hwnd传递给VBA – SparcU 2011-01-20 10:03:46