得知視窗hWnd的方法:

  取得hWnd的值後可以做到很多很霸道的功能這點相信大家也知道,因為之前的文章也提過。但是如何取得視窗的hWnd值呢?以下是一些可行的方法:

利用API函數GetForegroundWindow:(從得到駐點的視窗取得hWnd)

Private Declare Function GetForegroundWindow Lib "user32" () As Long

Private Sub Form_Load()
 Timer1.Interval = 1
End Sub

Private Sub Timer1_Timer()
 Text1 = GetForegroundWindow
End Sub

利用API函數FindWindow:(從表單標題取得hWnd)

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
Dim GethWnd As Long
 GethWnd = FindWindow(vbNullString, Me.Caption)
 Print GethWnd 'GethWnd就是表單的hWnd值
End Sub

利用API函數WindowFromPoint:(從滑鼠位置取得hWnd)

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
 x As Long
 y As Long
End Type

Dim a As POINTAPI
Dim GethWnd As Long

Private Sub Form_Activate()
 Do
  DoEvents
  GetCursorPos a
  GethWnd = WindowFromPoint(a.x, a.y)
  Text1 = GethWnd 'GethWnd就是表單的hWnd值
 Loop
End Sub

以上是一些方法,其實還有其他做法如利用API函數ENumWindow等…但一般常用的程式利用以上方法便足夠了!


上一頁