zoukankan      html  css  js  c++  java
  • C# 设置鼠标光标位置

    C# 设置鼠标光标位置

    using System.Drawing;
    using System.Runtime.InteropServices;
    
    namespace ZB.QueueSys.Common
    {
        public class MouseHelper
        {
            private static MouseHelper instance;
            public static MouseHelper Instance
            {
                get
                {
                    if (instance == null) instance = new MouseHelper();
                    return MouseHelper.instance;
                }
            }
    
            /// <summary>
            /// 引用user32.dll动态链接库(windows api),
            /// 使用库中定义 API:SetCursorPos 
            /// </summary>
            [DllImport("user32.dll")]
            private static extern int SetCursorPos(int x, int y);
            /// <summary>
            /// 移动鼠标到指定的坐标点
            /// </summary>
            public void MoveMouseToPoint(Point p)
            {
                SetCursorPos(p.X, p.Y);
            }
            /// <summary>
            /// 设置鼠标的移动范围
            /// </summary>
            public void SetMouseRectangle(Rectangle rectangle)
            {
                System.Windows.Forms.Cursor.Clip = rectangle;
            }
            /// <summary>
            /// 设置鼠标位于屏幕中心
            /// </summary>
            public void SetMouseAtCenterScreen()
            {
                int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
                int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
                Point centerP = new Point(winWidth / 2, winHeight / 2);
                MoveMouseToPoint(centerP);
            }
    
        }
    }
    
    
    调用测试如下:
                int y = Screen.PrimaryScreen.WorkingArea.Height - 180;
                int x = Screen.PrimaryScreen.WorkingArea.Width - 180;
                Point p = new Point(x, y);
                MouseHelper.Instance.MoveMouseToPoint(p);    
    

    C#获取指定控件所在屏幕的位置

             int x = this.dgvList.Location.X;
                int y = this.dgvList.Location.Y;
                Point p = new Point(x, y);
                Point pp = this.dgvList.PointToScreen(p);
                MouseHelper.Instance.MoveMouseToPoint(pp);
    

      

      

  • 相关阅读:
    最近在项目中使用ibatis小结
    35 岁前程序员要规划好的四件事
    C# webbrowser小结
    高并发网站架构
    高斯混合模型(GMM)
    EM算法学习(Expectation Maximization Algorithm)
    如何用CSS3美化菜单
    Intellij IDEA配置自动同步到FTP服务器
    Mac 快速休眠关机重启锁屏
    JavaScript并行运算新机遇——Web Workers的神奇魔法
  • 原文地址:https://www.cnblogs.com/YYkun/p/12054381.html
Copyright © 2011-2022 走看看