ฉันจะรับตำแหน่งเมาส์ได้อย่างไร? ฉันต้องการมันในแง่ของตำแหน่งหน้าจอ
ฉันเริ่มโปรแกรมของฉันฉันต้องการตั้งค่าเป็นตำแหน่งเมาส์ปัจจุบัน
Location.X = ??
Location.Y = ??แก้ไข:สิ่งนี้จะต้องเกิดขึ้นก่อนที่จะสร้างแบบฟอร์ม
ฉันจะรับตำแหน่งเมาส์ได้อย่างไร? ฉันต้องการมันในแง่ของตำแหน่งหน้าจอ
ฉันเริ่มโปรแกรมของฉันฉันต้องการตั้งค่าเป็นตำแหน่งเมาส์ปัจจุบัน
Location.X = ??
Location.Y = ??แก้ไข:สิ่งนี้จะต้องเกิดขึ้นก่อนที่จะสร้างแบบฟอร์ม
คำตอบ:
คุณควรใช้System.Windows.Forms.Cursor.Position : "จุดที่แสดงตำแหน่งของเคอร์เซอร์ในพิกัดหน้าจอ"
PointToClient.
                    หากคุณไม่ต้องการอ้างอิงแบบฟอร์มคุณสามารถใช้การทำงานร่วมกันเพื่อรับตำแหน่งเคอร์เซอร์:
using System.Runtime.InteropServices;
using System.Windows; // Or use whatever point class you like for the implicit cast operator
using System.Drawing;
/// <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);
    // NOTE: If you need error handling
    // bool success = GetCursorPos(out lpPoint);
    // if (!success)
        
    return lpPoint;
}Cursor.Positionจะได้รับ poisition หน้าจอปัจจุบันของเมาส์ (ถ้าคุณอยู่ในการควบคุมการMousePositionคุณสมบัติยังจะได้รับค่าเดียวกัน)
ในการตั้งค่าตำแหน่งเมาส์คุณจะต้องใช้Cursor.Positionและให้จุดใหม่:
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รับตำแหน่งของเคอร์เซอร์ของเมาส์ในพิกัดหน้าจอ "คุณสมบัติตำแหน่งจะเหมือนกับคุณสมบัติ 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()
);แม้ว่าจะพบจำนวนเต็ม8และ30จำนวนโดยการทดลอง
จะดีมากถ้ามีคนอธิบายได้ว่าทำไมถึงเป็นตัวเลขเหล่านี้ ^
นอกจากนี้ยังมีตัวแปรอื่น (การพิจารณารหัสอยู่ใน CodeBehind ของแบบฟอร์ม):
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);คุณต้องมีการนำเข้าต่อไปนี้เพื่อที่จะนำเข้า DLL
using System.Runtime.InteropServices;
using System.Diagnostics;