C#判断鼠标是否在自己程序的NotifyIcon上

    /// <summary>
    /// 程序托盘区图标位置
    /// </summary>
    public class IconInfo
    {
        /// <summary>
        /// 托盘区句柄
        /// </summary>
        public IntPtr hWnd = IntPtr.Zero;
        /// <summary>
        /// 图标X坐标
        /// </summary>
        public int X;
        /// <summary>
        /// 图标Y坐标
        /// </summary>
        public int Y;
        /// <summary>
        /// 图标宽度
        /// </summary>
        public int Width;
        /// <summary>
        /// 图标高度
        /// </summary>
        public int Height;
        public bool InIconRect(Point p)
        {
            if (p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height)
            {
                return true;
            }
            return false;
        }
    }

在图标的MouseMove事件中加入,用来确定图标位置信息

        GetIconInfo();


<pre name="code" class="csharp">        //获取图标位置信息
        private void GetIconInfo()
        {
            Point p = Cursor.Position;
            MyIcon.hWnd = WindowFromPoint(p);
            int BtnSize = SendMessage(MyIcon.hWnd, TB_GETBUTTONSIZE, IntPtr.Zero, IntPtr.Zero);
            MyIcon.Width = BtnSize & 0xFFFF;
            MyIcon.Height = (BtnSize / 0x10000) & 0xFFFF;
            Rect rect = new Rect();
            if (GetWindowRect(MyIcon.hWnd, out rect))
            {
                if (MyIcon.Width != 0 && MyIcon.Height != 0)
                {
                    MyIcon.X = (p.X - rect.Left - py) / MyIcon.Width * MyIcon.Width + rect.Left + py;
                    MyIcon.Y = (p.Y - rect.Top - py) / MyIcon.Height * MyIcon.Height + rect.Top + py;
                }
            }
        }

 

所用到的变量和函数

        /// <summary>
        /// 不同操作系统偏移量
        /// </summary>
        private int py = 0;

        private const int WM_USER = 0x400;
        private const int TB_GETBUTTONSIZE = WM_USER + 58;
        private const int WM_SYSCOMMAND = 0x112;
        private const long SC_MINIMIZE = 0xF020;
        private const int WM_MOUSEMOVE = 0X0200;
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hwnd, out Rect lpRect);
        [DllImport("User32.dll")]
        private static extern IntPtr WindowFromPoint(Point p);
        /// <summary>
        /// 图标信息
        /// </summary>
        private IconInfo MyIcon = new IconInfo();

        // WIN7之后的系统有两像素偏移量 程序load时加入以下代码
 
           OperatingSystem os = Environment.OSVersion;
            if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 6)
            {
                py = 2;
            }




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值