Chinaunix首页 | 论坛 | 博客
  • 博客访问: 527536
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-24 18:00:27

《windows程序设计》- 位图 - Sketch.c, 作者把简单的问题复杂化, 但是这是一个非常经典的程序, 以前我碰到过的难题在这里解决:怎么样保存画图的路径, 原来可以用位图来解决。
源代码:

#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    static TCHAR szAppName[] = TEXT("XZZ");
    
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = szAppName;
    wndclass.lpszClassName = szAppName;
    
    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Register failure..."),
            szAppName, MB_ICONERROR);
        return 0;
    }
    
    hwnd = CreateWindow(szAppName,
        szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);
    
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while(GetMessage(&msg, NULL, 0, 0)) {
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}


void display(int* pcxBitmap, int* pcyBitmap) {
    DEVMODE devmode;
    int num = 0;
    *pcxBitmap = *pcyBitmap = 0;
    ZeroMemory(&devmode, sizeof(DEVMODE));
    devmode.dmSize = sizeof(DEVMODE);
    
    while(EnumDisplaySettings(NULL, num++, &devmode)) {
        *pcxBitmap = max(*pcxBitmap, (int)devmode.dmPelsWidth);
        *pcyBitmap = max(*pcxBitmap, (int)devmode.dmPelsHeight);
    }
    
}
LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static int cxClient, cyClient, cxBitmap, cyBitmap;
    static HBITMAP hBitmap;
    static HDC hdcMem;
    HDC hdc;
    PAINTSTRUCT ps;
    static POINT point;
    
    switch(message) {
    case WM_CREATE:
        display(&cxBitmap, &cyBitmap);
        hdc = GetDC(hwnd);
        hdcMem = CreateCompatibleDC(hdc);
        hBitmap = CreateCompatibleBitmap(hdc, cxBitmap, cyBitmap);
        if(!hBitmap) {
            ReleaseDC(hwnd, hdc);
            DeleteDC(hdcMem);
            return 0;
        }
        SelectObject(hdcMem, hBitmap);
        PatBlt(hdcMem, 0, 0, cxBitmap, cyBitmap, WHITENESS);
        return 0;
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_LBUTTONDOWN:
        point.x = LOWORD(lParam);
        point.y = HIWORD(lParam);
        return 0;
    case WM_MOUSEMOVE:
        hdc = GetDC(hwnd);
        if(wParam & MK_LBUTTON) {
            MoveToEx(hdc, point.x, point.y, NULL);
            MoveToEx(hdcMem, point.x, point.y, NULL);
            point.x = LOWORD(lParam);
            point.y = HIWORD(lParam);
            LineTo(hdc, point.x, point.y);
            LineTo(hdcMem, point.x, point.y);
        }
        return 0;
        
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        BitBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY);
        EndPaint(hwnd, &ps);
        return 0;
        
    case WM_DESTROY:
        DeleteObject(hBitmap);
        DeleteDC(hdcMem);
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(996) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~