WPF 定时器DispatcherTimer+GetCursorPos 的使用,动态查看屏幕上任一点坐标

原文: WPF 定时器DispatcherTimer+GetCursorPos 的使用,动态查看屏幕上任一点坐标

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Data;
using  System.IO;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Imaging;
using  System.Windows.Navigation;
using  System.Windows.Shapes;
using  System.Diagnostics;
using  System.Runtime.InteropServices;
using  System.Windows.Threading;


namespace  AiGame
{
   
    
public   partial   class  MainWindow : Window
    {
        
public   struct  POINT
        {
            
public   int  X;
            
public   int  Y;
        }     
        [DllImport(
" user32.dll " , CharSet  =  CharSet.Auto)] // 导入Dll
         public   static   extern   bool  GetCursorPos( ref   POINT pt); // 定义相对应的函数,需使用ref传入结构,这里是传入结构的引用
         public  MainWindow()
        {
            InitializeComponent();
            
            DispatcherTimer dTimer 
=   new  System.Windows.Threading.DispatcherTimer();
            dTimer.Tick 
+=   new  EventHandler(dTimer_Tick);
            dTimer.Interval 
=   new  TimeSpan( 0 0 0 0 100 );               
            dTimer.Start();
        }
        
        
void  dTimer_Tick( object  sender, EventArgs e)
        {
            POINT p 
=   new  POINT();    
            GetCursorPos(
ref   p); // 这里传入结构实例
             this .Title =  p.X.ToString()  +   "    "   +  p.Y.ToString(); // 鼠标的实时坐标在标题上体现出来        
        }

    }
}

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/9807684.html
今日推荐