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

How to Get the Current Mouse Position in C#?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-20 04:12:10664browse

How to Get the Current Mouse Position in C#?

Getting the Mouse Position in C# Applications

Knowing the current mouse coordinates is essential for many C# applications needing precise user interaction control. This guide shows you how.

Screen Coordinates

To get the mouse position relative to the entire screen, use this property:

<code class="language-csharp">System.Windows.Forms.Cursor.Position;</code>

Example:

<code class="language-csharp">int x = System.Windows.Forms.Cursor.Position.X;
int y = System.Windows.Forms.Cursor.Position.Y;</code>

Important: This needs to happen before the form is created, as it depends on the system's current state.

Important Notes:

  • With multiple monitors, the position refers to the primary display.
  • System DPI settings might slightly affect the reported position.
  • For detailed mouse tracking (clicks, movement), explore System.Windows.Forms.Control.Mouse* events.

The above is the detailed content of How to Get the Current 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