[Yugong Series] Detailed Explanation of NotifyIcon Control on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of NotifyIcon control

NotifyIcon is one of the commonly used controls in Winform. It is usually used to display an icon in the system tray so that users can easily access the relevant functions of the program. The following are some commonly used NotifyIcon control properties and methods:

Attributes:

  1. Icon: Set or get the icon displayed by the NotifyIcon control in the system tray.
  2. Text: Specify the text displayed when the NotifyIcon control mouse hovers.
  3. Visible: Specifies whether the NotifyIcon control is visible.

method:

  1. ShowBalloonTip: Display a bubble tip in the system tray, you can set properties such as title, text and icon.
  2. HideBalloonTip: Hide the currently displayed bubble tips.
  3. ShowContextMenu: Shows the specified menu in the system tray context menu.

Using the NotifyIcon control usually requires the following steps:

  1. Create a new NotifyIcon control and set related properties.
  2. Write event handlers to respond to various events of the NotifyIcon control, such as mouse click, double click, right-click menu, etc.
  3. Add a NotifyIcon control to the system tray in the form's Load event.
  4. When the program exits, be sure to remove the NotifyIcon control from the system tray.
private void button1_Click(object sender, EventArgs e)
{
    
    
    notifyIcon1.Visible = true;
    notifyIcon1.Icon = SystemIcons.Exclamation;
    notifyIcon1.BalloonTipTitle = "Balloon Tip Title";
    notifyIcon1.BalloonTipText = "Balloon Tip Text.";
    notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;

    notifyIcon1.ShowBalloonTip(30000);
}

insert image description here

1. Attribute introduction

1.1 BalloonTipIcon

The NotifyIcon control is a commonly used system tray control in Winform. It can display an icon in the icon area on the right side of the taskbar of the Windows system, and pop up a menu or prompt when the user clicks the icon.

The BalloonTipIcon property of the NotifyIcon control is used to set the icon displayed in the pop-up prompt box, and its value can be one of the following enumeration types:

  • None: No icon is displayed.
  • Info: Displays the info icon (a blue circle with a white i).
  • Warning: Displays the warning icon (a yellow triangle and a black exclamation point).
  • Error: Displays the error icon (a red circle and a white X).

The following is a sample code that demonstrates how to set the BalloonTipIcon property of the NotifyIcon control:

// 设置NotifyIcon控件的BalloonTipIcon属性为Info
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;

// 设置NotifyIcon控件的BalloonTipIcon属性为Warning
notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;

// 设置NotifyIcon控件的BalloonTipIcon属性为Error
notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;

In the above sample code, we can see how to display different icons by setting the BalloonTipIcon property of the NotifyIcon control. Among them, we use the ToolTipIcon enumeration type and assign it as the value of the BalloonTipIcon property.

1.2 BalloonTipText and BalloonTipTitle and Icon

The NotifyIcon control is a control commonly used in Winform, which is used to display icons and prompt information in the system tray. Among them, BalloonTipText, BalloonTipTitle and Icon properties are important properties of the control.

  1. BalloonTipText Property

This attribute is used to set the prompt information in the pop-up prompt box of BalloonTip. It can be set by the following code:

notifyIcon1.BalloonTipText = "这是提示信息";
  1. BalloonTipTitle attribute

This property is used to set the title of the BalloonTip pop-up prompt box. It can be set by the following code:

notifyIcon1.BalloonTipTitle = "提示标题";
  1. Icon property

This property is used to set the icon displayed by the NotifyIcon control in the system tray. It can be set by the following code:

notifyIcon1.Icon = new Icon("icon.ico");

where icon.ico is the path to the icon file.

It should be noted that after the above properties are set, the ShowBalloonTip method needs to be called to display the BalloonTip pop-up prompt box in the system tray. The sample code is as follows:

notifyIcon1.ShowBalloonTip(5000);

The parameter 5000 in the above code means that the display time of the prompt box is 5000 milliseconds.

1.3 Tag and Text and Visible

The NotifyIcon control is a notification icon control in Windows Forms, which can display an icon in the system tray to notify users of certain events. Its common properties are as follows:

  1. Tag property: Tag property can be used to store some control-related data, which can be accessed and modified through programming. For example, you can set the Tag property to an object, and then use this object in the control's event handler to perform some operations.

  2. Text property: The Text property is used to display the text information in the ToolTip, and this text information will be displayed when the user hovers the mouse over the icon. The value of this attribute can be set according to actual needs.

  3. Visible attribute: The Visible attribute is used to control the visibility of the control. If it is set to true, the control will be displayed in the system tray, otherwise it will not be displayed.

Here is a sample code using the NotifyIcon control:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    //创建NotifyIcon控件
    NotifyIcon notifyIcon = new NotifyIcon();
    notifyIcon.Tag = "一些与控件相关的数据";
    notifyIcon.Text = "这是ToolTip中的文本信息";
    notifyIcon.Icon = new Icon("icon.ico");
    notifyIcon.Visible = true;

    //为NotifyIcon控件注册鼠标事件
    notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_MouseClick);
}

private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
    
    
    //处理鼠标点击事件
    //...
}

In the above code, we first created a NotifyIcon control, and then set its Tag, Text and Visible properties. Finally, we register a mouse event handler for the NotifyIcon control, which is triggered when the user clicks or hovers over the icon, and handles accordingly.

1.4 ContextMenuStrip

The NotifyIcon control is a commonly used control in Windows Forms, which can display an icon in the system tray, and can also provide users with more operation options through the right-click menu. ContextMenuStrip is a property of the NotifyIcon control, which can specify a set of menu items for the right-click menu of the control.

The specific usage method is as follows:

  1. In the design window, drag and drop a NotifyIcon control onto the form.

  2. Find the ContextMenuStrip property of the NotifyIcon control in the Properties window, and click the selector.

  3. In the menu designer that pops up, you can add required menu items and submenu items.

  4. Add Click event handlers for menu items to respond to user actions.

  5. In the program code, the bubble tip can be displayed through the ShowBalloonTip method of the NotifyIcon control, and the right-click menu can be displayed through the ContextMenuStrip property.

Below is a sample code that demonstrates how to use the ContextMenuStrip property in the NotifyIcon control.

private void Form1_Load(object sender, EventArgs e)
{
    
    
    // 设置NotifyIcon控件的图标和提示信息
    notifyIcon1.Icon = new Icon("icon.ico");
    notifyIcon1.Text = "NotifyIcon示例程序";
    
    // 创建右键菜单项
    ContextMenuStrip menu = new ContextMenuStrip();
    // 添加菜单项
    menu.Items.Add("打开窗口");
    menu.Items.Add("退出程序");

    // 设置菜单项的Click事件处理程序
    menu.Items[0].Click += new EventHandler(OpenWindow_Click);
    menu.Items[1].Click += new EventHandler(Exit_Click);
    
    // 设置NotifyIcon控件的右键菜单
    notifyIcon1.ContextMenuStrip = menu;
}

// 打开窗口菜单项的Click事件处理程序
void OpenWindow_Click(object sender, EventArgs e)
{
    
    
    this.WindowState = FormWindowState.Normal;
}

// 退出程序菜单项的Click事件处理程序
void Exit_Click(object sender, EventArgs e)
{
    
    
    Application.Exit();
}

In the above code, we set some basic properties of the NotifyIcon control through the Form1_Load method, including icon and prompt information. Then created a ContextMenuStrip object and added two menu items to it. Finally, assign the ContextMenuStrip object to the ContextMenuStrip property of the NotifyIcon control. At the same time, we added a handler to the Click event of each menu item, respectively realizing the functions of opening the window and exiting the program.

2. Common scenarios

The NotifyIcon control is a common small icon control in Winform, usually used in the following scenarios:

  1. System Tray Icon: Minimizes the application to the system tray so that users can quickly access the application when needed.

  2. Prompt message: When the application needs to notify the user of some information, a balloon prompt or flashing icon can be displayed in the system tray through this control.

  3. Quick actions: Users can quickly perform some common actions by clicking controls, such as switching music, pausing playback, etc.

  4. Status monitoring: Display the running status of the application through the control, such as network connection status, CPU usage, etc.

  5. Menu operation: The user can pop up a menu by right-clicking the control to perform corresponding operations.

The NotifyIcon control can provide a simple, intuitive, and effective notification and operation method for applications.

3. Specific cases

The following is a specific case of NotifyIcon control in Winform:

First, place a NotifyIcon control on the form and set its Icon property to the application icon. Then, add the following code to the Form_Load event:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    notifyIcon1.Visible = true;
    notifyIcon1.Text = "这是一个NotifyIcon控件的案例";
    notifyIcon1.BalloonTipTitle = "提示";
    notifyIcon1.BalloonTipText = "您的应用程序已最小化到托盘";
    notifyIcon1.Icon = this.Icon;
}

In the window minimize event, hide the form and display the prompt information in the NotifyIcon's BalloonTip:

private void Form1_SizeChanged(object sender, EventArgs e)
{
    
    
    if (this.WindowState == FormWindowState.Minimized)
    {
    
    
        this.Hide();
        notifyIcon1.ShowBalloonTip(1000);
    }
}

When the user clicks the NotifyIcon, the form is displayed:

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

When the user right-clicks the NotifyIcon, a context menu is displayed:

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
    
    
    if (e.Button == MouseButtons.Right)
    {
    
    
        ContextMenu menu = new ContextMenu();
        menu.MenuItems.Add(new MenuItem("退出", new EventHandler(MenuItemExit_Click)));
        notifyIcon1.ContextMenu = menu;
        menu.Show(notifyIcon1, e.Location);
    }
}

private void MenuItemExit_Click(object sender, EventArgs e)
{
    
    
    Application.Exit();
}

In the form closing event, the form needs to be hidden first, otherwise the NotifyIcon will still exist in the tray after the form is closed:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    
    
    if (this.WindowState != FormWindowState.Minimized)
    {
    
    
        this.Hide();
        e.Cancel = true;
    }
}

This is a simple case of the NotifyIcon control, through which you can understand the basic usage of the NotifyIcon control.

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132707610