1

How do I limit mouse to one particular HWND in Win32 API programming? This HWND is not necessarily created by me. It could be a browser window, or photoshop program window. I'm trying to write a program that doesn't let mouse leave particular Windows program. I can get HWND of the program by GetWindowText.

bodacydo
  • 75,521
  • 93
  • 229
  • 319

1 Answers1

4

Check out MSDN: ClipCursor function

ClipCursor function (winuser.h)

Confines the cursor to a rectangular area on the screen. If a subsequent cursor position (set by the SetCursorPos function or the mouse) lies outside the rectangle, the system automatically adjusts the position to keep the cursor inside the rectangular area.

Syntax

BOOL ClipCursor(
  [in, optional] const RECT *lpRect
);

Parameters

[in, optional] lpRect

Type: const RECT*

A pointer to the structure that contains the screen coordinates of the upper-left and lower-right corners of the confining rectangle. If this parameter is NULL, the cursor is free to move anywhere on the screen.

Return value

Type: BOOL

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The cursor is a shared resource. If an application confines the cursor, it must release the cursor by using ClipCursor before relinquishing control to another application.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • Thanks @Vlad. You're the man. Do you happen to know if this is per-application or global per-screen API? Looks like it's per screen? Meaning if I don't release the cursor then Windows is screwed because mouse can't move out of screen region? – bodacydo Aug 07 '15 at 20:14
  • It is global, but the documentation does not state whether it is per-monitor or not. I suspect that it is relative to the [virtual screen](https://msdn.microsoft.com/en-us/library/windows/desktop/dd145136.aspx) as a whole, not to any particular monitor. The documentation does state: "*Confines the cursor to a rectangular area on the screen. If a subsequent cursor position (set by the SetCursorPos function or the mouse) lies outside the rectangle, the system automatically adjusts the position to keep the cursor inside the rectangular area.*" I do not know whether the rect can span monitors... – Remy Lebeau Aug 07 '15 at 20:20
  • The doc also states: "*The cursor is a shared resource. If an application confines the cursor, it must release the cursor by using ClipCursor before relinquishing control to another application.*" So yes, once the cursor is clipped to a rectangle, it cannot leave that rectangle until released. – Remy Lebeau Aug 07 '15 at 20:20
  • 1
    This is only half an answer: While it does provide a way to confine mouse movement to a rectangular region, it won't tell you, how to connect this to a `HWND`. [GetWindowRect](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633519.aspx) is only half a solution as well, as it doesn't account for changes in window size (or visibility). – IInspectable Aug 08 '15 at 01:03
  • @IInspectable - I made an assumption that the essence of that question was "how to constrain cursor position" and that bodacydo can take it from there, and apparently I was right :) – Vlad Feinstein Aug 08 '15 at 21:43
  • You misinterpret the goal of stackoverflow. It's not about helping a particular developer. Stackoverflow is about questions with answers, that are generally applicable. This answer does not address the question, and a missing *Accept* mark seems to indicate that. – IInspectable Aug 09 '15 at 17:25
  • @llnspectable then the answer should be, "read the documentation" – me_ Jul 15 '17 at 12:07
  • @Deleted Done. However, I was hoping that the reader can find documentation on the Win32 API given its name. – Vlad Feinstein Dec 05 '22 at 18:57
  • Awesome thanks for the edit. True, but just like a good restaurant, it's nice to know a little bit about a potential meal by reading what the menu says about it, before one orders. +1 good sir. –  Dec 05 '22 at 23:32