2012-11-29 36 views
11

Xin chào Trong ứng dụng C# của tôi, tôi đang cố gắng thu nhỏ ứng dụng vào khay hệ thống, khi biểu mẫu được đóng lại. Đây là mã tôi đã thử.C# Thu nhỏ khay hệ thống ở gần

public void MinimizeToTray() 
    { 
     try 
     { 
      notifyIcon1.BalloonTipTitle = "Sample text"; 
      notifyIcon1.BalloonTipText = "Form is minimized"; 

      if (FormWindowState.Minimized == this.WindowState) 
      { 
       notifyIcon1.Visible = true; 
       notifyIcon1.ShowBalloonTip(500); 
       this.Hide(); 
      } 
      else if (FormWindowState.Normal == this.WindowState) 
      { 
       notifyIcon1.Visible = false; 
      } 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

và tôi đang gọi phương thức để tạo sự kiện đóng. Nhưng vấn đề là nó không giảm thiểu đến khay. Nó chỉ đóng hình thức.

+0

Khi biểu mẫu đang được đóng hoặc thu nhỏ? – Danpe

+0

Bạn cũng đang hủy/dừng sự kiện đóng? Mã này sẽ không giúp ích nhiều nếu ứng dụng bị đóng sau khi thực hiện điều này. –

+0

Khi đóng cửa. – Rakesh

Trả lời

10

Viết một sự kiện trong trường hợp bế Mẫu.

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{ 
     e.Cancel = true;       
     Hide(); 
} 

Và viết bằng cách sử dụng Dải menu tùy chỉnh để biểu tượng thông báo hiển thị.

+3

cần thêm dòng trong hàm tạo: this.FormClosing + = this.Form1_FormClosing; – temple

1

Bạn nên hủy sự kiện FormClosing và sau đó gọi hàm MinimizeToTray().

Điều này được thực hiện thông qua thuộc tính Cancel của FormClosingEventArgs.

Ngoài ra, xem xét sử dụng một nơi nào đó để boolphép đóng Form trong một số điều kiện, chẳng hạn như nếu bạn đang sử dụng một trình đơn File > Exit hoặc một cái gì đó:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if(!allowClosing) 
    { 
     e.Cancel = true; 
     MinimizeToTray(); 
    } 
} 
+0

Thay vì sử dụng allowClosing, bạn có thể chọn thời điểm đóng bằng FormClosingEventArgs: nếu (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; } –

0

Bạn cần sử dụng FormClosing-Event.

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) { 
    e.Cancel = true; 
    MinimizeToTray(); 
} 
29

e.Cancel = true; mã sẽ luôn hủy sự kiện này ngay cả khi bạn tắt máy tính xuống, nhưng đây là một mã giúp bạn:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if (e.CloseReason == CloseReason.UserClosing) 
    { 
      myNotifyIcon.Visible = true; 
      this.Hide(); 
      e.Cancel = true; 
    } 
} 

Nó sẽ cho phép đóng cửa dưới dạng programmaticaly.

+1

Giải pháp này tốt hơn nhiều, vì giải pháp của ** arun kumar non ascii ** là một câu chuyện không bao giờ kết thúc: D – Ismoh

+0

tôi đã trải qua nếu bạn không đặt hình ảnh biểu tượng của thông báo, thì nó không được hiển thị trong khay hệ thống trong cửa sổ 8.1. – Fer

1

Để giảm thiểu khi bế mạc thiết windowState để Giảm thiểu

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) { 
    e.Cancel = true; 
    WindowState = FormWindowState.Minimized; 
} 
2
namespace MinimizeTrayNotification 
    { 
     public partial class Form1 : Form 
     { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void MinimzedTray() 
    { 
     notifyIcon1.Visible = true; 
     notifyIcon1.Icon = SystemIcons.Application; 

     notifyIcon1.BalloonTipText = "Minimized"; 
     notifyIcon1.BalloonTipTitle = "Your Application is Running in BackGround"; 
     notifyIcon1.ShowBalloonTip(500); 

    } 

    private void MaxmizedFromTray() 
    { 
     notifyIcon1.Visible = true; 
     notifyIcon1.BalloonTipText = "Maximized"; 
     notifyIcon1.BalloonTipTitle = "Application is Running in Foreground"; 
     notifyIcon1.ShowBalloonTip(500); 


    } 



    private void Form1_Resize(object sender, EventArgs e) 
    { 
     if(FormWindowState.Minimized==this.WindowState) 
     { 
     MinimzedTray(); 
     } 
     else if (FormWindowState.Normal == this.WindowState) 
     { 

      MaxmizedFromTray(); 
     } 
     } 

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     this.WindowState = FormWindowState.Normal; 
     Form1 frm = new Form1(); 
     frm.Show(); 
     MaxmizedFromTray(); 


    } 

    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) 
    { 
     this.WindowState = FormWindowState.Normal; 
    } 

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     if (e.CloseReason == CloseReason.UserClosing) 
     { 
      e.Cancel = true; 
      this.WindowState = FormWindowState.Minimized; 
      this.ShowInTaskbar = false; 
      this.Hide(); 

     } 


    } 

    private void notifyIcon1_Click(object sender, EventArgs e) 
    { 
     this.WindowState = FormWindowState.Normal; 
     notifyIcon1.BalloonTipText = "Normal"; 
     notifyIcon1.ShowBalloonTip(500); 
    } 
} 

}

+1

Tôi chắc chắn mã này sẽ hoạt động, nhưng nó sẽ hữu ích hơn với một hoặc hai bình luận về những gì đang diễn ra! – leo

+0

cho notifyIcon1_MouseDoubleClick, bạn nên sử dụng 'this.Show()' thay vì tạo một thể hiện mới của biểu mẫu. Bạn có thể muốn điều khiển biểu mẫu của mình để duy trì giá trị của chúng. – prospector

0

Bạn có thể xử lý FormClosing tổ chức sự kiện như micsoft Form Closing Event như sau ví dụ về C#

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
    { 
     // Determine if text has changed in the textbox by comparing to original text. 
     if (textBox1.Text != strMyOriginalText) 
     { 
      // Display a MsgBox asking the user to save changes or abort. 
      if (MessageBox.Show("Do you want to save changes to your text?", "My Application", 
       MessageBoxButtons.YesNo) == DialogResult.Yes) 
      { 
       // Cancel the Closing event from closing the form. 
       e.Cancel = true; 
       // Call method to save file... 
      } 
     } 
}