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

我想用VB,实现隐藏外部程序窗口。不是最小化,是隐藏。首先:获取窗口句柄。代码如下////PrivateDeclareFunctionGetForegroundWindo... 我想用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 来隐藏 指定句柄。

但是我怎么也不懂,也不知道怎么用,我只是初学者
知道的 请附上 完整代码。
展开
 我来答
宇歆sunny
推荐于2017-09-28 · 知道合伙人数码行家
宇歆sunny
知道合伙人数码行家
采纳数:374 获赞数:4165
毕业于大连软件学院,软件编程行业2年从业经验,参与多个项目操作,现从业与博远科技程序员

向TA提问 私信TA
展开全部
添加一个窗体:
窗体代码:
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
孟祥度
2011-05-02 · TA获得超过153个赞
知道小有建树答主
回答量:250
采纳率:70%
帮助的人:93.2万
展开全部
ShowWindow 的参数是 窗口句柄 和 布尔值(真---显示窗口,假---隐藏窗口)。
ShowWindow Jubing, 0
就可以了。
追问
不行啊
追答
加在这句之前:GetWindowText Jubing, BiaoTi, 256
参数里面的0如果不行就换成vb里面的“假”。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hmlky
2011-05-02 · TA获得超过1369个赞
知道小有建树答主
回答量:239
采纳率:0%
帮助的人:365万
展开全部
添加一个窗体:
窗体代码:
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
追问
有错误提示
追答
会不会是你操作错了,我调试运行没有问题啊,
编译器也没有报错。
先运行你的程序,然后打开一个记事本程序,
保证记事本为当前活动窗口时,按一下F7就
可以隐藏了,再按一下F7又显示了。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
feiniaoflyer
2015-10-03 · TA获得超过542个赞
知道小有建树答主
回答量:585
采纳率:0%
帮助的人:531万
展开全部
Private Sub Form_Load()
Me.Hide
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
泡芙可爱如猪TV
2011-05-03 · TA获得超过1271个赞
知道小有建树答主
回答量:1393
采纳率:0%
帮助的人:899万
展开全部
你需要的在这个程序里都能找到
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式