Mouse operations

The program demonstrates the common mouse operation, after the execution of the program will be in the mouse moving track to draw a red dot, press the left button to draw small squares, press Ctrl and left keys to draw large squares, press the right button to exit the program. 

// Compiler Env: Visual C++ 6.0~2022,EasyX_2023大暑版
// https://easyx.cn
//
#include <graphics.h>

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

	ExMessage m;		// Define a message variable

	while(true)
	{
		// Get a mouse message or a key message
		m = getmessage(EX_MOUSE | EX_KEY);

		switch(m.message)
		{
			case WM_MOUSEMOVE:
				// Draw a red pixel when the mouse move
				putpixel(m.x, m.y, RED);
				break;

			case WM_LBUTTONDOWN:
				// If the left button is clicked while pressing the Ctrl key
				if (m.ctrl)
					// Draw a big square
					rectangle(m.x - 10, m.y - 10, m.x + 10, m.y + 10);
				else
					// Draw a small square
					rectangle(m.x - 5, m.y - 5, m.x + 5, m.y + 5);
				break;

			case WM_KEYDOWN:
				if (m.vkcode == VK_ESCAPE)
					return 0;	// Press ESC key to exit
		}
	}

	// Close the graphics window
	closegraph();
	return 0;
}

For more examples, please refer to https://codebus.cn

(贡献者:Krissi  编辑