win32_step_6 - 8BitsCoding/RobotMentor GitHub Wiki
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_DESTROY) PostQuitMessage(0);
else if (uMsg == WM_PAINT) {
PAINTSTRUCT ps;
HDC h_dc = BeginPaint(hWnd, &ps);
Rectangle(h_dc, g_rect.left, g_rect.top, g_rect.right, g_rect.bottom);
EndPaint(hWnd, &ps);
return 0;
}
else if (uMsg == WM_LBUTTONDOWN) {
/*
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
g_prev_pos.x = GET_X_LPARAM(lParam);
g_prev_pos.y = GET_Y_LPARAM(lParam);
if (x >= g_rect.left && x <= g_rect.right && y >= g_rect.top && y <= g_rect.bottom) {
g_is_clicked = 1;
}
*/
// μ’ λ μ¬μ΄λ°©λ²μΌλ‘ ꡬν
g_prev_pos.x = GET_X_LPARAM(lParam);
g_prev_pos.y = GET_Y_LPARAM(lParam);
if (PtInRect(&g_rect, g_prev_pos)) {
g_is_clicked = 1;
SetCapture(hWnd);
}
}
else if (uMsg == WM_LBUTTONUP) {
g_is_clicked = 0;
ReleaseCapture();
}
else if (uMsg == WM_MOUSEMOVE) {
if (g_is_clicked == 1) {
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
/*
int h_interval = x - g_prev_pos.x;
int v_interval = y - g_prev_pos.y;
g_rect.left += h_interval;
g_rect.top += v_interval;
g_rect.right += h_interval;
g_rect.bottom += v_interval;
*/
// μ½λλ₯Ό μ’ λ κ°λ¨ν
OffsetRect(&g_rect, x - g_prev_pos.x, y - g_prev_pos.y);
g_prev_pos.x = x;
g_prev_pos.y = y;
InvalidateRect(hWnd, NULL, TRUE);
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}