如题,我用VB写的程序,已经知道另一其他程序窗口中的EDIT控制的句柄,我现在想把这个EDIT中的内容,清空,再赋值,如何实现
使用PostMessage实现,参考:
VB code
Private Const WM_CHAR = &H102
Private Const WM_CLEAR = &H303
Private 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
'清空文本框的内容
Private Sub btnClear_Click()
txtEdit.SelStart = 0
txtEdit.SelLength = Len(txtEdit.Text)
Call subClearEdit(txtEdit.hwnd)
End Sub
'向文本框写入文本内容
Private Sub btnWrite_Click()
Call subPostString(txtEdit.hwnd, "中华人民共和国")
End Sub
Private Sub Form_Load()
txtEdit.Text = "中华民国"
btnClear.Caption = "清除文本"
btnWrite.Caption = "写入文本"
End Sub
'将一个字符机械厂35/366KV总降压变电所设计串通过PostMessage传送出去
Private Sub subPostString(ByVal lngHandle As Long, ByVal strP As String)
Dim intP As Integer
Dim intK As Integer
Dim lngAsc As Long
Dim lngP As Long
Dim strT As String
On Error GoTo errSub
For intP = 0 To Len(strP) - 1
strT = Mid(strP, intP + 1, 1)
If Asc(strT) <= 0 Then
lngAsc = AscW(StrConv(strT, 128))
If lngAsc < 0 Then
lngAsc = lngAsc + 65536 '2^16
End If
Else
lngAsc = Asc(strT)
End If
lngP = PostMessage(lngHandle, WM_CHAR, lngAsc, 0)
Next intP
Exit Sub
errSub:
End Sub
Private Sub subClearEdit(ByVal lngHandle)
Dim lngP As Long
On Error GoTo errSub
lngP = PostMessage(lngHandle, WM_CLEAR, 0&, 0&)
Exit Sub
errSub:
End Sub
要清除首先的将该文本框的内容设置为选中状态,如果清除失败,应该是没有处于选中状态的原因
其实lngP = PostMessage(lngHandle, WM_CLEAR, 0&, 0&)执行成功没有很容易知道的,你单步执行,看看lngP的值不是0就成功了。