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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-08 14:50:02

这个例子展示了用SetWindoworgEx与滚动条结合起来使用, 我发了很长时间才明白窗口,视口这些玩意。
代码看起来非常简单, 所以我就没有写过多的注释了:

源代码:

#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("Draw 100 circles");
    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) {
    static int cxClient, cyClient;
    HDC hdc;
    PAINTSTRUCT ps;
    SCROLLINFO si;
    int i, j, iVertPos, iHorzPos;
    HBRUSH hBrush;
    
    switch(message) {
        
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        si.cbSize = sizeof(si);
        si.fMask = SIF_PAGE | SIF_RANGE;
        si.nPage = cyClient;
        si.nMin = 0;
        si.nMax = 10 * cyClient;
        SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
        si.nPage = cxClient;
        si.nMin = 0;
        si.nMax = 10 * cxClient;
        SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
        return 0;
        
    case WM_VSCROLL:
        si.cbSize = sizeof(si);
        si.fMask = SIF_ALL;
        GetScrollInfo(hwnd, SB_VERT, &si);
        iVertPos = si.nPos;
        
        switch(LOWORD(wParam)) {
        case SB_LINEUP:
            si.nPos -= 10;
            break;
        case SB_LINEDOWN:
            si.nPos += 10;
            break;
        case SB_PAGEUP:
            si.nPos -= si.nPage;
            break;
        case SB_PAGEDOWN:
            si.nPos += si.nPage;
            break;
        case SB_THUMBTRACK:
            si.nPos = si.nTrackPos;
            break;
        default:
            break;
        }
        
        si.fMask = SIF_POS;
        SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
        GetScrollInfo(hwnd, SB_VERT, &si);
        if(si.nPos != iVertPos) {
            ScrollWindow(hwnd, 0, iVertPos - si.nPos, NULL, NULL);
            UpdateWindow(hwnd);
        }
        return 0;
        
        case WM_HSCROLL:
            si.cbSize = sizeof(si);
            si.fMask = SIF_ALL;
            GetScrollInfo(hwnd, SB_HORZ, &si);
            iHorzPos = si.nPos;
            
            switch(LOWORD(wParam)) {
            case SB_LINELEFT:
                si.nPos -= 20;
                break;
            case SB_LINERIGHT:
                si.nPos += 20;
                break;
            case SB_PAGELEFT:
                si.nPos -= cxClient;
                break;
            case SB_PAGERIGHT:
                si.nPos += cxClient;
                break;
            case SB_THUMBTRACK:
                si.nPos = si.nTrackPos;
            default:
                break;
            }
            
            si.fMask = SIF_POS;
            SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
            GetScrollInfo(hwnd, SB_HORZ, &si);
            if(si.nPos != iHorzPos) {
                ScrollWindow(hwnd, iHorzPos - si.nPos, 0, NULL, NULL);
                UpdateWindow(hwnd);
            }
            return 0;
            
            case WM_PAINT:
                hdc = BeginPaint(hwnd, &ps);
                si.cbSize = sizeof(si);
                si.fMask = SIF_POS;
                GetScrollInfo(hwnd, SB_VERT, &si);
                iVertPos = si.nPos;
                GetScrollInfo(hwnd, SB_HORZ, &si);
                iHorzPos = si.nPos;
                SetWindowOrgEx(hdc, iHorzPos, iVertPos, NULL);

                hBrush = CreateSolidBrush(RGB(0, 100, 100));
                SelectObject(hdc, hBrush);
                
                for(i = 0; i < 10; i++)
                    for(j = 0; j < 10; j++)
                    {
                        Ellipse(hdc, j * cxClient + cxClient / 4, i * cyClient + (cyClient / 2 - cxClient / 4),
                            j * cxClient + 3 * cxClient / 4, i * cyClient + (cyClient / 2 + cxClient / 4));
                    }
                    EndPaint(hwnd, &ps);
                    DeleteObject(hBrush);
                    return 0;
                    
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(821) | 评论(0) | 转发(0) |
0

上一篇:画刷练习

下一篇:RECT结构及其操作展示

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