C#获取Windows下光标位置(转)

使用C#获取光标相对于显示器屏幕的位置:

方式一:

 1 [csharp] view plaincopyprint?
 2 using System;  
 3 using System.Drawing;  
 4 using System.Runtime.InteropServices;  
 5  
 6 namespace ColorPicker  
 7 {  
 8     /// <summary>  
 9     /// win8下wpf程序测试成功  
10     /// </summary>  
11     public class CursorPointManager  
12     {  
13         #region 得到光标在屏幕上的位置  
14         [DllImport("user32")]  
15         private static extern bool GetCursorPos(out Point lpPoint);  
16   
17         /// <summary>  
18        /// 获取光标相对于显示器的位置  
19         /// </summary>  
20         /// <returns></returns>  
21         public static Point GetCursorPosition()  
22         {  
23             Point showPoint = new Point();  
24             GetCursorPos(out showPoint);  
25             return showPoint;  
26         }  
27         #endregion  
28   
29     }  
30 }  

 

方式二:

使用System.Windows.Forms的Cursor类。

在WPF程序中引入System.Windows.Forms命名空间,如下调用

1 [csharp] view plaincopyprint?
2 System.Drawing.Point point =System.Windows.Forms.Cursor.Position;  

 

posted @ 2013-05-15 08:57  夜曲984  阅读(825)  评论(0编辑  收藏  举报