vb应用程序中如何自动获取当前窗口的名称
如我在设计一个键盘记录器时,要知道QQ登录窗口的名称,为了不让小孩上网聊天,当他登录QQ时,当软件侦测到QQ窗口名称时,计算机将自动关闭QQ登录窗口。...
如我在设计一个键盘记录器时,要知道QQ登录窗口的名称,为了不让小孩上网聊天,当他登录QQ时,当软件侦测到QQ窗口名称时,计算机将自动关闭QQ 登录窗口。
展开
2个回答
展开全部
1. 在 程序中
EnumWindows AddressOf EnumWindowsProc2, 0&
然后判断
if lFindWIndowHwnd <>0 then
TerminateProcess lFindWIndowHwnd,0 ’关闭指定的程序
end if
2. 下面是一个模块
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const SW_SHOWNORMAL = 1
Public Const SW_HIDE = 0
Public Const SWP_HIDEWINDOW = &H80
Public Const SWP_SHOWWINDOW = &H40
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
public lFindWIndowHwnd as long '找到窗口的句柄
Public Function EnumWindowsProc2(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sA
Dim S As String
EnumWindowsProc2 = False
sA = “QQ2011” 要监控的程序窗口标题,或者直接写成QQ
If Trim(sA) = "" Then Exit Function
S = String(80, Chr(0))
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
If InStr(1, S, sA) <= 0 Then
EnumWindowsProc2 = True
else
lFindWIndowHwnd = hwnd '返回找到的窗口句柄。
End If
End Function
EnumWindows AddressOf EnumWindowsProc2, 0&
然后判断
if lFindWIndowHwnd <>0 then
TerminateProcess lFindWIndowHwnd,0 ’关闭指定的程序
end if
2. 下面是一个模块
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const SW_SHOWNORMAL = 1
Public Const SW_HIDE = 0
Public Const SWP_HIDEWINDOW = &H80
Public Const SWP_SHOWWINDOW = &H40
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
public lFindWIndowHwnd as long '找到窗口的句柄
Public Function EnumWindowsProc2(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sA
Dim S As String
EnumWindowsProc2 = False
sA = “QQ2011” 要监控的程序窗口标题,或者直接写成QQ
If Trim(sA) = "" Then Exit Function
S = String(80, Chr(0))
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
If InStr(1, S, sA) <= 0 Then
EnumWindowsProc2 = True
else
lFindWIndowHwnd = hwnd '返回找到的窗口句柄。
End If
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询