Hi @Việt Trần , Welcome to Microsoft Q&A,
You can achieve this function through the MouseClick event of NotifyIcon. When the NotifyIcon is clicked, you can get the position of the mouse and set the position of the application UI accordingly.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xxx
{
public partial class Form1 : Form
{
private NotifyIcon notifyIcon;
public Form1()
{
InitializeComponent();
InitializeNotifyIcon();
}
private void InitializeNotifyIcon()
{
notifyIcon = new NotifyIcon();
notifyIcon.Icon = SystemIcons.Information;
notifyIcon.Text = "NotifyIcon Demo";
notifyIcon.Visible = true;
notifyIcon.MouseClick += NotifyIcon_MouseClick;
}
private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Get the current position of the mouse
Point mousePosition = Control.MousePosition;
// If the form is minimized, restore the form display
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
//Display the form above notifyIcon
this.Location = new Point(mousePosition.X - this.Width / 2, mousePosition.Y - this.Height);
this.StartPosition = FormStartPosition.Manual;
this.Show();
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.