मैं माउस स्थिति कैसे प्राप्त करूं? मैं इसे स्क्रीन पोजीशन की अवधि में चाहता हूं।
मैं अपना कार्यक्रम शुरू करता हूं जिसे मैं वर्तमान माउस स्थिति पर सेट करना चाहता हूं।
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 को माउस की वर्तमान स्क्रीन कविता मिलेगी (यदि आप एक नियंत्रण में हैं , तो माउसपोज़िशन गुण को भी समान मूल्य मिलेगा)।
माउस स्थिति सेट करने के लिए, आपको इसका उपयोग करना होगा 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
स्क्रीन निर्देशांक में माउस कर्सर की स्थिति हो जाती है। "स्थिति संपत्ति 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;
}
}
यदि आपको फॉर्म के क्षेत्र में वर्तमान स्थिति प्राप्त करने की आवश्यकता है (प्रयोगात्मक रूप से प्राप्त करें), कोशिश करें:
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);
DLL आयात करने के लिए आपके पास निम्न आयात भी होने चाहिए
using System.Runtime.InteropServices;
using System.Diagnostics;