求个简单的VB获取当前聚焦窗口的句柄代码

来源:百度知道 编辑:UC知道 时间:2024/05/06 21:39:06
我在网上抄人家的代码,自己写了个简单的内存修改器,写好了,但感觉非常不方便,我自己又没实力改

他是通过FindWindow函数查找窗口标题,获取句柄

然后GetWindowThreadProcessId跟OpenProcess

明显不同程序的标题不一样,这样不方便啊,我就是想直接获取当前聚焦的窗口(整个窗口不是某个控件啊)的句柄,然后进行我的简单内存修改

我在网上查了下,有说用GetForegroundWindow的,但不知怎么回事,就是无法进行内存修改,所以来求助各位大侠

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long

Dim hAcive As Long

Private Sub Form_Load()
hAcive = 0
End Sub

Private Sub Timer1_Timer()
Dim a As Long
a = GetForegroundWindow
If a <> Me.hWnd Then hAcive = a
End Sub

'hAcive 就是当前拥有聚焦窗口的句柄

'返回窗口句柄 whWnd 所属进程 ID
Private Function WindowProcessID(ByVal whWnd As Long) As Long
Dim lpdwProcessId As Long
GetWindowThreadProcessId whWnd, lpdwProcessId

WindowProcessID = lpdwProcessId
End Function