C# 设置鼠标光标位置

Wesley13
• 阅读 566

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);
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
Linux终端Terminal常用快捷键
快捷键功能CTRLALTT打开终端Ctrla光标移动到开始位置Ctrle光标移动到最末尾Ctrlk删除此处至末尾的所有内容Ctrlu删除此处至开始的所有内容Ctrld删除当前字符Ctrlh删除当前字符前一个字符Ctrlw删除此处到左边的单词Ctrly粘贴由Ctrl
Stella981 Stella981
2年前
Flutter TextField设置默认值默认值和光标位置
主要通过controller实现,具体代码如下TextField(          //输入键盘类型keyboardType:TextInputType.text,autofocus:true,decoration:Inpu
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
Unity 利用射线实现弹孔效果
1、利用 Camera.main.ScreenPointToRay从屏幕到鼠标点击位置生成一条射线。2、利用 Physics.Raycast 发射射线,同时判断是否碰撞到目标物体。3、通过 RaycastHit获取射线所碰撞到的位置。4、在目标位置生成一个弹孔预设体。 具体代码:using UnityEngine;
Stella981 Stella981
2年前
Deepin重置管理员密码
Debian系重置管理员密码的指南网络上很多,这里针对Deepin列出步骤。关键是第3步,要在正确的位置输入内容。1.引导界面,按下“e”2.移动光标到包含“rosplashquiet”的行3.按下“Ctrle”,光标移动到行末,再把光标移动到“quiet”的后面4.按下空格键,输入“init/bin/bash”5
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
Unity中鼠标拖动物体移动的算法
鼠标拖动物移动在游戏设计上用的非常多,像飞机射击游戏,我来分享下我的思路:  我的思路: 因为鼠标的屏幕坐标是二维的,物体是世界坐标,三维的。首先将鼠标的屏幕坐标转换为三维的世界坐标,这样为了计算鼠标与物体位置之间的距离,再将移动后的鼠标的屏幕坐标转换为世界坐标,加上之前的距离量,将和值赋给物体坐标,就可以实现了。C算法实现:IEnumera
凿壁偷光 凿壁偷光
1年前
RapidClick for mac(鼠标自动点击软件)v1.5.1激活版
RapidClickforMac是一款鼠标自动点击软件,它可以帮助用户自动点击鼠标并设置点击速度和间隔时间。RapidClickforMac非常容易使用,只需下载并运行应用程序,然后按照简单的说明进行设置即可。
子桓 子桓
6个月前
文本识别软件分享 TextSniper 免激活最新介绍
TextSniper是一款Mac上的OCR(光学字符识别)应用程序,它可以帮助用户快速准确地提取文本信息。以下是TextSniper的一些优点:易于使用:TextSniper提供了简洁直观的界面,使用户能够轻松上手。只需将鼠标光标放在要提取文本的位置,按下