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

How to Get the Screen Mouse Position in C#?

DDD
Release: 2025-01-20 04:21:39
Original
565 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template