{ this.MouseClick += new MouseEventHandler(Form1_MouseClick); this.Capture = true; //<--- allow you to click outside the form } void Form1_MouseClick(object sender, MouseEventArgs e) { Point p = e.Location; //<--- position this.Capture = false; // stop capturing }
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="GetCursorPos")] public static extern int GetCursorPos ( ref POINTAPI lpPoint ); [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct POINTAPI { public int x; public int y; } 使用 POINTAPI pt = new POINTAPI(); GetCursorPos(ref pt); this.label1.Text = "x=" + pt.x + " y=" + pt.y;
{
this.MouseClick += new MouseEventHandler(Form1_MouseClick);
this.Capture = true; //<--- allow you to click outside the form
}
void Form1_MouseClick(object sender, MouseEventArgs e)
{
Point p = e.Location; //<--- position
this.Capture = false; // stop capturing
}
public static extern int GetCursorPos (
ref POINTAPI lpPoint
); [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct POINTAPI
{
public int x;
public int y;
}
使用
POINTAPI pt = new POINTAPI();
GetCursorPos(ref pt);
this.label1.Text = "x=" + pt.x + " y=" + pt.y;
获取不了窗体外的,我单击窗体外的任何一个地方的时候都没有反映。
GetCursorPos(ref pt);
MessageBox.Show( "x=" + pt.x + " y=" + pt.y);
哪这个要写在那里呢?
我是要单击窗体外的地方。如桌面的时候得到坐标。