1. 程式人生 > >WPF 定時器DispatcherTimer+GetCursorPos 的使用,動態檢視螢幕上任一點座標

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(); // 滑鼠的實時座標在標題上體現出來        
        }

    }
}