기본적인 마우스 메시지
마우스메시지의 종류
메시지 | 설명 |
WM_LBUTTONDOWN | 마우스 왼쪽 버튼을 눌렀을 때 |
WM_LBUTTONUP | 마우스 왼쪽 버튼을 뗏을 때 |
WM_LBUTTONDBLCLK | 마우스 왼쪽 버튼을 더블클릭 했을 때 |
WM_RBUTTONDOWN | 마우스 오른쪽 버튼을 눌렀을 때 |
WM_RBUTTONUP | 마우스 오른쪽 버튼을 뗏을 때 |
WM_RBUTTONDBLCLK | 마우스 오른쪽 버튼을 더블클릭 했을 때 |
WM_MBUTTONDOWN | 마우스 가운데 버튼을 눌렀을 때 |
WM_MBUTTONUP | 마우스 가운데 버튼을 뗏을 때 |
WM_MBUTTONDBLCLK | 마우스 가운데 버튼을 더블클릭 했을 때 |
WM_MOUSEMOVE | 마우스가 움직일 때 |
마우스 클릭시 좌표정보( lParam )
lParam 매개변수는 32비트인 변수로 상위 2바이트는 y 좌표 값이 저장되고, 하위 2바이트는 x 좌표 값이 저장된다.
마우스 클릭 상태와 키보드 조합( wParam )
탐색기에서 여러 개의 파일을 선택할 때 Shift키나 Ctrl키를 누르고 마우스로 선택한다. 즉 마우스의 상태와 키 상태를 알아야 처리가 가능하다. 이러한 상태값을 가지고 있는 매개변수가 바로 wParam이다.
키값 | 설명 |
MK_LBUTTON | 마우스 왼쪽 버늩을 눌려져 있다. |
MK_RBUTTON | 마우스 오른쪽 버늩을 눌려져 있다. |
MK_MBUTTON | 마우스 가운데 버늩을 눌려져 있다. |
MK_CONTROL | Ctrl 키가 눌려져 있다. |
MK_SHIFT | Shift 키가 눌려져 있다. |
마우스로 원을 선택해보자
#include <windows.h> #include <TCHAR.H> LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS WndClass; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = _T("Window Class Name"); RegisterClass(&WndClass); hwnd = CreateWindow(_T("Window Class Name"), _T("Window Title Name"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } #include <math.h> #define BSIZE 40 double LengthPts(int x1, int y1, int x2, int y2) { return(sqrt((float)((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)))); }
BOOL InCircle(int x, int y, int mx, int my) { if (LengthPts(x, y, mx, my) < BSIZE) else }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static int x, y; static BOOL Selection; int mx, my;
switch (iMsg) { case WM_CREATE: x = 50; y = 50; Selection = FALSE break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); if (Selection) Rectangle(hdc, x - BSIZE, y - BSIZE, x + BSIZE, y + BSIZE); Ellipse(hdc, x - BSIZE, y - BSIZE, x + BSIZE, y + BSIZE); EndPaint(hwnd, &ps); break; case WM_LBUTTONDOWN: mx = LOWORD(lParam); my = HIWORD(lParam); if (InCircle(x, y, mx, my)) InvalidateRgn(hwnd, NULL, TRUE); break; case WM_LBUTTONUP: Selection = FALSE; InvalidateRgn(hwnd, NULL, TRUE); break; case WM_DESTROY: PostQuitMessage(0); break; } return(DefWindowProc(hwnd, iMsg, wParam, lParam)); } |
실행 결과] 원을 선택하면 사각형 테두리가 생심
마우스로 드래그하여 원을 이동하기
#include <math.h> #define BSIZE 40 double LengthPts(int x1, int y1, int x2, int y2) { return(sqrt((float)((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)))); }
BOOL InCircle(int x, int y, int mx, int my) { if (LengthPts(x, y, mx, my) < BSIZE) return TRUE else return FALSE }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDChdc; PAINTSTRUCT ps; static intx, y; static BOOL Selection; intmx, my;
switch (iMsg) { case WM_CREATE: x = 50; y = 50; Selection = FALSE; break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); if (Selection) Rectangle(hdc, x - BSIZE, y - BSIZE, x + BSIZE, y + BSIZE); Ellipse(hdc, x - BSIZE, y - BSIZE, x + BSIZE, y + BSIZE); EndPaint(hwnd, &ps); break; case WM_LBUTTONDOWN: mx = LOWORD(lParam); my = HIWORD(lParam); if (InCircle(x, y, mx, my)) Selection = TRUE; InvalidateRgn(hwnd, NULL, TRUE); break; case WM_LBUTTONUP: Selection = FALSE; InvalidateRgn(hwnd, NULL, TRUE); break; case WM_MOUSEMOVE: mx = LOWORD(lParam); my = HIWORD(lParam); if (Selection) { x = mx; y = my; InvalidateRgn(hwnd, NULL, TRUE); } break; case WM_DESTROY: PostQuitMessage(0); break; } return(DefWindowProc(hwnd, iMsg, wParam, lParam)); } |
실행 결과]
'프로그램언어 > C++' 카테고리의 다른 글
API 리소스 (0) | 2020.09.23 |
---|---|
API 그리기 모드(Draw Mode) (0) | 2020.09.23 |
API 타이머 (0) | 2020.09.21 |
API 키도드 입력로 도형제어 (0) | 2020.09.21 |
파일 입출력 (0) | 2020.08.24 |