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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-07 21:02:45

画笔是一类GDI对象, 这个小例子展示了如何创建与删除画笔:
源代码:

/* 画笔练习程序, 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(WHITE_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);
        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);
        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);
}


上面一个地方是比较有意思的, 我们看看LOGPEN结构的定义:

typedef struct tagLOGPEN {
  UINT lopnStyle;
  POINT lopnWidth;
  COLORREF lopnColor;
}LOGPEN, *PLOGPEN;

lopnWidth竟然是一个POINT结构!!!!!!!
那我们怎么给它赋什呢?
MSDN又给出这样一句话:

lopnWidth

    The POINT structure that contains the pen width, in logical units. If the pointer member is NULL, the pen is one pixel wide on raster devices. The y member in the POINT structure for lopnWidth is not used.

这就可以理解为什么我这样给lopnWidth赋值了……
阅读(952) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~