日期:2014-05-19  浏览次数:20874 次

求解探讨:隐藏窗体的操作[乱顶勿进]
目的:
右下角出现图标,双击显示主窗体,该窗体并可以用热键呼出

问题难点:
1、主窗体的隐藏,目前网络上的代码无一可以真正实现。
唯一接近的是用主窗体不显示在任务栏并且最小化实现的,但是这时候你是可以用Alt+Tab看到该窗体的存在的。如何实现窗体在加载的同时不可见,就似乎成了一大难点。

2、隐藏的窗体,无法用热键呼出。
正常的窗体,没有问题,但是窗体一旦Visible   =   False,就不接受热键了。

这些所谓的难点,在其他语言里面是根本不存在的,无论是VB还是Dephi,都无此类问题,.Net真是岂有此理阿~~

其他:
我不是初学者,大家给方案就好,代码不需要复制那么多了。
======================================================================
本贴说明:
1、技术交流贴,非散分。分只给交流的人,结贴率100%;
2、欢迎一切问题有关的交流——无论答案对错;
3、不欢迎   顶、Mark、支持之类口水混分的人;
4、因为你的行为,影响了我删除零回复的权力;
所以,我保留我对蹭分者的厌恶,你也有权力对我鄙视。

<希望只知道蹭分的垃圾们去死,还技术区的清静>
======================================================================



------解决方案--------------------
codeproject 上面有类似的代码, 还是有楼主所说的问题,在窗体隐藏到右下角图标时,用API就找不到这个窗体句柄.
我后来是通过 在窗体隐藏前 将句柄保存到注册表中. (后来觉得保存在配置文件中通用些,效果没试). 有了句柄问题解决了. 楼主可试试.

------解决方案--------------------
采用注册热键的方法,下面的例子注册了Ctrl+Shift+B的热键
Declare Function RegisterHotKey Lib "user32 " (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Declare Function UnregisterHotKey Lib "user32 " (ByVal hWnd As IntPtr, ByVal id As Integer) As Integer
Private Key_Alt = 1
Private Key_Control = 2
Private Key_Shift = 4
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim success As Integer = RegisterHotKey(Me.Handle, 1111112, Key_Control Or Key_Shift, Keys.B)
If success = 0 Then
MsgBox( "注册失败,热键冲突 ")
End If
End Sub

Private Sub Form2_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
If Me.WindowState = FormWindowState.Minimized Then
Me.Hide()
End If
End Sub

Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
Me.Show()
Me.WindowState = FormWindowState.Normal
End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.WParam.ToInt32
Case 1111112
Me.Visible = Not Me.Visible
Me.Activate()
Case Else
MyBase.WndProc(m)
End Select
End Sub

还可以在sub main函数中注册系统热键,这时要添加一个MessageFilter
Module Common
Public context As ApplicationContext
Declare Function RegisterHotKey Lib "user32 " (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Declare Function UnregisterHotKey Lib "user32 " (ByVal hWnd As IntPtr, ByVal id As Integer) As Integer
Private Key_Alt = 1
Private Key_Control = 2
Private Key_Shift = 4
Public Sub main(ByVal args As String())
Dim frm As New Form1
Dim hwnd As IntPtr = frm.Handle
RegisterHotKey(hwnd, 11112, Key_Control Or Key_Shift, Keys.B)
context = New ApplicationContext(frm)
Application.AddMessageFilter(New TestMessageFilter)
Application.Run(context)
UnregisterHotKey(hwnd, 11112)
End Sub

Public Class TestMessageFilter
Implements IMessageFilter
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage
Select Case m.WParam.ToInt32
Case 11112
context.MainForm.Visible = Not context.M