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

VB如何实现:隐藏外部程序窗口,第1张

添加一个窗体

窗体代码:

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

别瞎试,很厉害的

先把form1的BorderStyle属性设为0

Private Sub Form_Load()

App.TaskVisible = False '在任务管理器中隐藏

Me.Left = 0

Me.Top = 0

Me.Height = Screen.Height

Me.Width = Screen.Width

Timer1.Interval = 10

End Sub

Private Sub Timer1_Timer()

Me.Show

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/12015647.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存