전체적인 구조의 코드는 너무 기니깐, LRESULT CALLBACK WndProc()함수의 코드만 적도록 하겠다...
//전역변수
int g_is_clicked = 0;
RECT g_rect = { 50, 50, 50, 50 };
POINT g_prev_pos;
LREULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg == MW_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){
g_is_clicked = 1;
g_prev_pos.x = LOWORD(lParam);
g_prev_pos.y = HIWORD(lParam);
if(g_prev_pos.x <= g_rect.right && g_prev_pos.x>=g_rect.left&&
g_prev_pos.y <=g_rect.top && g_prev_pos.y >=g_rect.bottom){
g_is_clicked = 1;
SetCapture(hWnd); //사각형이 클라이언트(유효화영역)에서 나가도 마우스로 움직일수 있게 해주는 API함수
}
}
else if(uMsg == WM_LBUTTONUP){
g_is_clicked = 0;
ReleaseCapture();
}
else if(uMsg == WM_MOUSEMOVE){
if(g_is_clicked){
int x = LOWORD(lParam);
int y = HIWORD(lParam);
int h_interval = x - g_prev_pos.x;
int v_interval = y - g_prev_pos.y;
g_rect.left += h_interval;
g_rect.right += h_interval;
g_rect.top += v_interval;
h_interval.bottom += v_interval;
g_prev_pos.x = x;
g_prev_pos.y = y;
InvalidateRect(hWnd,NULL,TRUE);
}
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
'MFC' 카테고리의 다른 글
Brush Handle값 받아서 테두리없는 사각형패턴 (0) | 2020.09.09 |
---|---|
Pen Handle값 받아서 Pen이나 Brush 다르게 그리는 것 (0) | 2020.09.09 |
BitBlt,WM_CLOSE (0) | 2020.09.07 |
선그리기 (0) | 2020.09.05 |
핸들(HANDLE) (0) | 2020.08.30 |
댓글