How to get the position of NotifyIcon at runtime?

Việt Trần 0 Reputation points
2024-05-15T04:06:34.3866667+00:00

Hi, I want to retrieve the location and size of notifyIcon whenever it be clicked. The purpose is to set the application UI above the notifyIcon like the below photo.

User's image

Please show me how I can do this with C#.

Thank you!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,858 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,479 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 38,021 Reputation points Microsoft Vendor
    2024-05-15T09:35:02.5166667+00:00

    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.


  2. KOZ6.0 5,375 Reputation points
    2024-05-16T06:27:07.9966667+00:00

    I was able to catch the window with WindowFromPoint, but the size I get with GetWindowRect seems to be larger than what is displayed.

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    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;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    
        [DllImport("user32.dll")]
        private static extern IntPtr WindowFromPoint(Point Point);
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    
        private void NotifyIcon_MouseClick(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Left) {
                var hwnd = WindowFromPoint(MousePosition);
                if (hwnd != IntPtr.Zero) {
                    if (GetWindowRect(hwnd, out RECT rect)) {
                        var f = new Form {
                            StartPosition = FormStartPosition.Manual,
                            TransparencyKey = BackColor,
                            ControlBox = false,
                            Text = string.Empty,
                            FormBorderStyle = FormBorderStyle.FixedSingle,
                            Bounds = Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom)
                        };
                        f.Show();
                    }
                }
            }
        }
    }
    

    enter image description here