Windows/Windows API

한꺼번에 출력하기 (LockWindowUpdate)

aucd29 2013. 10. 1. 18:55
#include <windows.h>

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

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;
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_CREATE:
        SetWindowText(hWnd,"L키:그리기 금지, U키:그리기 허가");
        return 0;
    case WM_KEYDOWN:
        if (wParam == 'L') {
            SetWindowText(hWnd,"그리기 금지");
            LockWindowUpdate(hWnd);
        }
        else if (wParam == 'U') {
            SetWindowText(hWnd,"그리기 허가");
            LockWindowUpdate(NULL);
        }
        return 0;
    // 시작점의 위치를 배열에 기록해 둔다.
    case WM_LBUTTONDOWN:
        bnowDraw=TRUE;
        Line[index].x=LOWORD(lParam);
        Line[index].y=HIWORD(lParam);
        Line[index].Move=TRUE;
        index++;
        return 0;
    // 다음 마우스 이동점까지 선을 그리고 배열상에 기록한다.
    case WM_MOUSEMOVE:
        if (bnowDraw==TRUE) {
            hdc=GetDC(hWnd);
            MoveToEx(hdc,Line[index-1].x,Line[index-1].y,NULL);
            Line[index].x=LOWORD(lParam);
            Line[index].y=HIWORD(lParam);
            Line[index].Move=FALSE;
            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;
        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));
}