我尝试使用PostMessage将字符宏按钮发送到Windows窗体桌面应用程序,但我没有收到我在应用程序中的预期。我使用C#中的模板创建doc并为其提供自定义属性,即桌面应用程序窗口句柄(sVsHandle)。然后将这样的Postessage发送到句柄:
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_KEYDOWN = &H100
Option Explicit
Public myRibbon As IRibbonUI
Sub Onload(ribbon As IRibbonUI)
Set myRibbon = ribbon
End Sub
'Callback for Button onAction
Sub HandleButtons(ByRef control As IRibbonControl)
Dim sText As String
Dim sWordHandle As String
Dim sVsHandle As Long
Dim sMessage As String
ActiveDocument.Saved = True
sVsHandle = ActiveDocument.CustomDocumentProperties("Handle")
sWordHandle = ActiveDocument.CustomDocumentProperties("wordHandle")
Call SendText(sVsHandle)
End Sub
Sub SendText(hWnd As Long)
If hWnd = 0 Then MsgBox "no hWnd supplied": Exit Sub
Dim zwParam As Long
Dim zlParam As Long
zlParam = &H1E0001
zwParam = Asc("D")
PostMessage hWnd, WM_KEYDOWN, zwParam, zlParam
End Sub
我在Windows窗体应用程序中收到了这样的内容:
[System.Runtime.InteropServices.DllImport("user32",
EntryPoint = "PostMessage",
ExactSpelling = false,
CharSet = System.Runtime.InteropServices.CharSet.Auto,
SetLastError = true)]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == 135)
if (Convert.ToInt32(m.WParam) > 0)
{
StartUp.gclsViewDocs.EvaluateWordMessages(System.Runtime.InteropServices.Marshal.PtrToStringUni(m.LParam));
}
}
然而,lParam或wParam始终为0且m.Msg始终等于135,尽管期望它为& H100(256)。
我以前使用过SendMessage,但想将其更改为PostMessage。有人可以解释我出错的地方,谢谢。