In a comment to a previous answer you refer to your previous Question "Form move inside client desktop area". The code in the accepted answer prevents the window to be moved outside the desktop. And now you want to "pin" the cursor to the window while the user is dragging it. So if the window cannot be moved further the cursor also should not go further.
If my understanding of your question is correct you should handle the WM_NCLBUTTONDOWN message to calculate the limits for the mouse movement. You can then use Cursor.Clip
to apply these limits.
However, if you apply the clip rectangle in the WM_NCLBUTTONDOWN message it will be removed immediately. If you apply it in the WM_MOVING message instead it will be automatically removed when the dragging ends.
This is then also a solution to the aforementioned question: If the mouse can only be moved in the calculated rectangle while the user is dragging the window then the window itself can only be moved in the allowed region.
public const int WM_MOVING = 0x0216;
public const int WM_NCLBUTTONDOWN = 0x00A1;
public const int HT_CAPTION = 0x0002;
private Rectangle _cursorClip = Rectangle.Empty;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCLBUTTONDOWN:
if (m.WParam.ToInt32() == HT_CAPTION)
{
Point location = Cursor.Position;
Rectangle screenBounds = Screen.PrimaryScreen.Bounds;
Rectangle formBounds = Bounds;
_cursorClip = Rectangle.FromLTRB(location.X + screenBounds.Left - formBounds.Left,
location.Y + screenBounds.Top - formBounds.Top,
location.X + screenBounds.Right - formBounds.Right,
location.Y + screenBounds.Bottom - formBounds.Bottom);
}
break;
case WM_MOVING:
Cursor.Clip = _cursorClip;
break;
}
base.WndProc(ref m);
}