The use of SetCapture and ReleaseCapture in win32 (check the use in VCL)

      Recently, I used win32 to write the minesweeper game in "Visual C++ Classic Game Programming". When I wrote the mouse click minefield, I used the SetCapture and ReleaseCapture system functions.

So why do you need to use the mouse capture function?

I mistakenly thought that the tracking of the mouse can be handled by the value of Point, which can achieve the function I want, but I neglected to remove the mouse from the window when my mouse is pressed. In this case When the mouse is outside, when I flip the mouse up, the mouse position is not in the minesweeper window, so I need to capture the mouse position when the mouse is pressed, which solves the problem that the mouse is not in the window. .

 

Below I quote the introduction of SetCapture in Baidu Encyclopedia:


 Function: This function sets the mouse capture in the specified window belonging to the current thread. Once a window has captured the mouse, all mouse input is directed to that window, whether or not the cursor is within the bounds of the window. Only one window can capture the mouse at a time. If the mouse cursor is over a window created by another thread, the system directs mouse input to the specified window only when the mouse button is down.

  Prototype: HWND SetCapture (HWND hwnd); Parameters: hWnd: The window handle of the current thread to capture the mouse. Return value: The return value is the window handle where the mouse was last captured. If no such handle exists, the return value is NULL. Note: Only the foreground window can capture the mouse. If a background window wants to capture the mouse, the window will only receive messages for mouse events whose cursor hotspot is on the visible portion of the window. In addition, the user can click another window to bring it to the foreground even if the foreground window has captured the mouse. When a window no longer needs all mouse input, the thread that created the window should call the function ReleaseCapture to release the mouse. This function cannot be used to capture mouse input from another process. Windows 95: Calling SetCaptune causes windows that lose mouse capture to receive a WM_CAPTURECHANGED message. Quick check: Header file: Winuser.h: Input library: user32.lib. Javascript mouse capture Mouse capture (setCapture) is used to capture mouse events to the specified object of the current document. This object will receive all mouse events for the current application or the entire system.

 

ReleaseCapture is a computer term for a function that releases mouse capture from the window in the current thread and resumes normal mouse input processing.
Function: This function releases the mouse capture from the window in the current thread and resumes normal mouse input processing. The window that captures the mouse receives all mouse input (regardless of the cursor's position) unless the cursor hotspot is in another thread's window when the mouse button is clicked.
Function prototype: BOOL ReleaseCapture (VOlD)
Return value: If the function call succeeds, a non-zero value is returned; if the function call fails, the return value is zero. To get more error information, call the GetlastError function.
Note: The application calls this function after calling the function SetCaPture.

 

Let me give an example to easily understand the role of SetCapture and ReleaseCapture:

When you are browsing this log, you will pull the slider on the right side to adjust the up and down position of the content, then when you press the left button, move the mouse to the non-scroll bar, you will find that moving the mouse up and down the scroll bar still After the control, yes, this is the role of the capture mouse function SetCapture.

 

https://blog.csdn.net/haussuden/article/details/5853365

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324993923&siteId=291194637