Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Manage mouse pointers

Feedback

In this article

The mousepointer, which is sometimes referred to as the cursor, is a bitmap that specifies a focus point on the screen for user input with the mouse. This article provides an overview of the mouse pointer in Windows Forms and describes some of the ways to modify and control the mouse pointer.

Accessing the mouse pointer

The mouse pointer is represented by theCursor class, and eachControl has aControl.Cursor property that specifies the pointer for that control. TheCursor class contains properties that describe the pointer, such as thePosition andHotSpot properties, and methods that can modify the appearance of the pointer, such as theShow,Hide, andDrawStretched methods.

The following example hides the cursor when the cursor is over a button:

private void button1_MouseEnter(object sender, EventArgs e) =>    Cursor.Hide();private void button1_MouseLeave(object sender, EventArgs e) =>    Cursor.Show();
Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter    Cursor.Hide()End SubPrivate Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave    Cursor.Show()End Sub

Controlling the mouse pointer

Sometimes you might want to change the position of the mouse or restrict the mouse pointer to a specific area. You can get or set the current location of the mouse using theCursor.Position property. Also, you can limit the area available to the mouse pointer by setting theClip property. The clip area, by default, is the entire screen.

The following example positions the mouse pointer between two buttons when they're clicked:

private void button1_Click(object sender, EventArgs e) =>    Cursor.Position = PointToScreen(button2.Location);private void button2_Click(object sender, EventArgs e) =>    Cursor.Position = PointToScreen(button1.Location);
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    PointToScreen(Button2.Location)End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click    PointToScreen(Button1.Location)End Sub

Changing the mouse pointer

Changing the mouse pointer is an important way of providing feedback to the user. For example, the mouse pointer can be modified in the handlers of theMouseEnter andMouseLeave events to tell the user that computations are occurring and to limit user interaction in the control. Sometimes, the mouse pointer changes because of system events, such as when your application is involved in a drag-and-drop operation.

The primary way to change the mouse pointer is by setting theControl.Cursor orDefaultCursor property of a control to a newCursor. For examples of changing the mouse pointer, see the code example in theCursor class. In addition, theCursors class exposes a set ofCursor objects for many different types of pointers, such as a pointer that resembles a hand.

The following example changes the cursor of the mouse pointer for a button to a hand:

button2.Cursor = System.Windows.Forms.Cursors.Hand;
Button2.Cursor = System.Windows.Forms.Cursors.Hand

To display the wait pointer, which resembles an hourglass, whenever the mouse pointer is on the control, use theUseWaitCursor property of theControl class.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?