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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-09 12:32:20

本文主要讲下面几个函数:
FillRect, FrameRect, SetRect, InflateRect, OffsetRect, IntersectRect
其他就请看API了, 我写两个程序分别展示它们……
1.运行结果:
源代码:

#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("映像方式");
    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 | WS_VSCROLL | WS_HSCROLL,
        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;
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    HDC hdc;
    static int cxClient, cyClient;
    PAINTSTRUCT ps;
    RECT rect;
    HBRUSH hBrush;
    int i;
    switch(message) {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
        
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        SetViewportOrgEx(hdc, cxClient / 2 , cyClient / 2, NULL);
        SetRect(&rect, -cxClient / 32, -cyClient / 32, cxClient / 32, cyClient / 32);
        FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));

        for(;rect.bottom < cyClient / 2 &&
            rect.right < cxClient / 2;) {
            InflateRect(&rect, cxClient / 32, cyClient / 32);
            FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
        }
        for(i = 100;rect.bottom >= cyClient / 32 &&
            rect.right >= cxClient / 32; i += 10) {
            hBrush = CreateSolidBrush(RGB(0, i, i));
            InflateRect(&rect, -cxClient / 32, -cyClient / 32);
            FillRect(hdc, &rect, hBrush);
            DeleteObject(hBrush);
        }
        
        EndPaint(hwnd, &ps);
        return 0;
        
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

2. 运行结果:
源代码:

/*
 * RECT结构练习, by netrookie, ChinaUnix
 */

#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("rect demo");
    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)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static int cxClient, cyClient;
    HDC hdc;
    PAINTSTRUCT ps;
    int i, j;
    /*
     * rect用于保存下一个的矩形, tmpRect用于保存上一个矩形,
     * destRect用于保存上一个与下一个矩形的交集
     */

    RECT rect, tmpRect, destRect;
    HBRUSH hBrush;
    
    switch(message) {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
        
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        /*
         * 初始化矩形
         */

        SetRect(&rect, 0, 0, cxClient / 16, cyClient / 16);
        /*
         * 画第一个矩形
         */

        FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
        /*
         * 保存上一个的矩形
         */

        tmpRect = rect;
        /*
         * 设置下一次矩形
         */

        OffsetRect(&rect, cxClient / 32, cyClient / 32);
        for(i = 100; rect.right < cxClient &&
            rect.bottom < cyClient; i += 5) {
            /*
             * 创建画刷, 用于FillRect函数
             */

            hBrush = CreateSolidBrush(RGB(0, i, i));
            /*
             * 求上一个矩形与下一个矩形的交集, 保存于destRect
             */

            IntersectRect(&destRect, &tmpRect, &rect);
            /*
             * 填充上一个与下一个矩形的交集
             */

            FillRect(hdc, &destRect, hBrush);
            /*
             * 重复
             */

            FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
            tmpRect = rect;
            OffsetRect(&rect, cxClient / 32, cyClient / 32);
            DeleteObject(hBrush);
        }
        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


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