12滚动条

滚动条 Scrollbar


我们经常在网页、word,都会遇到可以向下或向上拉动的滚动条。那么如何实现的呢?

记住,滚动条也是窗口哦。

步骤:
1.创建水平滚动条---SBS_HORZ 代表水平滚动条
CreateWindowEx(0,"scrollbar",0,WS_CHILD|WS_VISIBLE|SBS_HORZ,4,4,200,40,hWnd,(HMENU)IDM_SCROLLBAR,hInst,0);

注意:
SBS_HORZ 代表水平滚动条 -----SBS--SCROLLBAR STYLE
SBS_VERT 代表垂直滚动条

2.设置滚动条的范围
SendMessage((HWND)lParam,SBM_SETRANGE,0,100);
滑块的初始位置
SendMessage((HWND)lParam,SBM_SETPOS,50,TRUE);

SBM-------SCROLLBAR MESSAGE

3.捕捉WM_HSCROLL的消息
case WM_HSCROLL:
switch(LOWORD(wParam))
{
case SB_LINELEFT: //点击左箭头移动
break;
case SB_LINERIGHT: //点击右箭头移动
break;
case SB_PAGEDOWN: //点击滚动条右边空白处移动
break;
case SB_PAGEUP: //点击滚动条左边空白处移动
break;
case SB_THUMBPOSITION: //拖动滚动条任意位置
break;
}


3.发送消息设置位置

SendMessage((HWND)lParam,SBM_SETPOS,SendMessage((HWND)lParam,SBM_GETPOS,0,0)+1,TRUE);
第三参数 再一次获取当前的滑块的位置,再作为参数传入,重新设定。


SendMessage((HWND)lParam,SBM_SETPOS,HIWORD(wParam),TRUE);
第三参数 再拖动任意位置时,时刻获取当前滑块的位置,及时设定。

 

同理,也可以制动垂直滚动条。
1.将SBS_HORZ改变成SBS_VERT
CreateWindowEx(0,"scrollbar",0,WS_CHILD|WS_VISIBLE|SBS_VERT,4,4,200,40,hWnd,(HMENU)IDM_SCROLLBAR,hInst,0);
2.捕捉WM_VSCROLL.
3.发送消息设置位置


另外,以上的例子是利用SendMessage()来实现,也可以通过API来实现,API只是将消息进行封装,使用函数如下:

//设置滚动条的长度
SetScrollRange(GetDlgItem(hWnd,IDM_SCROLLBAR),SBS_HORZ,0,100,TRUE);
//设置滑块的初始位置
SetScrollPos(GetDlgItem(hWnd,IDM_SCROLLBAR),SBS_HORZ,50,TRUE);
//获取当前滑块的位置
GetScrollPos((HWND)lParam,SBS_HORZ)

//右击箭头时,设置滑块的新位置,注意SetScrollPos()第三参数,实现不同的功能
SetScrollPos((HWND)lParam,SBS_HORZ,GetScrollPos((HWND)lParam,SBS_HORZ)+1,TRUE);

//任意拖动滑块
SetScrollPos((HWND)lParam,SBS_HORZ,HIWORD(wParam),TRUE);

 

源代码:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    switch (message)
    {
    case WM_CREATE:
        CreateWindowEx(0,"scrollbar",0,WS_CHILD|WS_VISIBLE|SBS_HORZ,4,4,200,40,hWnd,(HMENU)IDM_SCROLLBAR,hInst,0);
        
        SendMessage((HWND)lParam,SBM_SETRANGE,0,100);
        SendMessage((HWND)lParam,SBM_SETPOS,50,TRUE);
        
        break;
    case WM_HSCROLL:
        switch(LOWORD(wParam))
        {
        case SB_LINELEFT:
            SendMessage((HWND)lParam,SBM_SETPOS,SendMessage((HWND)lParam,SBM_GETPOS,0,0)-1,TRUE);
            break;
        case SB_LINERIGHT:
            SendMessage((HWND)lParam,SBM_SETPOS,SendMessage((HWND)lParam,SBM_GETPOS,0,0)+1,TRUE);
            break;
        case SB_PAGEDOWN:
            SendMessage((HWND)lParam,SBM_SETPOS,SendMessage((HWND)lParam,SBM_GETPOS,0,0)+10,TRUE);
            break;
        case SB_PAGEUP:
            SendMessage((HWND)lParam,SBM_SETPOS,SendMessage((HWND)lParam,SBM_GETPOS,0,0)-10,TRUE);
            break;
        case SB_THUMBPOSITION:
            SendMessage((HWND)lParam,SBM_SETPOS,HIWORD(wParam),TRUE);

        }
        break;
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // 分析菜单选择:
        switch (wmId)
        {
        case IDM_SCROLLBAR:
            switch(HIWORD(wParam))
            {
            case SB_LINELEFT:
                SetScrollPos((HWND(lParam)),SBS_HORZ,GetScrollPos((HWND(lParam)),SBS_HORZ)-1,TRUE);
            case SB_LINERIGHT:
                SetScrollPos((HWND(lParam)),SBS_HORZ,GetScrollPos((HWND(lParam)),SBS_HORZ)+1,TRUE);
            }
            break;
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: 在此添加任意绘图代码...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

 

posted @ 2017-09-03 21:48  gd_沐辰  阅读(308)  评论(0编辑  收藏  举报