要求在程序启动的时候主窗口隐藏,只在系统托盘里显示一个图标。一直以来采用的方法都是设置窗口的ShowInTaskBar=false, WindowState=Minimized。但是偶然发现尽管这样的方法可以使主窗口隐藏不见,但是在用Alt+Tab的时候却可以看见这个程序的图标并把这个窗口显示出来。因此这种方法其实并不能满足要求。c#隐藏窗口方法一: 重写setVisibleCore方法protectedoverridevoidSetVisibleCore(bool){baseSetVisibleCore(false);}这个方法比较简单,但是使用了这个方法后主窗口就再也不能被显示出来,而且在退出程序的时候也必须调用ApplicationExit方法而不是Close方法。这样的话就要考虑一下,要把主窗口的很多功能放到其他的地方去。c#隐藏窗口方法二: 不创建主窗口,直接创建NotifyIcon和ContextMenu组件这种方法比较麻烦,很多代码都必须手工写staticvoidMain(){ApplicationEnableVisualStyles();ApplicationSetCompatibleTextRenderingDefault(false);SystemResourcesResourceManagerresources=newSystemResourcesResourceManager(myResource,SystemReflectionAssemblyGetExecutingAssembly());NotifyIconni=newNotifyIcon();niBalloonTipIcon=SystemWindowsFormsToolTipIconWarning;niBalloonTipText=test!;niBalloonTipTitle=test;//niContextMenuStrip=contextMenu;niIcon=((SystemDrawingIcon)(resourcesGetObject(niIcon)));niText=Test;niVisible=true;niMouseClick+=delegate(objectsender,MouseEventArgse){niShowBalloonTip(0);};ApplicationRun();}如果需要的组件太多,这个方法就很繁琐,因此只是做为一种可行性研究。c#隐藏窗口方法三:前面两种方法都有一个问题,主窗口不能再显示出来。
您可以通过以下两种方法隐藏应用:
1、您可以点击 设置 > 安全和隐私 > 隐私空间,根据屏幕提示设置隐私空间,然后将需要隐藏的应用安装到隐私空间。
2、您也可以点击 设置 > 用户和帐户 > 多用户,根据屏幕提示添加访客,然后通过访客模式访问来实现应用隐藏。
添加一个窗体:
窗体代码:
Option
Explicit
Private
Sub
Form_Load()
'设置窗体过程
SetWindowHook
MehWnd
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
以上就是关于C#隐藏窗口的几种方法全部的内容,包括:C#隐藏窗口的几种方法、华为荣耀8手机如何设置可以隐藏应用程序、VB如何实现:隐藏外部程序窗口等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)