现在的位置: 首页 > 综合 > 正文

发现:Click事件也能获取鼠标单击的坐标

2012年09月06日 ⁄ 综合 ⁄ 共 728字 ⁄ 字号 评论关闭

按照MSDN的说明以及平时的习惯,我们要获取鼠标单击时的相对坐标,都会使用MouseClick等事件,今天,偶然发现,原来Click事件也可以。

 

/*
 惊天地泣鬼神的考古业绩。
 * 原来Cilck事件也能获取鼠标点击的当前坐标,
  MSDN上说要用MouseClick事件,哈哈
 * 原来Click事件也可以!!!
 * 但是,如果通过键盘引发事件,而不是通过鼠标操作,即不能获取。
 * 鼠标右键单击无效。
 */

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.button1.Click += (s, e) =>
                {
                    try
                    {
                        MessageBox.Show("事件源类型:" +
                            s.GetType().ToString() + "\n" +
                            "事件参数类型:" +
                            e.GetType().ToString() + "\n" +
                            "鼠标点击时X坐标:" +
                            ((MouseEventArgs)e).X.ToString() + "\n" +
                            "Y坐标:" +
                            ((MouseEventArgs)e).Y.ToString());
                    }
                    catch
                    {
                        MessageBox.Show("你可能通过回车键触发事件,无法获取数据。");
                    }
                };
        }
    }
}

 

抱歉!评论已关闭.