日期:2014-05-18 浏览次数:21486 次
public partial class DemoForm : Form
{
[StructLayout(LayoutKind.Sequential)]
struct NativeRECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, ref NativeRECT rect);
........省略部分代码
private void btnFind_Click(object sender, EventArgs e)
{
NativeRECT rect = new NativeRECT();
IntPtr fdwd=FindWindow(null, "程序");
txtFind.Text = fdwd.ToString();
GetWindowRect(fdwd, ref rect);
txt_X1.Text = rect.left.ToString();//获取左上角X的坐标
txt_Y1.Text = rect.top.ToString();//获取左上角Y的坐标
}