原文:
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();//滑鼠的實時坐标在标題上展現出來
}
}