How to restrict mouse cursor inside window?
![]()
every windows application is a citizen of Windows OS and the mouse is shared between them. If your applications shows some critical dialog to get response, but its quite possible that your dialog can be skipped by user and take another application window. How can you restrict the mouse cursor inside your application window?
For instance, the following application – CursorJail. How can you close your mouse pointer into the dialog prison?

![]()
You can use the api – ClipCursor(). See the following code snippet.
// Get window rect. RECT WindowRect; GetWindowRect( &WindowRect ); // Restrict the cursor ClipCursor( &WindowRect ); ... // Free the cursor. ClipCursor( 0 );
Well, need the code snippet for drawing prison?
// Get client rect.
CRect ClientRect;
GetClientRect( &ClientRect );
const int SteelBarCount = 5;
int xOffset = ClientRect.Width() / SteelBarCount;
dc.Draw3dRect( 0,0, ClientRect.Width(), ClientRect.Height(), 0, 0 );
// Draw steel bars.
for( int SteelBarIndex = 1; SteelBarIndex < SteelBarCount; ++SteelBarIndex )
{
dc.MoveTo( xOffset * SteelBarIndex, 0 );
dc.LineTo( xOffset * SteelBarIndex, ClientRect.Height());
}
dc.MoveTo( 0,0 );
dc.LineTo( ClientRect.Width(), ClientRect.Height());
![]()
You can use this clip feature to restrict the cursor between different dialogs of same application. You can hide a lot of GUI bugs. Try it
![]()
Targeted Audience – Beginners.
Alright i have researched for 2 hours now and am about to die from not knowing….. What header do you need so you dont get errors for “dc.” stuff and “cRect”
Hi Crouchy,
CRect is mfc class. I hope you’re trying this code snippet for fun and in that case, just try the same in an mfc project, which includes all necessary headers.
Best Regards,
Jijo.