当前位置: 首页 > news >正文

【Csharp】获取实时的鼠标光标位置,测试用——做窗口软件绘图需要确定光标位置

前言

我的爱沉重,污浊,里面带有许多令人不快的东西,比如悲伤,忧愁,自怜,绝望,我的心又这样脆弱不堪,自己总被这些负面情绪打败,好像在一个沼泽里越挣扎越下沉。——《挪威的森林》

在这里插入图片描述


         \;\\\;\\\;

目录

    • 前言
    • Cursor

Cursor

把窗口放在左下角,这样在运行测试别的窗口程序的时候,就可以实时显示位置了。
在这里插入图片描述

using static System.Net.Mime.MediaTypeNames;

namespace CursorPoint
{
    public partial class Form1 : Form
    {

        private System.Windows.Forms.Timer timer; // 声明 Timer 控件



        public Form1()
        {
            InitializeComponent();

            //居中
            //this.CenterToScreen();


            // 获取主屏幕的实际尺寸
            Rectangle b = Screen.PrimaryScreen.Bounds;
            // 计算窗体应该放置的位置
            int x = 0; // 屏幕的左边缘
            int y = b.Height - this.Height; // 屏幕高度减去窗体高度

            // 设置窗体位置
            this.Location = new Point(x, y);

            // 输出计算结果
            Console.WriteLine($"Screen bounds: ({b.Width}, {b.Height})");
            Console.WriteLine($"Form size: ({this.Width}, {this.Height})");
            Console.WriteLine($"New form location: ({x}, {y})");

            /*
            窗口计算都是对的,但是Location设置位置一直不对!窗口还跳动
            要把StartPosition,改成Manual!!!这样就没问题了!
             */




            //置顶
            this.TopMost = true;


            //定时器设置
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 100; //ms
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Start(); // 开始计时
        }



        //这个只有鼠标到窗口上才起作用
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            UpdateCursorPoint();
        }


        //实时显示鼠标位置,核心函数
        private void UpdateCursorPoint()
        {
            // 获取全局的鼠标位置并更新 Label
            Point globalCursorPosition = Cursor.Position;
            string text = $"({globalCursorPosition.X}, {globalCursorPosition.Y})";

            // 更新 Label 的文本
            label1.Text = text;
        }



        private void Timer_Tick(object sender, EventArgs e)
        {
            UpdateCursorPoint();
        }

    }
}

         \;\\\;\\\;

相关文章:

  • GenerationMixin概述
  • Python Cookbook-5.5 根据内嵌的数字将字符串排序
  • 清明假期间
  • 数据分析-Excel-学习笔记
  • AI大模型:(二)2.1 从零训练自己的大模型概述
  • 【LeetCode 热题100】55:跳跃游戏(详细解析)(Go语言版)
  • 用python来操作mysql(复习一,主要是mysql连接和授权)
  • 【清明折柳】写在扬马三周目后
  • 【NLP 54、大模型训练相关知识】
  • Sentinel核心源码分析(上)
  • InfoSec Prep: OSCP靶场渗透
  • 定义和初始化 vector 对象(三十八)
  • TypeScript学习第十六篇 - interface和type的区别?
  • 【在线OJ项目测试报告】
  • 第十章Python语言高阶加强-SQL(数据库)
  • windows AndroidStudio上传maven中央仓库
  • MySQL约束详解:从入门到精通
  • java.util.Collections中常用api
  • Systemd安全加密备份系统与智能通知
  • 深信服Python开发面经及参考答案(110道题)
  • 郑州做网站的联系方式/惠州seo建站
  • 淘宝禁止了网站建设类/seo优化是啥
  • 网站建设 企炬江阴/正版google下载
  • 自己做的网站套dedecms教程/重庆快速网络推广
  • 网站空间租/网站里的友情链接
  • 嵩县网站开发/荆门网络推广