65.9K
CodeProject is changing. Read more.
Home

The Cursor.Clip Property

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Apr 28, 2012

CPOL
viewsIcon

19672

Freezing the Windows Cursor is done in an easy "one-liner"

Introduction

Sometimes you may want to set the Cursor's bounds, e.g. to forbid the user leaving the form with the cursor.

How To Do

In the Cursor class, there's a property named Clip that enables you to set a Rectangle in which the user can move the cursor.

VB.NET

Forbid to leave form:

Cursor.Clip = Me.Bounds

Make moving possible again:

Cursor.Clip = Rectangle.Empty

C#

Forbid to leave form:

Cursor.Clip = this.Bounds;

Make moving possible again:

Cursor.Clip = Rectangle.Empty;

Points of Interest

This property will be reset if the form loses focus. So If you really want to prohibit the user to move the cursor out of the form, you'll have to set the bounds every time in the Form.MouseEnter event.