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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-15 21:16:51

请参考《windows程序设计》- 子窗口控件, 关于滚动条控件的那一部分, 本代码是能过范例模仿出来的,
增加了些新的功能, 当然出没有那么麻烦, 主要是为练习而用。
源代码:

#include <windows.h>

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

    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,
        0,
        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 HBRUSH hBrush;
    static int cxChar, cyChar;
    static HWND hwndScroll, hwndStatic;
    static int iVertPos;
    TCHAR buffer[5];

    switch(message) {
    case WM_CREATE:
        cxChar = LOWORD(GetDialogBaseUnits());
        cyChar = HIWORD(GetDialogBaseUnits());
        hwndScroll = CreateWindow(TEXT("scrollbar"),
            NULL,
            WS_CHILD | WS_VISIBLE | WS_TABSTOP | SBS_VERT,
            cxChar, cyChar, 5 * cxChar, 5 * cyChar,
            hwnd,
            (HMENU)1,
            hInst,
            NULL);
        hwndStatic = CreateWindow(TEXT("static"),
            TEXT("0"),
            WS_CHILD | WS_VISIBLE | SS_CENTER,
            cxChar, 6 * cyChar, 5 * cxChar, cyChar,
            hwnd,
            (HMENU)2,
            hInst,
            NULL);
        hBrush = CreateSolidBrush(0);
        SetScrollRange(hwndScroll, SB_CTL, 0, 255, FALSE);
        SetScrollPos(hwndScroll, SB_CTL, 0, TRUE);
        return 0;


    case WM_VSCROLL:
        switch(LOWORD(wParam)) {
        case SB_LINEUP:
            iVertPos = max(0, min(iVertPos - 1, 255));
            break;

        case SB_LINEDOWN:
            iVertPos = max(0, min(iVertPos + 1, 255));
            break;

        case SB_PAGEUP:
            iVertPos = max(0, min(iVertPos - 15, 255));
            break;

        case SB_PAGEDOWN:
            iVertPos = max(0, min(iVertPos + 15, 255));
            break;

        case SB_TOP:
            iVertPos = 0;
            break;

        case SB_BOTTOM:
            iVertPos = 255;
            break;

        case SB_THUMBTRACK:
            iVertPos = HIWORD(wParam);
            break;

        default:
            break;
        }
        SetScrollPos(hwndScroll, SB_CTL, iVertPos, TRUE);
        wsprintf(buffer, "%i", iVertPos);
        SetWindowText(hwndStatic, buffer);
        hBrush = CreateSolidBrush(RGB(0, iVertPos, iVertPos));
        DeleteObject((HBRUSH)SetClassLong(hwnd, GCL_HBRBACKGROUND,
            hBrush));
        InvalidateRect(hwnd, NULL, TRUE);
        return 0;

        case WM_CTLCOLORSCROLLBAR:
            return (LRESULT)hBrush;

        case WM_CTLCOLORSTATIC:
            SetTextColor((HDC)wParam, RGB(255, 0, 0));
            SetBkColor((HDC)wParam, RGB(0, iVertPos, iVertPos));
            return (LRESULT)hBrush;

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


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

上一篇:按钮练习

下一篇:清单控件练习

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