涂鸦板

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    static int  cxClient, cyClient, draw=0, started=0, LineWide=0, R=0, G=0, B=0;
    static HPEN hPen;
    static HDC         hdc ;
    int         i ;
    static PAINTSTRUCT ps ;
    switch(Message) {
        case WM_SIZE: {
            cxClient = LOWORD (lParam) ;
            cyClient = HIWORD (lParam) ;
            break;
        }
        case WM_LBUTTONDOWN:
            draw = 1; 
            if (!started) {
                hdc = BeginPaint (hwnd, &ps) ;
            }
            started = 1; 
            MoveToEx (hdc, LOWORD(lParam), HIWORD(lParam), NULL) ;
            break; 
        case WM_LBUTTONUP:
            draw = 0; 
            break; 
        case WM_MOUSEMOVE:
            if (draw) {
                hPen = CreatePen(PS_SOLID, LineWide, RGB(R,G,B));
                SelectObject(hdc, hPen);
                LineTo(hdc, LOWORD(lParam), HIWORD(lParam)) ;
            }
            break; 
        case WM_KEYDOWN:
            switch(wParam){
                case VK_UP: if (LineWide<20) LineWide++; break;
                case VK_DOWN: if (LineWide>0) LineWide--; break;
                case VK_LEFT: if (R<255) R++; if (G<255) G++; if (B<255) B++; break;
                case VK_RIGHT: if (R>0) R--; if (G>0) G--; if (B>0) B--; break;
                case 'R': case 'r':    if (R<255) R++; else {if(G>0)G--;if(B>0)B--;}; break; 
                case 'G': case 'g':    if (G<255) G++; else {if(R>0)R--;if(B>0)B--;}; break; 
                case 'B': case 'b':    if (B<255) B++; else {if(R>0)R--;if(G>0)G--;}; break; 
            }
            break;
        case WM_PAINT: {
            break;
        }
        case WM_CLOSE: {
            DestroyWindow(hwnd);
            break;
        }
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    memset(&wc,0,sizeof(wc));
    wc.cbSize         = sizeof(WNDCLASSEX);
    wc.lpfnWndProc     = WndProc; /* This is where we will send messages to */
    wc.hInstance     = hInstance;
    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
    wc.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","涂鸦板",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
    if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    while(GetMessage(&Msg, NULL, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

 

posted @ 2013-02-21 18:04  海豚爸爸  阅读(224)  评论(0编辑  收藏  举报