setcapture

This function is used to enable the ability to capture mouse messages outside of the graphics window.

void setcapture();

Parameters

None

Return Value

None

Remarks

By default, the graphics window can only capture mouse messages within the window scope. When necessary, this function can be set to enable the capture of mouse messages outside the graphics window scope. And with this feature set to enable, the graphics window can get mouse messages outside the window scope only when the left, middle, or right mouse button is pressed.

When the graphics window no longer needs to capture mouse messages outside the window scope, the releasecapture function should be called in time to release the mouse, so that the capture of mouse messages is only within the graphics window scope.

setcapture and releasecapture function are commonly used like this:

  1. When the left mouse button is pressed, call setcapture to start capturing the mouse.
  2. Responds to mouse movement messages.
  3. When the left mouse button is released, call releasecapture to release the mouse and restore the original capture scope.

Examples

The following complete example implements the function of drawing a line, and the operation of drawing will not be interrupted when the mouse leave the drawing window scope:

#include <graphics.h>

int main()
{
	initgraph(640, 480);				// Create graphics window

	POINT from, to;						// The start and end points of the line
	bool drawing = false;				// Whether a line is drawing
	ExMessage msg;						// Message variable

	setrop2(R2_XORPEN);					// Set the line mode to XOR
	setlinecolor(GREEN);				// Set the line color to GREEN

	do
	{
		msg = getmessage();				// Get message

		switch (msg.message)
		{
			case WM_LBUTTONDOWN:		// Left mouse button down
				setcapture();
				from.x = to.x = msg.x;
				from.y = to.y = msg.y;
				drawing = true;
				line(from.x, from.y, to.x, to.y);
				break;

			case WM_LBUTTONUP:			// Left mouse button up
				releasecapture();
				drawing = false;
				break;

			case WM_MOUSEMOVE:			// Mouse move
				if (drawing)
				{
					line(from.x, from.y, to.x, to.y);		// Erase the previous line
					to.x = msg.x;
					to.y = msg.y;
					line(from.x, from.y, to.x, to.y);		// Draw the new line
				}
				break;

			default:
				break;
		}
	} while (msg.message != WM_RBUTTONDOWN);				// Press the right mouse button to exit
	
	closegraph();
	return 0;
}

See Also

releasecapture

(贡献者:Krissi  编辑