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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-11 23:02:40

在《windows程序设计》第7章的BLOCKOUT1.C中, 作者给我们演示了另外一种画图方法,我记得在贝塞乐曲线那一节作者也演示了一种方法,而我也写了几个小小的画图程序,但是那种方法依赖于窗口背景颜色, 看看这次作者是如何采用另外一种方法达到相同的目的:

/*
 * 这个程序是根据第七章BLOCKOUT1.C与BLOCKOUT2.C演化而来的,书上的
 * 程序过于复杂, 所以我重写了, 主要演示了SetCapture与ReleaseCapture
 * 的用法, 作者也使用了一种新的方法来进行画图, 我们将会看到SetROP2的妙用
 * by netrookie, ChinUnix
 */

#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 ("BlokOut1") ;
    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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName = NULL ;
    wndclass.lpszClassName = szAppName ;
    
    if (!RegisterClass (&wndclass))
    {
        MessageBox (NULL, TEXT ("Program requires Windows NT!"),
            szAppName, MB_ICONERROR) ;
        return 0 ;
    }
    
    hwnd = CreateWindow (szAppName, TEXT ("Mouse Button Demo"),
        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 Draw(HWND hwnd, POINT ptBeg, POINT ptEnd) {
    HDC hdc = GetDC(hwnd);
    /*
     * 将画刷与画笔的内置改变为客户区背景色的反转色
     */

    SetROP2(hdc, R2_NOT);
    /*
     * 只绘制边框,不绘制图形内部
     */

    SelectObject(hdc, GetStockObject(NULL_BRUSH));
    Ellipse(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
    ReleaseDC(hwnd, hdc);
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static POINT ptBeg, ptEnd;
    HDC hdc;
    PAINTSTRUCT ps;

    switch(message) {
    case WM_LBUTTONDOWN:
        /*
         * 我靠!如果不释放的话,连关闭应用程序都比较难。
         */

        SetCapture(hwnd);
        ptBeg.x = ptEnd.x = LOWORD(lParam);
        ptBeg.y = ptEnd.y = HIWORD(lParam);
        return 0;
    case WM_MOUSEMOVE:
        if(wParam & MK_LBUTTON) {
        /*
         * 清除上一次所画的图形
         */

        Draw(hwnd, ptBeg, ptEnd);
        ptEnd.x = LOWORD(lParam);
        ptEnd.y = HIWORD(lParam);
        Draw(hwnd, ptBeg, ptEnd);
        }
        return 0;
    case WM_LBUTTONUP:
        ptEnd.x = LOWORD(lParam);
        ptEnd.y = HIWORD(lParam);
        ReleaseCapture();
        InvalidateRect(hwnd, NULL, FALSE);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        SelectObject(hdc, CreateSolidBrush(RGB(0, 255, 0)));
        Ellipse(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
        ReleaseDC(hdc, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


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