사각형 마우스로 드래그 엔 드랍






LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;

//렉트 선언
static RECT rc;

//마우스 포인터의 위치를 담을 변수 선언
static POINT pt = { 0, 0 };
static char strPt[24];

static bool click = false; //좌클릭 여부 판단

//클릭위치와 원래 상자와의 거리
static int x2_left = 0;
static int x2_right = 0;
static int y2_top = 0;
static int y2_bottom = 0;


switch (iMessage)
{
case WM_CREATE:
//중심을 기준으로 사각형을 그리는 rc 처리
rc = RectMakeCenter(400, 400, 100, 100);
break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

//사각형 그림
Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);

//확인용 마우스 위치 뿌려 주기
wsprintf(strPt, "x : %d,   y : %d", pt.x, pt.y);
TextOut(hdc, 20, 20, strPt, strlen(strPt));

EndPaint(hWnd, &ps);
break;

case WM_MOUSEMOVE:
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if (click == true)
{
rc.left = pt.x - x2_left;
rc.right = pt.x + x2_right;
rc.top = pt.y - y2_top;
rc.bottom = pt.y + y2_bottom;
}
InvalidateRect(hWnd, NULL, true);

break;

case WM_LBUTTONDOWN://마우스 왼쪽 버튼
if (((rc.left < pt.x) && (pt.x < rc.right)) && ((rc.top < pt.y) && (pt.y < rc.bottom)))
{
x2_left = pt.x - rc.left;
x2_right = rc.right - pt.x;
y2_top = pt.y - rc.top;
y2_bottom = rc.bottom - pt.y;
click = true;
}
break;
case WM_LBUTTONUP:
click = false;
break;

댓글