回 帖 发 新 帖 刷新版面

主题:处理鼠标事件(获取x y坐标怎么错了啊)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace mouseevent
{
    

    public partial class Form1 : Form
    {
        private bool tracking = false;
        string coordinateDisplay = null;
        
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            tracking=true;

           
        }

        private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            tracking = false;
            MouseCoordinateDisplay.Clear();
        }

        private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (tracking)
            {

                coordinateDisplay = "X: " + e.X + "Y: " + e.Y;
                MouseCoordinateDisplay.Text = coordinateDisplay;
            }
        }

        public void Main()
        {
            

            Form1 a=new Form1();
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }

        private void MouseCoordinateDisplay_TextChanged(object sender, EventArgs e)
        {

        }
        


    }
}

回复列表 (共1个回复)

沙发

这样获取到的坐标是鼠标相对于窗体客户区的坐标
要获取屏幕坐标,还要使用其它的方法.

我来回复

您尚未登录,请登录后再回复。点此登录或注册