This topic has not yet been rated - Rate this topic

Cursor.Clip Property

Gets or sets the bounds that represent the clipping rectangle for the cursor.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public static Rectangle Clip { get; set; }

Property Value

Type: System.Drawing.Rectangle
The Rectangle that represents the clipping rectangle for the Cursor, in screen coordinates.

A clipped cursor is allowed to move only within its clipping rectangle. Generally, the system allows this only if the mouse is currently captured. If the cursor is not currently clipped, the resulting rectangle contains the dimensions of the entire screen.

The following code example creates a cursor from the Current cursor's Handle, changes its position and clipping rectangle. The result is the cursor will move up and to the left 50 pixels from where it is when the code is executed. Additionally, the cursor's clipping rectangle is changed to the bounds of the form (by default it is the user's whole screen). This example requires that you have a Form and a Button to call this code when it is clicked.


private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 

   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
  • UIPermission  

    for all windows to set this property. Associated enumeration: UIPermissionWindow.AllWindows

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Fine

This work perfect multi-monitor, but you must put in the correct place...
try put like this

.....


RECT selfwindow;
GetWindowRect(hwnd,&selfwindow);MSG msg;

ZeroMemory(&msg,sizeof(msg));

while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
ClipCursor(&selfwindow); // this keep the mouse in the window
UpdateMousePos();
Render();
}



.....


Limitation
This does NOT work on multi-monitor systems. Microsoft Team, could you add support for clipping the Cursor on multi-monitor systems?

Thanks, Bruce