Private Declare Function GetFocus Lib "user32" () As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Sub Form_click() Form2.Show End SubPrivate Sub Timer1_Timer() Dim lnghWnd As Long Dim strText As String * 1000 Dim lngLenth As Long Dim lngReturn As Long lnghWnd = GetFocus() lngLenth = 1000 lngLenth = GetWindowText(lnghWnd, strText, lngLenth) Text1.Text = lnghWnd Text2.Text = Left(strText, lngLenth) End Sub 这种方法只能获得,本进程内的焦点控件的文本
窗体中放一个Text1,timer1,Command1,点击Command1后马上把焦点移到Text1,试试 Private Sub Command1_Click() Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Enabled = False Timer1.Interval = 2000 End SubPrivate Sub Timer1_Timer() MsgBox ActiveControl.Text Timer1.Enabled = False End Sub
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Sub Form_click()
Form2.Show
End SubPrivate Sub Timer1_Timer()
Dim lnghWnd As Long
Dim strText As String * 1000
Dim lngLenth As Long
Dim lngReturn As Long
lnghWnd = GetFocus()
lngLenth = 1000
lngLenth = GetWindowText(lnghWnd, strText, lngLenth)
Text1.Text = lnghWnd
Text2.Text = Left(strText, lngLenth)
End Sub
这种方法只能获得,本进程内的焦点控件的文本
Private Sub Command1_Click()
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 2000
End SubPrivate Sub Timer1_Timer()
MsgBox ActiveControl.Text
Timer1.Enabled = False
End Sub