如何用VB实现向其他程序发送字符或文字?
为了减少重复性的输入,我想用VB编程的方式如何实现向Word或记事本等其他程序模拟键盘发送字符或文字,如中华人民共和国Chinese等等,另请不要仅仅回复使用如Sendk...
为了减少重复性的输入,我想用VB编程的方式如何实现向Word或记事本等其他程序模拟键盘发送字符或文字,如中华人民共和国Chinese等等,
另请不要仅仅回复使用如Sendkeys等函数,我想要完整源码学习VB,因我认为此程序需使用API,但我对API一窃不通.多谢
多谢“天枫十一郎”的回复
但这些都是声明API,但如何具体应用这些函数呢?即我按一下“CMD”按钮,如何能让此程序向WORD发送“中华人民共和国Chinese”等等字符和文字呢?多谢指教!
多谢“狮儿”的回复:
可能是我写的不清,我的目的减少键盘的手工输入,即先将一些常用字符和文字储存起来,在需向其他程序(不专用于WORD、记事本,还可以是其他如QQ,EXCEL等等程序)输入这些常用字符和文字时,就可以调用这个VB程序来实现输入,免去手工重复地用键盘向其他程序输入字符和文字的无用功。多谢! 展开
另请不要仅仅回复使用如Sendkeys等函数,我想要完整源码学习VB,因我认为此程序需使用API,但我对API一窃不通.多谢
多谢“天枫十一郎”的回复
但这些都是声明API,但如何具体应用这些函数呢?即我按一下“CMD”按钮,如何能让此程序向WORD发送“中华人民共和国Chinese”等等字符和文字呢?多谢指教!
多谢“狮儿”的回复:
可能是我写的不清,我的目的减少键盘的手工输入,即先将一些常用字符和文字储存起来,在需向其他程序(不专用于WORD、记事本,还可以是其他如QQ,EXCEL等等程序)输入这些常用字符和文字时,就可以调用这个VB程序来实现输入,免去手工重复地用键盘向其他程序输入字符和文字的无用功。多谢! 展开
2个回答
展开全部
楼主的想法实现起来是非常复杂的. 因为QQ已经屏蔽了SendMessage函数, 要使用Hook才能写QQ尾巴. 而API Hook已经远远超出我的知识范围. 就算有高手写出来, 楼主也不一定能看得懂.
而且写这种程序, 还不如直接在某个地方事先输入好常用的语句字符, 然后用键盘复制粘贴(CtrlC CtrlV), 这样也很快捷. 而要写一个所有程序通用的这种输入程序, 还不如用紫光拼音输入法自造词句来得更快, 你说是不是?
-----------------我是分隔线-------------------------------
我写过, 不过代码不在手头上. 如果你要往WORD里写入东西, 那不是用SendMessage的, VB可以与WORD用接口连接, 用类方法实现这种功能. 如果要往记事本等写入文本, 才用SendMessage. 以下代码用于改变标题为"无标题 - 记事本"的记事本窗口的文本. 注意, 是设置, 而不是追加文本. 我忘了追加文本是用什么消息了. 抱歉.
Option Explicit
'取句柄.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Any, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Long
'发消息
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'设置文本用的消息
Private Const WM_SETTEXT = &HC&
Private Sub Command1_Click()
Dim hWndWindow As Long
Dim hWndEdit As Long
'用FindWindow通过窗口标题取得窗口句柄
hWndWindow = FindWindow(vbNullString, "无标题 - 记事本")
'用findWindowEx通过窗口句柄取得文本框(即"edit")的句柄
hWndEdit = FindWindowEx(hWndWindow, vbNullString, "edit", vbNullString)
'用SendMessage向取得的文本框句柄发送WM_SETTEXT消息
SendMessage hWndEdit, WM_SETTEXT, 0, ByVal "中华人民共和国 Chinese"
End Sub
而且写这种程序, 还不如直接在某个地方事先输入好常用的语句字符, 然后用键盘复制粘贴(CtrlC CtrlV), 这样也很快捷. 而要写一个所有程序通用的这种输入程序, 还不如用紫光拼音输入法自造词句来得更快, 你说是不是?
-----------------我是分隔线-------------------------------
我写过, 不过代码不在手头上. 如果你要往WORD里写入东西, 那不是用SendMessage的, VB可以与WORD用接口连接, 用类方法实现这种功能. 如果要往记事本等写入文本, 才用SendMessage. 以下代码用于改变标题为"无标题 - 记事本"的记事本窗口的文本. 注意, 是设置, 而不是追加文本. 我忘了追加文本是用什么消息了. 抱歉.
Option Explicit
'取句柄.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Any, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Long
'发消息
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'设置文本用的消息
Private Const WM_SETTEXT = &HC&
Private Sub Command1_Click()
Dim hWndWindow As Long
Dim hWndEdit As Long
'用FindWindow通过窗口标题取得窗口句柄
hWndWindow = FindWindow(vbNullString, "无标题 - 记事本")
'用findWindowEx通过窗口句柄取得文本框(即"edit")的句柄
hWndEdit = FindWindowEx(hWndWindow, vbNullString, "edit", vbNullString)
'用SendMessage向取得的文本框句柄发送WM_SETTEXT消息
SendMessage hWndEdit, WM_SETTEXT, 0, ByVal "中华人民共和国 Chinese"
End Sub
展开全部
取句柄 发消息.一向不为问题写手上没有的代码.呵
------------------------
'取句柄.
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Any, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Long
'发消息
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
------------------------
'取句柄.
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Any, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Long
'发消息
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询