سؤال

كيف أحصل على موقف الماوس؟ أريدها في وضع الشاشة.

أبدأ برنامجي أريد التعيين على موضع الماوس الحالي.

Location.X = ??
Location.Y = ??

يحرر: يجب أن يحدث هذا قبل إنشاء النموذج.

هل كانت مفيدة؟

المحلول

يجب عليك استخدامها system.windows.forms.cursor.posor.: "نقطة تمثل موضع المؤشر في إحداثيات الشاشة".

نصائح أخرى

إذا كنت لا ترغب في الإشارات المرجعية، فيمكنك استخدام 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. سيحصل على تسريع الشاشة الحالية للماوس (إذا كنت في مراقبة, ، ال ماوس ستحصل الممتلكات أيضا على نفس القيمة).

لتعيين موضع الماوس، سيتعين عليك استخدامها 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> OK)

System.Windows.Forms.Control.MousePosition

يحصل على موضع مؤشر الماوس في إحداثيات الشاشة."خاصية الموقف مطابقة للسيطرة. خاصية MousePosition."

للحصول على الموقف، انظر إلى حدث OnMousemove. سوف تعطيك Mouseventargs موقع X An 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 تم العثور على أعداد صحيحة من خلال التجربة.

سيكون رائعا إذا كان شخص ما يمكن أن يفسر لماذا بالضبط هذه الأرقام ^.


أيضا، هناك متغير آخر:

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top