如何获取鼠标所在位置的xy坐标?

2025-02-24 16:19:14
推荐回答(5个)
回答1:

鼠标在窗体里面?
那么就是Cursor属性 这个是个Cursor类 然后用Position属性.

如果不是
可以用win32Api
GetCursorPos Function
这样.Point p = new Point();
GetCursorPos(p);
p.x横坐标,p.y纵坐标.

--------------------------------------------------------------------------------

The GetCursorPos function retrieves the cursor's position, in screen coordinates.

Syntax

BOOL GetCursorPos( LPPOINT lpPoint
);
Parameters

lpPoint
[out] Pointer to a POINT structure that receives the screen coordinates of the cursor.
Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The cursor position is always specified in screen coordinates and is not affected by the mapping mode of the window that contains the cursor.

The calling process must have WINSTA_READATTRIBUTES access to the window station.

回答2:

不知道你问的是web还是winform.
通过JS获取坐标有2种.
1相对窗口,当然是以浏览器窗口为主了,即浏览器中可见部分(即包含文档部分)的左上角为坐标原点。这是用event.clientX和event.clientY获取的。

2相对屏幕,当然是以你的显示器为主了,显示器的左上角为原点,用event.screenX和event.screenY可以获取到

例子





X的坐标是:0,Y的坐标是:0


回答3:

如果你是想要鼠标相对于屏幕左上的坐标就用
Point getPoint = MousePosition;
如果你是要鼠标相对于窗体左上的坐标就定义一个全局变量获得窗体MouseMove事件里坐标,用的时候直接用getPoint就行
Point getPoint = MousePosition;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
getPoint = e.Location;
}

回答4:

如果在mouseevent中,可以用MouseEventArgs来得到
如果不非事件中,可以用Cursor.Position来得到,
Cursor.Position.Y;Cursor.Position.X

回答5:

private void lblTime_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
}

e.X, e.Y 就是鼠标的坐标