VB.Net程序最小化不起作用

时间:2014-11-01 16:03:02

标签: vb.net system-tray

我有一个如下代码

 Private Sub InterfaceProg_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    'program minimized
    Try
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Visible = True
            NotifyIcon1.Visible = True
            NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

代码工作正常,但有一个问题。 当我按下右上角的最小化按钮时,程序将进入系统托盘 当我按下关闭按钮时,程序也进入系统也进入系统托盘。

如果用户只按下关闭按钮,我想让程序转到系统托盘,如果用户按最小化,则将程序最小化到任务栏。怎么做?

3 个答案:

答案 0 :(得分:1)

使用FormClosing事件而不是Resize事件:

Private CloseAllowed As Boolean

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    If Not CloseAllowed And e.CloseReason <> CloseReason.WindowsShutDown Then
        Me.Hide()
        e.Cancel = True
        NotifyIcon1.Visible = True
        '' etc..
    End If
End Sub

您仍然需要为用户提供退出程序的方法。 NotifyIcon的上下文菜单是常用方法。添加退出项目:

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    CloseAllowed = True
    Me.Close()
End Sub

答案 1 :(得分:0)

然后你需要调用你的Code of&#34; InterfaceProg_Resize&#34;在甚至关闭的形式 因此,制作一个新的子&#34; Private Sub ProgToTray()&#34;

你也不需要尝试捕获它 您还需要隐藏任务栏图标&#34; Me.ShowInTaskbar = False&#34;
并最小化形式&#34; Me.WindowState = FormWindowState.Minimized&#34;

Private Sub ProgToTray()

    Me.ShowInTaskbar = False
    Me.WindowState = FormWindowState.Minimized
    NotifyIcon1.Visible = True
    NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)

End Sub

如果用户在表单上单击x,则需要在FormClosing事件中取消关闭表单。&#34; e.Cancel = True&#34;
然后你打电话给#34; ProgToTray()&#34;

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

    e.Cancel = True
    ProgToTray()

End Sub

然后,如果您使用x关闭表单,它将转到通知区域。

请勿忘记点击事件再次从通知图标和选项中打开表单,以便用户退出表单。

要制作退出按钮,您需要在表单中添加ContextMenuStrip1 enter image description here

要在右键单击NotifyIcon1时打开ContextMenuStrip1,需要添加代码&#34; ContextMenuStrip1.Show(Cursor.Position)&#34; NotifyIcon1点击事件

您还需要检查点击了哪个按钮 &#34;如果e.Button = Windows.Forms.MouseButtons.Right那么&#34;对于右键。

&#34; e.Button = Windows.Forms.MouseButtons.Left然后&#34;对于左键。

因此,如果用户点击左侧,则表单将再次打开,当他单击右键时,将显示ContextMenuStrip1。

如果用户单击鼠标左键,则将formwindowstate设置为正常 同时再次显示任务栏图标并隐藏托盘图标。

    Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick

    If e.Button = Windows.Forms.MouseButtons.Right Then
        ContextMenuStrip1.Show(Cursor.Position)
    ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
        Me.WindowState = FormWindowState.Normal
        Me.ShowInTaskbar = True
        NotifyIcon1.Visible = False
    End If

End Sub

然后为退出按钮创建一个click事件,并删除表单关闭事件的处理程序&#34; RemoveHandler MyBase.FormClosing,AddressOf Form1_FormClosing&#34;。
取消表格将不会取消。 然后打电话给me.close()

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing
    Me.Close()
End Sub

答案 2 :(得分:0)

当您按下minimizeclose按钮时,会发送 WM_SYSCOMMAND 消息。 WPARAM 指定按下哪个按钮:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = &H112 Then 'WM_SYSCOMMAND 
        If CInt(m.WParam) = &HF060 Then 'SC_CLOSE, the close button is pressed
            Me.Visible = False
            Me.ShowInTaskbar = False

            Return 'cancel the message
        End If

        If CInt(m.WParam) = &HF020 Then 'SC_MINIMIZE, the minimize button is pressed
            'do your staff

        End If
    End If

    MyBase.WndProc(m)
End Sub

WM_SYSCOMMAND message