如何在控制VC ++滚动条的Win32 API [英] how to control scrollbar in vc++ win32 api

查看:594
本文介绍了如何在控制VC ++滚动条的Win32 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有可进行其它控件,比方说按钮,位图,文本框等自定义窗口,现在的事情是,如果该项目走到窗外,我的意思是,如果我想很明显创建像20按钮它会走出去的窗口,所以我想我应该创建一个滚动条,使窗口滚动能力。显然,你不能添加WS_VSCROLL或WS_HSCROLL引起的向上和向下的箭头,它不会是即使点击,但如果你试图拖动滑块,将跳转刚刚复出顶端。所以我想我更好地创造与CreateWindow的()函数,滚动条和控制的东西,剩下的。

basically i have a custom window which can carry other controls, let's say buttons, bitmaps, text boxes and etc, now the thing is that if the items went out of window, i mean if i tried to create like 20 button obviously it will go out of window, so i thought i should create a scroll bar to make the window scroll able. obviously you can't add WS_VSCROLL or WS_HSCROLL cause the up and down arrow of it won't be even clickable but if you tried to drag the thumb it will jump just back to the top. so i thought i better create a scroll bar with createwindow() function and control the rest of stuff.

下面是code如何创建窗口本身和滚动条:
code:

Here is the code how i create the window itself and the scroll bar: Code:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, "Name", WS_OVERLAPPEDWINDOW/*WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU*/,
  CW_USEDEFAULT, 0, 1014, 585, NULL, NULL, hInstance, NULL);

 if (!hWnd)
{
  return FALSE;
}

WNDCLASSEX wcs;

wcs.cbSize          = sizeof(wcs);
wcs.lpszClassName   = szClassName;
wcs.hInstance       = GetModuleHandle(0);
wcs.lpfnWndProc     = CustWndProc;
wcs.hCursor         = LoadCursor(NULL, IDC_ARROW);
wcs.hIcon           = 0;
wcs.lpszMenuName        = 0;
wcs.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
wcs.style           = 0;
wcs.cbClsExtra      = 0;
wcs.cbWndExtra      = 0;
wcs.hIconSm         = 0;

if(!RegisterClassEx(&wcs))
{
    MessageBox(NULL, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return 0;
}

hwndCtrl = CreateWindowEx(
                     0L, // give it a standard border
                     szClassName,
                     _T("A custom control"),
                     WS_VISIBLE|WS_CHILD|WS_BORDER,
                     0, 0, 0, 0,
                     hWnd,
                     NULL, GetModuleHandle(0), CustWndProc
                   );
ShowWindow (hwndCtrl, SW_SHOW);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

这里是code处理其消息:

here is the code for handling its messages:

LRESULT CALLBACK CustWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
RECT rc = {};
GetClientRect(hwnd, &rc);
const SIZE sz = { rc.right - rc.left, rc.bottom - rc.top };
SCROLLINFO si;

switch(msg)
{
    case WM_MOUSEHOVER: 
        ::MessageBox(hwnd, "Enter", "Info", MB_OK); 

        return 0; 
case WM_CREATE:
    int w , h;
    w = 10;
    h = 10;
    HWND buttons;
    for(h=10;h<500; h+=35){
        buttons = CreateWindow("BUTTON", "How", WS_VISIBLE|WS_CHILD, w, h, 50, 30, hwnd, (HMENU)1231,NULL, NULL);
    }
    int width, height;
    width           = LOWORD(lParam);                                                   // Width Size of hWnd
    height          = HIWORD(lParam);
    Scrollbar  = CreateWindowEx(0L,
                   "SCROLLBAR",
                   NULL, // There is no text to display
                   WS_CHILD | WS_VISIBLE | SBS_VERT,
                   980,
                   47,
                   18,
                   405,
                   hWnd,
                   NULL,
                   hInst,
                   NULL
                );
    return 0;
case WM_INITDIALOG:
    ZeroMemory(&si, sizeof(si));
    si.cbSize = sizeof(si);
    si.fMask  = SIF_RANGE | SIF_PAGE | SIF_POS;
    si.nMin   = 0;
    si.nMax   = 1000;
    si.nPage  = 10;
    si.nPos   = 54;
    SetScrollInfo(Scrollbar, SB_CTL, &si, TRUE);
    return TRUE;


default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
}
return FALSE;
}

你可以看到有哪些会自动与正在创建循环和滚动条本身被创建后,一些按钮,现在我不知道如何使它向下滚动,并如此等等。
所有答复都欢迎。

as you can see there are some buttons which is automatically being created with for loop and after that the scroll bar itself is being created, now i don't know how to make it scroll down and up and so and so forth. All replies are welcomed

推荐答案

一个快速和肮脏的例子:

A quick and dirty example:

case WM_VSCROLL:
{
    auto action = LOWORD(wParam);
    HWND hScroll = (HWND)lParam;
    int pos = -1;
    if (action == SB_THUMBPOSITION || action == SB_THUMBTRACK) {
        pos = HIWORD(wParam);
    } else if (action == SB_LINEDOWN) {
        pos = g_scrollY + 30;
    } else if (action == SB_LINEUP) {
        pos = g_scrollY - 30;
    } 
    if (pos == -1)
        break;
    WCHAR buf[20];
    SCROLLINFO si = { 0 };
    si.cbSize = sizeof(SCROLLINFO);
    si.fMask = SIF_POS;
    si.nPos = pos;
    si.nTrackPos = 0;
    SetScrollInfo(hWnd, SB_VERT, &si, true);
    GetScrollInfo(hWnd, SB_VERT, &si);
    pos = si.nPos;
    POINT pt;
    pt.x = 0;
    pt.y = pos - g_scrollY;
    auto hdc = GetDC(hWnd);
    LPtoDP(hdc, &pt, 1);
    ReleaseDC(hWnd, hdc);
    ScrollWindow(hWnd, 0, -pt.y, NULL, NULL);
    g_scrollY = pos;
    return 0;
}
case WM_CREATE:
{
    for (int i = 0; i < 100; ++i) {
        auto hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"",
            WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 10, 30 * i, 250, 21, hWnd, NULL, hInst, NULL);
        wchar_t buf[10];
        StringCchPrintf(buf, 10, L"%d", i);
        SetWindowText(hEdit, buf);
    }
    RECT rc = { 0 };
    GetClientRect(hWnd, &rc);
    SCROLLINFO si = { 0 };
    si.cbSize = sizeof(SCROLLINFO);
    si.fMask = SIF_ALL;
    si.nMin = 0;
    si.nMax = 30 * 99 + 21;
    si.nPage = (rc.bottom - rc.top);
    si.nPos = 0;
    si.nTrackPos = 0;
    SetScrollInfo(hWnd, SB_VERT, &si, true);
    return 0;
}

效果:

滚动效应

全部code: http://pastebin.com/byE1xFsb

由于OP无法理解的code,这里是解释:

Since OP cannot understand the code, here is the explanation:


  1. 窗口的整个高度应为30 * 99 + 21 99为编辑数,21是编辑控件的高度。

  1. The whole height of the window should be 30 * 99 + 21. 99 is the edit count, 21 is the edit control's height.

为窗口滚动范围应 [0,30 * 99 + 21 - 客户区域高度] 30 * 99 + 21 - 客户区高度+客户区高度等于 30 * 99 + 21 这是窗口的整个高度

The scroll range for the window should be [0, 30 * 99 + 21 - client-area-height]. 30 * 99 + 21 - client-area-height + client-area-height equals to 30 * 99 + 21 which is the whole height of the window.

要确保上述滚动范围,

si.nMax = 30 * 99 + 21;
si.nPage = (rc.bottom - rc.top);


这篇关于如何在控制VC ++滚动条的Win32 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆