永发信息网

VB如何实现:隐藏外部程序窗口

答案:5  悬赏:0  手机版
解决时间 2021-03-04 12:23
  • 提问者网友:欲望失宠
  • 2021-03-04 08:32
我想用VB,实现 隐藏外部程序窗口。不是最小化,是隐藏。

首先:获取窗口句柄。代码如下////

Private Declare Function GetForegroundWindow 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 Long
Private Sub Timer1_Timer()
Jubing = GetForegroundWindow()
BiaoTi = Space(256)
GetWindowText Jubing, BiaoTi, 256
Cls
Print "当前活动窗口 句柄是:" & Jubing

End Sub

'注释下: 我获取的窗口名是 Baby[sYs]:

之后 我就不知道怎么下手了。

有人说用 Showwindow 来隐藏 指定句柄。

但是我怎么也不懂,也不知道怎么用,我只是初学者
最佳答案
  • 五星知识达人网友:骨子里都是戏
  • 2021-03-04 09:47
添加一个窗体:
窗体代码:
Option Explicit
Private Sub Form_Load()
'设置窗体过程
SetWindowHook Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
'取消窗体过程。
DelWindowHook
End Sub

添加一个模块,模块代码:
Option Explicit
Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function RegisterHotKey Lib "user32" _
(ByVal hWnd As Long, ByVal id As Long, _
ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" _
(ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_HOTKEY = &H312
Private Const VK_F7 = &H76
Dim hPrevWndProc As Long '原先的窗口过程。
Dim hPrevHandle As Long '原先的窗口句柄。
Dim hHotKey As Long '热键句柄。
Dim hH As Long '隐藏的窗口句柄。
Private Function GetClassNameStr(ByVal hWnd As Long) As String
'获取窗体的类名。
Dim TempStr As String, Rc As Long
Const NAME_MAX_LEN = 256
TempStr = Space(NAME_MAX_LEN)
Rc = GetClassName(hWnd, TempStr, NAME_MAX_LEN)
GetClassNameStr = StrConv(LeftB$(StrConv(TempStr, _
vbFromUnicode), Rc), vbUnicode)
End Function
Private Function GetWindowTextStr(ByVal hWnd As Long) As String
'获取窗体标题。
Dim TempStr As String, Rc As Long
Const NAME_MAX_LEN = 256
TempStr = Space(NAME_MAX_LEN)
Rc = GetWindowText(hWnd, TempStr, NAME_MAX_LEN)
GetWindowTextStr = StrConv(LeftB$(StrConv(TempStr, _
vbFromUnicode), Rc), vbUnicode)
End Function
Private Function WindowProc(ByVal hWnd As Long, _
ByVal uMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
'新的窗口过程。
Dim hNowWindow As Long
If uMsg = WM_HOTKEY And wParam = 101 Then
'如果按下了指定热键。

hNowWindow = GetForegroundWindow()
If GetClassNameStr(hNowWindow) = "Notepad" _
And GetWindowTextStr(hNowWindow) = _
"无标题 - 记事本" And hH = 0 Then
'如果当前的活动窗口是无标题记事本。

'隐藏窗体。
hH = hNowWindow
ShowWindow hH, 0
ElseIf hH <> 0 Then
'显示窗体
ShowWindow hH, 5
hH = 0
End If

End If
'调用原有的窗体过程。
WindowProc = CallWindowProc(hPrevWndProc, _
hWnd, uMsg, wParam, lParam)
End Function
Public Sub SetWindowHook(ByVal hWnd As Long)
'设置新的窗口过程。
hPrevHandle = hWnd
hPrevWndProc = SetWindowLong _
(hPrevHandle, GWL_WNDPROC, AddressOf WindowProc)
'设置热键(F7)。
hHotKey = RegisterHotKey(hPrevHandle, 101, 0, VK_F7)
End Sub
Public Sub DelWindowHook()
'恢复原有的窗口过程。
SetWindowLong hPrevHandle, GWL_WNDPROC, hPrevWndProc
'取消热键
UnregisterHotKey hPrevHandle, hHotKey
End Sub
全部回答
  • 1楼网友:怙棘
  • 2021-03-04 13:10
ShowWindow 的参数是 窗口句柄 和 布尔值(真---显示窗口,假---隐藏窗口)。 ShowWindow Jubing, 0 就可以了。
  • 2楼网友:梦中风几里
  • 2021-03-04 12:48
option explicit const sw_hide = 0 const gw_owner = 4 private declare function getwindow lib "user32" (byval hwnd as long, byval wcmd as long) as long private declare function showwindow lib "user32" (byval hwnd as long, byval ncmdshow as long) as long private sub form_load()     me.visible = false     showwindow getwindow(me.hwnd, gw_owner), sw_hide end sub private sub timer1_timer() '测试用的     debug.print timer end sub
  • 3楼网友:胯下狙击手
  • 2021-03-04 11:36
Private Sub Form_Load() Me.Hide End Sub
  • 4楼网友:孤独入客枕
  • 2021-03-04 10:41
添加一个窗体: 窗体代码: Option Explicit Private Sub Form_Load() '设置窗体过程 SetWindowHook Me.hWnd End Sub Private Sub Form_Unload(Cancel As Integer) '取消窗体过程。 DelWindowHook End Sub 添加一个模块,模块代码: Option Explicit Private Declare Function ShowWindow Lib "user32" _ (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long Private Declare Function SetWindowLong Lib "user32" _ Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long Private Declare Function CallWindowProc Lib "user32" _ Alias "CallWindowProcA" _ (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function RegisterHotKey Lib "user32" _ (ByVal hWnd As Long, ByVal id As Long, _ ByVal fsModifiers As Long, ByVal vk As Long) As Long Private Declare Function UnregisterHotKey Lib "user32" _ (ByVal hWnd As Long, ByVal id As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _ (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _ (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Const GWL_WNDPROC = (-4) Private Const WM_HOTKEY = &H312 Private Const VK_F7 = &H76 Dim hPrevWndProc As Long '原先的窗口过程。 Dim hPrevHandle As Long '原先的窗口句柄。 Dim hHotKey As Long '热键句柄。 Dim hH As Long '隐藏的窗口句柄。 Private Function GetClassNameStr(ByVal hWnd As Long) As String '获取窗体的类名。 Dim TempStr As String, Rc As Long Const NAME_MAX_LEN = 256 TempStr = Space(NAME_MAX_LEN) Rc = GetClassName(hWnd, TempStr, NAME_MAX_LEN) GetClassNameStr = StrConv(LeftB$(StrConv(TempStr, _ vbFromUnicode), Rc), vbUnicode) End Function Private Function GetWindowTextStr(ByVal hWnd As Long) As String '获取窗体标题。 Dim TempStr As String, Rc As Long Const NAME_MAX_LEN = 256 TempStr = Space(NAME_MAX_LEN) Rc = GetWindowText(hWnd, TempStr, NAME_MAX_LEN) GetWindowTextStr = StrConv(LeftB$(StrConv(TempStr, _ vbFromUnicode), Rc), vbUnicode) End Function Private Function WindowProc(ByVal hWnd As Long, _ ByVal uMsg As Long, ByVal wParam As Long, _ ByVal lParam As Long) As Long '新的窗口过程。 Dim hNowWindow As Long If uMsg = WM_HOTKEY And wParam = 101 Then '如果按下了指定热键。 hNowWindow = GetForegroundWindow() If GetClassNameStr(hNowWindow) = "Notepad" _ And GetWindowTextStr(hNowWindow) = _ "无标题 - 记事本" And hH = 0 Then '如果当前的活动窗口是无标题记事本。 '隐藏窗体。 hH = hNowWindow ShowWindow hH, 0 ElseIf hH <> 0 Then '显示窗体 ShowWindow hH, 5 hH = 0 End If End If '调用原有的窗体过程。 WindowProc = CallWindowProc(hPrevWndProc, _ hWnd, uMsg, wParam, lParam) End Function Public Sub SetWindowHook(ByVal hWnd As Long) '设置新的窗口过程。 hPrevHandle = hWnd hPrevWndProc = SetWindowLong _ (hPrevHandle, GWL_WNDPROC, AddressOf WindowProc) '设置热键(F7)。 hHotKey = RegisterHotKey(hPrevHandle, 101, 0, VK_F7) End Sub Public Sub DelWindowHook() '恢复原有的窗口过程。 SetWindowLong hPrevHandle, GWL_WNDPROC, hPrevWndProc '取消热键 UnregisterHotKey hPrevHandle, hHotKey End Sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯