2014-11-01 79 views
2

我有一個像下面VB.Net程序最小化不工作

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 

代碼工作正常代碼,但有一個問題。 當我按右上角的最小化按鈕,程序進入系統托盤 當我按下關閉按鈕,程序也去系統也去系統托盤。

我想讓程序進入系統托盤,如果用戶只按下關閉按鈕,並最小化程序任務欄,如果用戶按最小化。怎麼做?

回答

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 
0

然後,您需要在表單中關閉甚至調用您的「InterfaceProg_Resize」代碼。
因此,使一個新的子「Private Sub ProgToTray()」

也你不需要嘗試趕上它。
您還需要隱藏任務欄圖標「Me.ShowInTaskbar =假」
並最小化形式「Me.WindowState = FormWindowState.Minimized」

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事件。「e.Cancel =真」
然後調用「ProgToTray()」

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

要打開ContextMenuStrip1當你右鍵點擊將NotifyIcon1你需要添加代碼「ContextMenuStrip1.Show(Cursor.Position)」你將NotifyIcon1單擊事件

您還需要檢查被點擊的按鈕。
「如果e.Button = Windows.Forms.MouseButtons.Right然後」爲正確的按鈕。

「e.Button = Windows.Forms.MouseButtons.Left Then」爲左側按鈕。

因此,如果用戶點擊左側,表單將再次打開,當他點擊右側時,將顯示ContextMenuStrip1。

如果用戶單擊鼠標左鍵,將窗體狀態設置恢復正​​常。
再次顯示任務欄圖標並隱藏托盤圖標。

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 

然後你做出了退出按鈕的點擊事件,並刪除了形式關閉事件「RemoveHandler MyBase.FormClosing,AddressOf Form1_FormClosing」的處理程序。
取消關閉表格不會發生。 然後給我打電話。接近()

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

他將如何關閉他的表格呢? – 2014-11-01 18:47:55

+0

忘了發佈該部分。我現在把它寫在我的答案中。 – Creator 2014-11-01 21:35:42

+0

thx ..它完美的工作。只是一件事,但我已經解決了它。當我使用me.close()程序仍然運行在任務欄事件中時,程序似乎很接近。目前我使用「結束」在我的電腦上的toolstripmenu – user2364790 2014-11-02 16:56:59

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