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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-11 23:35:12

我靠, 这个接口太简单了:

                case WM_MOUSEWHEEL:
                    acc += (short)HIWORD(wParam);
                    while(acc >= 40) {
                        SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
                        acc -= 40;
                    }
                    while(acc <= -40) {
                        SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
                        acc += 40;
                    }
                    return 0;

作者想得更宽广, 当然也加重了读者的负担, 我觉得没必要, 我们就认为:

WHEEL_DELTA为120
delta为40,滑动一下代表3行

这样问题就简单了。
还是以前的例子,加上了滑轮接口:

#define WINVER 0x0500
#define _WIN32_WINNT 0x0500 // for Mouse Wheel support

#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("keyboard interface demo");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.lpfnWndProc = WndProc;
    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;
    static int acc;
    
    switch(message) {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        
        si.cbSize = sizeof(si);
        si.fMask = SIF_RANGE | SIF_PAGE;
        si.nMin = 0;
        si.nMax = 10 * cyClient;
        si.nPage = cyClient;
        SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
        si.cbSize = sizeof(si);
        si.fMask = SIF_RANGE | SIF_PAGE;
        si.nMin = 0;
        si.nMax = cxClient * 10;
        si.nPage = 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_TOP:
            si.nPos = si.nMin;
            break;
        case SB_BOTTOM:
            si.nPos = si.nMax;
            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 -= 15;
                break;
            case SB_LINERIGHT:
                si.nPos += 15;
                break;
            case SB_PAGELEFT:
                si.nPos -= si.nPage;
                break;
            case SB_PAGERIGHT:
                si.nPos += si.nPage;
                break;
            case SB_THUMBTRACK:
                si.nPos = si.nTrackPos;
                break;
            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_KEYDOWN:
                switch(wParam) {
                case VK_HOME:
                    SendMessage(hwnd, WM_VSCROLL, SB_TOP, NULL);
                    break;
                case VK_END:
                    SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, NULL);
                    break;
                case VK_PRIOR:
                    SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, NULL);
                    break;
                case VK_NEXT:
                    SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, NULL);
                    break;
                case VK_UP:
                    SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, NULL);
                    break;
                case VK_DOWN:
                    SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, NULL);
                    break;
                case VK_LEFT:
                    SendMessage(hwnd, WM_HSCROLL, SB_LINELEFT, NULL);
                    break;
                case VK_RIGHT:
                    SendMessage(hwnd, WM_HSCROLL, SB_LINERIGHT, NULL);
                    break;
                default:
                    break;
                }
                return 0;
                
                case WM_MOUSEWHEEL:
                    acc += (short)HIWORD(wParam);
                    while(acc >= 40) {
                        SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
                        acc -= 40;
                    }
                    while(acc <= -40) {
                        SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
                        acc += 40;
                    }
                    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);
                    for(i = 0; i < 10; i++)
                        for(j = 0; j < 10; j++) {
                            Ellipse(hdc, cxClient * i, cyClient * j, cxClient * (i + 1), cyClient * (j + 1));
                        }
                        EndPaint(hwnd, &ps);
                        return 0;
                        
                case WM_DESTROY:
                    PostQuitMessage(0);
                    return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


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