如何在vba中用SendMessage向窗口发送中文字符?

直接使用api函数SendMessage发送WM_SETTEXT消息可以直接修改窗口的标题文本、如果是编辑控件,则直接修改编辑控件的内容。如果是按钮控件,则直接修改按钮的名称。

在vba中可以使用如下的代码直接修改窗口的标题文本:

Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Public Type POINTAPI
    X As Long
    Y As Long
End Type
Const WM_SETTEXT = &HC
Sub QQ1722187970()
    Dim i As POINTAPI
    GetCursorPos i
    Debug.Print i.X, i.Y
    Dim lHwnd As Long
    lHwnd = WindowFromPoint(i.X, i.Y)
   Debug.Print SendMessage(lHwnd, WM_SETTEXT, 0, "我和你在一起")
End Sub

注意 SendMessage的VB声明部分的第四个参数lParam一定要声明为ByVal lParam As Any才行,否则会出现乱码。

 

 

       

发表评论