读书人

API获得记事本编辑框文本的有关问题

发布时间: 2012-03-28 15:40:03 作者: rapoo

API获得记事本编辑框文本的问题
我编写了一个API获得记事本编辑框文本的小程序,但是句柄获得成功,但是文本确不能获得,求教各位高手。代码如下:

'获得窗体的句柄
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'获得窗体的文本
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Const WM_SETTEXT = &HC

Private Sub Command1_Click()
Dim WindowHandle As Long
Dim sBuffer As String

WindowHandle = FindWindow(vbNullString, "新建文本文档 (2).txt - 记事本")
Text1.Text = WindowHandle

sBuffer = Space(255)
GetWindowText WindowHandle, sBuffer, 255

Text2.Text = sBuffer '此处结果为记事本的标题


Ehwnd = FindWindowEx(WindowHandle, 0, "Edit", vbNullString)

Text3.Text = Ehwnd

sBuffer = Space(255)
GetWindowText Ehwnd, sBuffer, 255

Text4.Text = sBuffer '此处结果为空,什么都没有


End Sub


[解决办法]
发消息取得:

VB code
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long'获得窗体的文本Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPrivate Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As LongPrivate Const WM_GETTEXT As Long = &HD&Private Const WM_SETTEXT As Long = &HC&Private Sub Command1_Click()  Dim WindowHandle As Long  Dim sBuffer As String      WindowHandle = FindWindow(vbNullString, "无标题 - 记事本")  Text1.Text = WindowHandle      sBuffer = Space(255)  GetWindowText WindowHandle, sBuffer, 255      Text2.Text = sBuffer '此处结果为记事本的标题          Ehwnd = FindWindowEx(WindowHandle, 0, "Edit", vbNullString)      Text3.Text = Ehwnd      sBuffer = Space(255)  Call SendMessage(Ehwnd, WM_GETTEXT, 255, ByVal sBuffer)  sBuffer = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)      Text4.Text = sBuffer '此处结果为空,什么都没有End Sub 

读书人网 >VB

热点推荐