Home >Backend Development >C++ >How to Get the Screen Mouse Position in C#?
Retrieving Mouse Coordinates in C#
Efficiently obtaining the current mouse position is essential for many C# applications. This guide explains how to get the screen coordinates of the mouse cursor.
To position a window at the mouse cursor's location before the form is initialized, use this code:
<code class="language-csharp">// Get mouse position before form creation Point mousePos = System.Windows.Forms.Cursor.Position; // Set window location to mouse coordinates this.Location = mousePos;</code>
The System.Windows.Forms.Cursor.Position
property returns a Point
object containing the mouse's screen coordinates. This works reliably even before the form is fully displayed.
The above is the detailed content of How to Get the Screen Mouse Position in C#?. For more information, please follow other related articles on the PHP Chinese website!