C #에서 마우스 위치 가져 오기
마우스 위치는 어떻게 얻습니까? 화면 위치 측면에서 원합니다.
현재 마우스 위치로 설정하려는 프로그램을 시작합니다.
Location.X = ??
Location.Y = ??
편집 : 양식이 생성되기 전에 발생해야합니다.
System.Windows.Forms.Cursor.Position : "화면 좌표에서 커서의 위치를 나타내는 Point"를 사용해야 합니다.
양식을 참조하지 않으려면 interop을 사용하여 커서 위치를 가져올 수 있습니다.
/// <summary>
/// Struct representing a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public static Point GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
//bool success = User32.GetCursorPos(out lpPoint);
// if (!success)
return lpPoint;
}
Cursor.Position 은 마우스의 현재 화면 위치를 가져옵니다 ( Control 에있는 경우 MousePosition 속성도 동일한 값을 얻습니다).
마우스 위치를 설정하려면 다음을 사용 Cursor.Position하여 새 Point를 지정해야합니다 .
Cursor.Position = new Point(x, y);
Main양식을 작성하기 전에 방법 에서이 작업을 수행 할 수 있습니다 .
구체적인 예에 답하려면 :
// your example
Location.X = Cursor.Position.X;
Location.Y = Cursor.Position.Y;
// sample code
Console.WriteLine("x: " + Cursor.Position.X + " y: " + Cursor.Position.Y);
를 using System.Windows.Forms;추가하고 참조를 추가하는 것을 잊지 마십시오 (참조를 마우스 오른쪽 버튼으로 클릭> 참조 추가> .NET 탭> Systems.Windows.Forms> 확인).
System.Windows.Forms.Control.MousePosition
화면 좌표에서 마우스 커서의 위치를 가져옵니다. "Position 속성은 Control.MousePosition 속성과 동일합니다."
위치를 얻으려면 OnMouseMove 이벤트를보십시오. MouseEventArgs는 x 및 y 위치를 제공합니다.
protected override void OnMouseMove(MouseEventArgs mouseEv)
마우스 위치를 설정하려면 Cursor.Position 속성을 사용하십시오.
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx
internal static class CursorPosition {
[StructLayout(LayoutKind.Sequential)]
public struct PointInter {
public int X;
public int Y;
public static explicit operator Point(PointInter point) => new Point(point.X, point.Y);
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out PointInter lpPoint);
// For your convenience
public static Point GetCursorPosition() {
PointInter lpPoint;
GetCursorPos(out lpPoint);
return (Point) lpPoint;
}
}
현재 커서를 초기화합니다. 그것을 사용하여 X와 Y의 위치를 얻습니다.
this.Cursor = new Cursor(Cursor.Current.Handle);
int posX = Cursor.Position.X;
int posY = Cursor.Position.Y;
양식 영역에서 현재 위치를 가져와야하는 경우 (실험적으로) 시도해보십시오.
Console.WriteLine("Current mouse position in form's area is " +
(Control.MousePosition.X - this.Location.X - 8).ToString() +
"x" +
(Control.MousePosition.Y - this.Location.Y - 30).ToString()
);
Although, 8 and 30 integers were found by experimenting.
Would be awesome if someone could explain why exactly these numbers ^.
Also, there's another variant:
Point cp = this.PointToClient(Cursor.Position); // Getting a cursor's position according form's area
Console.WriteLine("Cursor position: X = " + cp.X + ", Y = " + cp.Y);
참고URL : https://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp
'Programming' 카테고리의 다른 글
| 마 젠토 블록 및 블록 유형 이해 (0) | 2020.08.10 |
|---|---|
| VSTS 2010 SGEN : 오류 : 파일 또는 어셈블리를로드 할 수 없습니다 (HRESULT에서 예외 : 0x80131515). (0) | 2020.08.10 |
| Eclipse 3.7을 4.2 (Juno)로 업그레이드하는 가장 쉬운 방법 (0) | 2020.08.10 |
| datagridview에 대한 오른쪽 클릭 컨텍스트 메뉴 (0) | 2020.08.10 |
| Android Studio Checkout Github 오류“CreateProcess = 2”(Windows) (0) | 2020.08.10 |