用Me.ActiveControl可以获得当前窗体中拥有焦点的那个控件。要注意的是,你不能在会导致焦点改变的情况下进行检测,比如按钮单击事件中,这时焦点是按钮了,就无法知道点击之前焦点是在哪个控件了。建议用时钟控件,比如: Private Sub Form_Load() Timer1.Interval = 500 Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() Debug.Print Me.ActiveControl.Name End Sub
Private Declare Function GetFocus Lib "user32" Alias "GetFocus" () As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long