본문 바로가기

Windows/Windows API

마우스 포지션(위치) 저장

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="Mouse2";

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
         ,LPSTR lpszCmdParam,int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst=hInstance;
    
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=(WNDPROC)WndProc;
    WndClass.lpszClassName=lpszClass;
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
         CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
         NULL,(HMENU)NULL,hInstance,NULL);
    ShowWindow(hWnd,nCmdShow);
    
    while(GetMessage(&Message,0,0,0)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}

// 그려지는 선의 정보를 담는 구조체 배열
struct {
    int x,y;
    BOOL Move;
} Line[10000];
int index=0;        // global

// struct 에 lParam의 loword(x) Hiword(y) 값을 넣어두고
// 상태를 나타내는 Move에 TRUE를 넣어준다.
void Capture(LPARAM lParam)
{
    Line[index].x=LOWORD(lParam);
    Line[index].y=HIWORD(lParam);
    Line[index].Move=TRUE;    
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    int i;
    static BOOL bnowDraw=FALSE;
    switch(iMessage) {
    // 시작점의 위치를 배열에 기록해 둔다.
    case WM_LBUTTONDOWN:
        bnowDraw=TRUE;
        Capture(lParam);
        index++;
        return 0;
    // 다음 마우스 이동점까지 선을 그리고 배열상에 기록한다.
    case WM_MOUSEMOVE:
        if (bnowDraw==TRUE) {
            hdc=GetDC(hWnd);
            MoveToEx(hdc,Line[index-1].x,Line[index-1].y,NULL);
            Capture(lParam);
            LineTo(hdc,Line[index].x,Line[index].y);
            index++;
            ReleaseDC(hWnd,hdc);
        }
        return 0;
    // 그리기를 종료한다.
    case WM_LBUTTONUP:
        bnowDraw=FALSE;
        return 0;
    // 화면을 지우고 다시 그리며 배열의 인덱스를 리셋하여 다시 기록하도록 한다.
    case WM_RBUTTONDOWN:
        index=0;                            // index를 초기화 하므로 struct에 내용을 다 지운거나 다름없다
        InvalidateRect(hWnd, NULL, TRUE);
        return 0;
    // 배열의 정보를 읽어 화면을 복구한다.
    case WM_PAINT:
        hdc=BeginPaint(hWnd, &ps);
        for (i=0;i<index;i++) {
            if (Line[i].Move == TRUE)
                MoveToEx(hdc,Line[i].x, Line[i].y, NULL);
            else
                LineTo(hdc,Line[i].x, Line[i].y);
        }
        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}