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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-07 21:22:44

以前总不是太明白这几者之间的关系:SetBkMode, SetBkColor, wndclass.hbrBackground,
说实在的, 还是拿程序说话, 写个程序就能明白了……
我们以前一次的一个程序来说明:

/* 画笔练习程序, by netrookie, ChinaUnix.
 * 请参考《windows程序设计》第五章:图形基础,画笔部分
 * 此程序通过一个switch结构, 告诉我们怎样创建画笔和删除画笔。
 */

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    static TCHAR szAppName[] = TEXT("MyPen");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    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(GRAY_BRUSH);
    wndclass.lpszMenuName = NULL;
    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)) {
        DispatchMessage(&msg);
        TranslateMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static int cxClient, cyClient, i;
    HDC hdc;
    PAINTSTRUCT ps;
    HPEN hPen;
    LOGPEN logPen;

    switch(message) {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        i = 0;
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        SetBkColor(hdc, RGB(0, 255, 255));

        // 注释运行和不注释它运行
        // SetBkMode(hdc, TRANSPARENT);

        switch(i) {
        case 0:
            hPen = CreatePen(PS_SOLID, 2, RGB(10, 10, 10));
            break;
        case 1:
            hPen = CreatePen(PS_DASH, 1, RGB(20, 20, 20));
            break;
        case 2:
            hPen = CreatePen(PS_DOT, 1, RGB(255, 30, 30));
            break;
        case 3:
            hPen = CreatePen(PS_DASHDOT, 1, RGB(40, 40, 40));
            break;
        case 4:
            hPen = CreatePen(PS_DASHDOTDOT, 1, RGB(50, 50, 50));
            break;
        case 5:
            logPen.lopnStyle = PS_SOLID;
            logPen.lopnColor = RGB(0, 60, 60);
            /*
             * 此处是非常有意思的,下面有说明
             */

            logPen.lopnWidth.x = 5;
            hPen = CreatePenIndirect(&logPen);
            break;
        default:
            hPen = GetStockObject(BLACK_PEN);
            break;
        }

        SelectObject(hdc, hPen);
        MoveToEx(hdc, 0, (i + 1) * cyClient / 10, NULL);
        LineTo(hdc, cxClient, (i + 1) * cyClient / 10);

        TextOut(hdc, i* cxClient / 7, 4 * cyClient / 5, "windows", lstrlen("windows"));
        if(i++ <= 5)
            InvalidateRect(hwnd, NULL, FALSE);
        EndPaint(hwnd, &ps);
        // 删除GDI对象, 释放内存!!

        DeleteObject(hPen);
        return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

一运行终于明白了
阅读(6305) | 评论(0) | 转发(0) |
0

上一篇:创建画笔练习程序

下一篇:画刷练习

给主人留下些什么吧!~~