Home >Backend Development >C++ >How to Get the Screen Mouse Position in C#?

How to Get the Screen Mouse Position in C#?

DDD
DDDOriginal
2025-01-20 04:21:39567browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn