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

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

  1. /* 
  2.  惊天地泣鬼神的考古业绩。 
  3.  * 原来Cilck事件也能获取鼠标点击的当前坐标, 
  4.   MSDN上说要用MouseClick事件,哈哈 
  5.  * 原来Click事件也可以!!! 
  6.  * 但是,如果通过键盘引发事件,而不是通过鼠标操作,即不能获取。 
  7.  * 鼠标右键单击无效。 
  8.  */  
  9.   
  10. using System;  
  11. using System.Collections.Generic;  
  12. using System.ComponentModel;  
  13. using System.Data;  
  14. using System.Drawing;  
  15. using System.Linq;  
  16. using System.Text;  
  17. using System.Windows.Forms;  
  18.   
  19. namespace WindowsFormsApplication1  
  20. {  
  21.     public partial class Form1 : Form  
  22.     {  
  23.         public Form1()  
  24.         {  
  25.             InitializeComponent();  
  26.             this.button1.Click += (s, e) =>  
  27.                 {  
  28.                     try  
  29.                     {  
  30.                         MessageBox.Show("事件源类型:" +  
  31.                             s.GetType().ToString() + " " +  
  32.                             "事件参数类型:" +  
  33.                             e.GetType().ToString() + " " +  
  34.                             "鼠标点击时X坐标:" +  
  35.                             ((MouseEventArgs)e).X.ToString() + " " +  
  36.                             "Y坐标:" +  
  37.                             ((MouseEventArgs)e).Y.ToString());  
  38.                     }  
  39.                     catch  
  40.                     {  
  41.                         MessageBox.Show("你可能通过回车键触发事件,无法获取数据。");  
  42.                     }  
  43.                 };  
  44.         }  
  45.     }  
  46. }  


  

原文地址:https://www.cnblogs.com/xieweikai/p/6832809.html