这是我自己写的程序,不过都是用API写的,有没用不引用API的方法。
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 句柄_相对坐标_绝对坐标查看器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//根据坐标获取窗口句柄
[DllImport("User32")]
private static extern IntPtr WindowFromPoint(Point Point);
// 获得窗口矩形
[DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
// 获得客户区矩形
[DllImport("user32.dll")]
public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
//最左坐标
public int Top;
//最上坐标
public int Right;
//最右坐标
public int Bottom;
//最下坐标
}
private void timer1_Tick(object sender, EventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
Point p = new Point(x, y);
IntPtr formHandle = WindowFromPoint(p);//得到窗口句柄
textBox1.Text = formHandle.ToString();
textBox2.Text = "X:" + Cursor.Position.X + "," + "Y:" + Cursor.Position.Y;//鼠标所在的坐标
RECT rw = new RECT();
RECT rs = new RECT();
GetWindowRect(formHandle, out rw);//获取句柄窗体的绝对坐标
GetClientRect(formHandle, out rs);//获取句柄窗体的相对坐标
textBox3.Text = "起点("+Convert.ToString(rw.Left)+","+ Convert.ToString(rw.Top)+"),终点("+ Convert.ToString(rw.Right)+","+ Convert.ToString(rw.Bottom)+")";
int sx = Cursor.Position.X - rw.Left;
int sy = Cursor.Position.Y - rw.Top;//计算得到句柄窗体相对坐标的位置,并在textbox4内随鼠标移动而不断显示
textBox4.Text = "X:" + sx.ToString() + ", Y:" + sy.ToString();
}
}
}