Home > Article > Backend Development > Detailed introduction to realizing the tray effect in C#
In the development of desktop programs, it is often considered to implement tray shortcut operations on the status bar. The implementation of tray programs is still relatively complicated in the API era. First, you can view its functions in MSDN details,
and then make complex settings according to the parameters it requires.
In the .NET era, MS encapsulates these troublesome details, and developers can easily control and operate the tray program. Now take a simple example as an example to illustrate:
1. Add notifyIcon control and contextMenuStrip control
2. Set its properties icon and contextMenuStrp properties. For the Icon property, you only need to select a tray icon, and contextMenuStrip is used for the first step of association. shortcut menu.
3. Add the SizeChanged event program of the form to call the tray when the form is minimized:
if (this.WindowState == FormWindowState.Minimized) { this.Hide(); //可以要,也可以不要,取决于是否隐藏主窗体 this.notifyIcon.Visible = true; }
4. Add the Click event of notifyIcon to open the shortcut menu item, or other changes Complex operating options, as needed.
private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); this.notifyIcon1.Visible = true; } } private void notifyIcon1_Click(object sender, EventArgs e) { this.contextMenuStrip1.Show(MousePosition.X,MousePosition.Y); }