开发者

C# Creating A Program That Runs In The Background? [duplicate]

开发者 https://www.devze.com 2022-12-31 20:19 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: What's the proper way to minimize to tray a C# WinForms app?
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

What's the proper way to minimize to tray a C# WinForms app?

How can I create a program that runs in the background, and can be accessed via the Windows' "Notification Area" (Where the date and time are in the lower right hand corner)?

In other words, I want to be able to create a pro开发者_JAVA技巧gram that runs and can toggle between having a display window and not having a display window.


  1. Drag and drop a NotifyIcon and a ContextMenuStrip.
  2. Set de NotifyIcon's context menu to the one you added
  3. Add 2 menuitems (e.g. Restore, Exit)
  4. Set the Form event resize and do the following check

    private void MyForm_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized) this.Hide();
        else this.Show();
    }
    
    // you could also restore the window with a
    // double click on the notify icon
    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
    }
    

For a example can download this project

Don't worry about the right click event, the NotifyIcon will automatically detect it and show the ContextMenu

0

精彩评论

暂无评论...
验证码 换一张
取 消