Home >Backend Development >C++ >How to Programmatically Move the Mouse Cursor in C#?

How to Programmatically Move the Mouse Cursor in C#?

DDD
DDDOriginal
2025-01-11 18:51:42655browse

How to Programmatically Move the Mouse Cursor in C#?

Use C# to simulate mouse movement

In order to automate tasks such as mouse movement, you need to understand how to control the mouse cursor using code.

Question: How to move the mouse cursor programmatically in C#?

Answer:

To manipulate the mouse cursor in C#, you can use the Cursor.Position attribute. This property allows you to set the coordinates of the mouse pointer.

The following code snippet demonstrates how to move the cursor 50 pixels up and left:

<code class="language-csharp">using System.Drawing;

private void MoveCursor()
{
    Point cursorPosition = Cursor.Position;
    cursorPosition.X -= 50;
    cursorPosition.Y -= 50;
    Cursor.Position = cursorPosition;
}</code>

For automatic mouse movement at specified intervals, you can use a timer. When the timer expires, simply call the MoveCursor() method to adjust the cursor position.

The above is the detailed content of How to Programmatically Move the Mouse Cursor 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