永发信息网

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

答案:5  悬赏:0  手机版
解决时间 2021-03-04 12:23
我想用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 来隐藏 指定句柄。

但是我怎么也不懂,也不知道怎么用,我只是初学者
最佳答案
添加一个窗体:
窗体代码:
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
全部回答
ShowWindow 的参数是 窗口句柄 和 布尔值(真---显示窗口,假---隐藏窗口)。 ShowWindow Jubing, 0 就可以了。
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
Private Sub Form_Load() Me.Hide End Sub
添加一个窗体: 窗体代码: 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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
手机上64g的内存卡怎么格式化成32g的
谷实丰农资有限责任公司地址有知道的么?有点
电信如何设置无法接通
智者近水仁者乐山
东山县福来食品有限公司地址在哪,我要去那里
对搜索结果进行红字显示
brewell特调师562烟草怎么辨别真假
麻辣诱惑勒泰店地址有知道的么?有点事想过去
I grew up in a small town 还是 i grew up o
姑姑喜欢挽我的手,在一起时很开心,她到底怎
哪种防噪音耳塞,能防止任何声音。
中国移动鑫联讯手机商场德盛路分店在什么地方
哪里有paas云计算平台实战视频教程?
枣庄仙坛山温泉小镇 好玩吗
用友财务软件怎么能设置在宽210.0,高255.0的
推荐资讯
明朝有个画家叫冯梦×﹖
现在工资低了,效益不好,老婆总说叫我去搞钱
执业助理医师能考几次啊?
一体机功放有电流声!求破!
求一面心灵上的镜子这篇作文怎么写
天工设计地址在哪,我要去那里办事
呼吸时胸腔内发出捻发音,由其是爬这的时候最
我从任何方向看这个立体图形,看到的都是圆,这
从建湖到盐城要乘什么车?
推荐几部魔幻爱情的动漫,就像星刻龙骑士那样
嘉声音响在什么地方啊,我要过去处理事情
成都9眼桥实际上有几个眼眼...?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?