Winapi 如何计算SCROLLINFO.nMax到我的窗口?

Winapi 如何计算SCROLLINFO.nMax到我的窗口?,winapi,windows-10,scrollbar,Winapi,Windows 10,Scrollbar,如何计算要设置到我的窗口的SCROLLINFO.nMax中的值?目前,我正在循环窗口中的所有控件,跟踪最小的top和最高的bottom值,以便获得整个窗口的大小,包括显示区域之外的窗口,当前未显示但仍在显示区域。但有点不对劲,我在滚动条的末尾得到了一个很大的空白区域,似乎nMax太大了。我错过了什么?一旦你完全向下滚动,它将一直显示在窗口中,直到编辑框出现。现在说话的样子: 我的尺码是这样的: int scrollHeight(void) { int mTop = 0; int

如何计算要设置到我的窗口的
SCROLLINFO.nMax
中的值?目前,我正在循环窗口中的所有控件,跟踪最小的
top
和最高的
bottom
值,以便获得整个窗口的大小,包括显示区域之外的窗口,当前未显示但仍在显示区域。但有点不对劲,我在滚动条的末尾得到了一个很大的空白区域,似乎
nMax
太大了。我错过了什么?一旦你完全向下滚动,它将一直显示在窗口中,直到编辑框出现。现在说话的样子:

我的尺码是这样的:

int scrollHeight(void)
{
    int mTop = 0;
    int mBottom = 0;
    RECT rt = {0};

    for(int i = 0; i < MAX_CONTROLS; i++)
    {
        HWND h = allControls[i];
        if(h == NULL) break;

        memset(&rt, 0, sizeof(RECT));
        if(!GetWindowRect(h, &rt))
        {
            ErrorExit(NAMEOF(scrollHeight), __LINE__, __FILENAME__);
        }

        mBottom = my_max(mBottom, rt.bottom);
        mTop = my_min(mTop, rt.top);
    }

    return mBottom - mTop;
}
void setUpScrollBar(HWND hwnd)
{
    RECT rc = { 0 };
    GetClientRect(hwnd, &rc);
    SCROLLINFO si = { 0 };
    si.cbSize = sizeof(SCROLLINFO);
    si.fMask = SIF_ALL;
    si.nMin = 0;
    si.nMax = scrollHeight();
    si.nPage = (rc.bottom - rc.top);
    si.nPos = 0;
    si.nTrackPos = 0;
    SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
}
完整代码:

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "Comctl32.lib")
#pragma comment(lib, "Gdi32.lib")

#define WIN32_LEAN_AND_MEAN
#define UNICODE
#define _UNICODE

#include <windows.h>
#include <Commctrl.h>
#include <crtdbg.h>
#include <strsafe.h>
#include <string.h>
#include <assert.h>

#ifdef UNICODE
#define STRSPLIT wcsrchr
#else
#define STRSPLIT strrchr
#endif

#define __FILENAME__ (STRSPLIT(TEXT(__FILE__), '/') ? STRSPLIT(TEXT(__FILE__), '/') + 1 : TEXT(__FILE__))
#define NAMEOF(s) TEXT(#s)
#define COUNTOF(a) (sizeof(a)/sizeof(a[0]))

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK CreateTabProc(HWND, UINT, WPARAM, LPARAM);
void ErrorExit(LPWSTR lpszFunction, int line, LPWSTR filename);
void InitComControls();
void ErrorExit(LPWSTR lpszFunction, int line, LPWSTR filename);
DWORD ShowLastError(LPWSTR lpszFunction, int line, LPWSTR filename);
void InitComControls();
void CreateTab(HWND hwnd);
void InsertTabItem(HWND tabHwnd, UINT id, LPWSTR text);
void CreateButtons(HWND hwnd);
RECT GetLocalCoordinates(HWND hWnd);
int scrollHeight(void);
int getHeight(HWND control);
void setUpScrollBar(HWND hwnd);
void pushControl(HWND);
inline int my_max(int a, int b);
inline int my_min(int a, int b);
void showText(HWND hwnd);

HINSTANCE ghInstance;
HWND hTab;
HWND hLabel1, hLabel2;
HWND hEdit1;

#define MAX_CONTROLS 8

static const wchar_t *title[] = { L"Button A1", L"Button B2", L"Button C3",
                                  L"Button D4", L"Button E5", L"Button F6",
                                  L"Button G" , L"Button 001", L"Button 002",
                                  L"Button 003", L"Button 004",
                                  L"Button 005", L"Button 006" };
HWND hButton[sizeof(title)/sizeof(title[0])] = {0};
HWND allControls[MAX_CONTROLS];
int allControls_indx = 0;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR pCmdLine, int nCmdShow)
{

    MSG  msg = {0};
    HWND hwnd;
    WNDCLASSW wc = {0};

    wc.lpszClassName = L"Window";
    wc.hInstance     = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    
    InitComControls();
    if(!RegisterClass(&wc)) {
        ErrorExit(NAMEOF(RegisterClass), __LINE__, __FILENAME__);
    }

    int width = 500;
    int height = 350/2; // half than the usual size, so that the scrollbar show up and we can test it
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int cx = (screenWidth - width) / 2;
    int cy = (screenHeight - height) / 2;
    hwnd = CreateWindowW(wc.lpszClassName, L"Window",
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                        cx, cy, width, height, NULL, NULL, 
                        hInstance, NULL);
    pushControl(hwnd); // push main window too
    ghInstance = hInstance;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(hwnd, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static int g_scrollY;

  switch(msg)
  {
      case WM_CREATE:
        hLabel1 = CreateWindowW(L"Static", L"This is label 1...",
          WS_VISIBLE | WS_CHILD | WS_TABSTOP,
          50, 10, 130, 25, hwnd, (HMENU) 18, NULL, NULL);
        pushControl(hLabel1);
        hLabel2 = CreateWindowW(L"Static", L"This is label 2...",
          WS_VISIBLE | WS_CHILD | WS_TABSTOP,
          50, 40, 130, 25, hwnd, (HMENU) 19, NULL, NULL);
        pushControl(hLabel2);
        CreateTab(hwnd);
        CreateButtons(hwnd);
        setUpScrollBar(hwnd);
          //ChangeToDefaultFont(hwnd);
      break;

      case WM_VSCROLL:
      {
        int 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;
        
        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;
        HDC hdc = GetDC(hwnd);
        LPtoDP(hdc, &pt, 1);
        ReleaseDC(hwnd, hdc);
        ScrollWindow(hwnd, 0, -pt.y, NULL, NULL);
        g_scrollY = pos;
        return 0;
    }
      
    break;

      case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
  }

  return DefWindowProc(hwnd, msg, wParam, lParam);
}

void pushControl(HWND hwnd)
{
    if(allControls_indx > MAX_CONTROLS) {
        assert(!"no room for extra controls");
    }

    allControls[allControls_indx++] = hwnd;
}

void CreateTab(HWND hwnd)
{
  hTab =
   CreateWindow(WC_TABCONTROLW, NULL,
            WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_TABSTOP,
            100, 80, 400, 250,
            hwnd,
            (HMENU) 1,
            NULL,
            NULL);
    InsertTabItem(hTab, 2, L"Tab 1");
    InsertTabItem(hTab, 3, L"Tab b");

    pushControl(hTab);
}

void CreateButtons(HWND hwnd)
{
    RECT rt = GetLocalCoordinates(hTab);
    TabCtrl_AdjustRect(hTab, FALSE, &rt);


    RECT rt2 = {0};
    GetWindowRect(hTab, &rt2);
    int tab_width = rt2.right - rt2.left;
    int tab_height = rt2.bottom - rt2.top;
    
    int id = 4;

    const int cy_breakSize = 25;
    int cx_initPos = rt.left;
    int cy_initPos = rt.top;
    int cx = cx_initPos;
    int cy = cy_initPos;
    const int button_width = 80;
    const int button_height = 25;
    const int cx_margin = 10;
    int nMaxButtonPerRow = tab_width / (button_width + cx_margin);

    for(int i = 0; i < sizeof(title)/sizeof(title[0]); ++i)
    {
        if(i != 0 && (i % nMaxButtonPerRow) == 0) {
            cy += cy_breakSize;
            cx = cx_initPos;
        }

        hButton[i] =
        CreateWindow(L"button", title[i], 
                    WS_VISIBLE | WS_CHILD | WS_TABSTOP | ES_AUTOHSCROLL,
                    cx, 
                    cy,
                    button_width,
                    button_height,
                    hwnd, (HMENU) id++, NULL, NULL);
        cx += button_width;
    }

    const int edit_width = 180;
    const int edit_height = 25;
    hEdit1 = CreateWindow(L"Edit", L"Hello, world!",
                          WS_VISIBLE | WS_CHILD | WS_BORDER,
                          cx,
                          // put below tab control's display area
                          getHeight(hTab) + cx,
                          edit_width,
                          edit_height,
                          hwnd,
                          (HMENU) id++,
                          NULL, NULL);
    pushControl(hEdit1);
    cx += edit_width;
}

void setUpScrollBar(HWND hwnd)
{
    RECT rc = { 0 };
    GetClientRect(hwnd, &rc);
    SCROLLINFO si = { 0 };
    si.cbSize = sizeof(SCROLLINFO);
    si.fMask = SIF_ALL;
    si.nMin = 0;
    si.nMax = scrollHeight();
    si.nPage = (rc.bottom - rc.top);
    si.nPos = 0;
    si.nTrackPos = 0;
    SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
}

int getHeight(HWND control)
{
    RECT rt;

    if(!GetWindowRect(control, &rt)) {
        ErrorExit(NAMEOF(getHeight), __LINE__, __FILENAME__);
    }

    return rt.bottom - rt.top;
}

int scrollHeight(void)
{
    int mTop = 0;
    int mBottom = 0;
    RECT rt = {0};

    for(int i = 0; i < MAX_CONTROLS; i++)
    {
        HWND h = allControls[i];
        if(h == NULL) break;

        memset(&rt, 0, sizeof(RECT));
        if(!GetWindowRect(h, &rt))
        {
            ErrorExit(NAMEOF(scrollHeight), __LINE__, __FILENAME__);
        }

        mBottom = my_max(mBottom, rt.bottom);
        mTop = my_min(mTop, rt.top);
    }

    return mBottom - mTop;
}

inline int my_max(int a, int b)
{
    return a > b ? a : b;
}

inline int my_min(int a, int b)
{
    return a < b ? a : b;
}

RECT GetLocalCoordinates(HWND hWnd)
{
    RECT Rect;
    GetWindowRect(hWnd, &Rect);
    MapWindowPoints(HWND_DESKTOP, GetParent(hWnd), (LPPOINT) &Rect, 2);
    return Rect;
}

void InsertTabItem(HWND tabHwnd, UINT id, LPWSTR text)
{
    TCITEMW tci = {0};
    tci.mask = TCIF_TEXT;
    tci.pszText = text;
    tci.cchTextMax = lstrlenW(text);
    SendMessage(tabHwnd, TCM_INSERTITEM, id, (LPARAM) &tci);
}

void InitComControls()
{
    INITCOMMONCONTROLSEX icex;
    /* initialize this component is required to use tab control,
        it seems.
    */
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_TAB_CLASSES;
    InitCommonControlsEx(&icex);
}

// display the error message from last error seen then
// exit the application, with that last error code seen.
// to test function, do something like:
//       if(!GetProcessId(NULL))
//          errorExit(TEXT("GetProcessId"));
// not quite a unittest but yeah.
void ErrorExit(LPWSTR lpszFunction, int line, LPWSTR filename)
{
    DWORD dw = ShowLastError(lpszFunction, line, filename);
    ExitProcess(dw);
}

DWORD ShowLastError(LPWSTR lpszFunction, int line, LPWSTR filename)
{
    #define MAX_DIGITS 16

   /* 
    * NOTE!!: calling GetLastError() must be done before calling
    * any other function, that would reset the GetLastError(), making
    * this function report error about the wrong function.
    */
    DWORD dw = GetLastError();
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0,
        NULL
    );

    lpDisplayBuf = (LPVOID) LocalAlloc(LMEM_ZEROINIT, 
            (lstrlen((LPCTSTR)lpMsgBuf) +
            lstrlen((LPCTSTR)lpszFunction) + 40 +
            (line > 0 ? MAX_DIGITS : 0) +
            (filename != NULL ? lstrlen(filename) : 0)) *
            sizeof(TCHAR)
    );
    StringCchPrintf((LPTSTR)lpDisplayBuf,
                    LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                    TEXT("%s failed with %d: %s"),
                    lpszFunction, dw, lpMsgBuf
    );
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    return dw;
}
#pragma注释(lib,“user32.lib”)
#pragma注释(lib,“Comctl32.lib”)
#pragma注释(lib,“Gdi32.lib”)
#定义WIN32_精益_和_平均值
#定义UNICODE
#定义UNICODE
#包括
#包括
#包括
#包括
#包括
#包括
#ifdef UNICODE
#定义STRSPLIT wcsrchr
#否则
#定义STRSPLIT strrchr
#恩迪夫
#定义uuu文件名uuuu(STRSPLIT(TEXT(uuuu文件),“/”)?STRSPLIT(TEXT(uuu文件),“/”)+1:TEXT(uu文件))
#定义文本的名称(#s)
#定义(a)(sizeof(a)/sizeof(a[0])的计数
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
LRESULT回调CreateTabProc(HWND、UINT、WPARAM、LPARAM);
void ErrorExit(LPWSTR lpszFunction,int行,LPWSTR文件名);
void InitComControls();
void ErrorExit(LPWSTR lpszFunction,int行,LPWSTR文件名);
DWORD ShowLastError(LPWSTR LPSZ函数,int行,LPWSTR文件名);
void InitComControls();
void CreateTab(HWND-HWND);
void InsertTabItem(HWND tabHwnd、UINT id、LPWSTR text);
无效创建按钮(HWND HWND);
RECT GetLocalCoordinates(HWND-HWND);
内部滚动高度(无效);
int getHeight(HWND控制);
无效设置滚动条(HWND HWND);
无效推力控制(HWND);
内联int my_max(int a,int b);
内联int my_min(int a,int b);
无效显示文本(HWND HWND);
例如,HINSTANCE;
HWND-hTab;
HWND hLabel1,hLabel2;
HWND-hEdit1;
#定义MAX_控件8
静态常量wchar_t*标题[]={L“按钮A1”、L“按钮B2”、L“按钮C3”,
L“按钮D4”,L“按钮E5”,L“按钮F6”,
L“按钮G”,L“按钮001”,L“按钮002”,
L“按钮003”,L“按钮004”,
L“按钮005”,L“按钮006”};
HWND hButton[sizeof(title)/sizeof(title[0])]={0};
HWND allControls[最大控制];
int-allControls_indx=0;
int WINAPI wWinMain(HINSTANCE HINSTANCE、HINSTANCE hPrevInstance、,
PWSTR pCmdLine,int nCmdShow)
{
MSG={0};
HWND-HWND;
WNDCLASSW wc={0};
wc.lpszClassName=L“窗口”;
wc.hInstance=hInstance;
wc.hbrBackground=GetSysColorBrush(颜色面);
wc.lpfnWndProc=WndProc;
wc.hCursor=加载光标(0,IDC_箭头);
InitComControls();
如果(!注册表类(&wc)){
ErrorExit(注册表类的名称),\uuuuu行\uuuuuuuuuuuuu文件名\uuuuuuuuu);
}
整数宽度=500;
int height=350/2;//比通常大小的一半,这样滚动条就会显示出来,我们可以测试它
int screenWidth=GetSystemMetrics(SM_CXSCREEN);
int screenHeight=GetSystemMetrics(SM_CYSCREEN);
int cx=(屏幕宽度-宽度)/2;
int cy=(屏幕高度-高度)/2;
hwnd=CreateWindowW(wc.lpszClassName,L“窗口”,
WS|U重叠窗口| WS|U可见,
cx、cy、宽度、高度、NULL、NULL、,
hInstance,NULL);
pushControl(hwnd);//也按主窗口
ghInstance=hInstance;
while(GetMessage(&msg,NULL,0,0))
{
如果(!IsDialogMessage(hwnd和msg))
{
翻译信息(&msg);
发送消息(&msg);
}
}
返回(int)msg.wParam;
}
LRESULT回调WndProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
静态int g_滚动;
开关(msg)
{
案例WM_创建:
hLabel1=CreateWindowW(L“静态”,L“这是标签1…”,
WS|u可见| WS|u儿童| WS|u禁忌,
50,10,130,25,hwnd,(HMENU)18,NULL,NULL);
推送控制(hLabel1);
hLabel2=CreateWindowW(L“静态”,L“这是标签2…”,
WS|u可见| WS|u儿童| WS|u禁忌,
50,40,130,25,hwnd,(HMENU)19,空,空);
pushControl(hLabel2);
CreateTab(hwnd);
创建按钮(hwnd);
设置滚动条(hwnd);
//更改为默认字体(hwnd);
打破
案例WM_VSCROLL:
{
int action=LOWORD(wParam);
//HWND hScroll=(HWND)lParam;
int pos=-1;
如果(动作==SB|U拇指位置|动作==SB|U拇指轨迹){
pos=HIWORD(wParam);
}else if(action==SB\u LINEDOWN){
位置=g_滚动+30;
}否则如果(动作==SB\U阵容){
位置=g_滚动-30;
} 
如果(位置==-1)
打破
SCROLLINFO si={0};
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_POS;
si.nPos=pos;
si.nTrackPos=0;
SetScrolInfo(hwnd、SB_VERT和si、TRUE);
获取滚动信息(hwnd、SB_VERT和si);
pos=si.nPos;
点pt;
pt.x=0;
pt.y=位置-g_滚动;
HDC HDC=GetDC(hwnd);
LPtoDP(hdc和pt,1);
释放DC(hwnd、hdc);
滚动窗口(hwnd,0,-pt.y,NULL,NULL);
g_scrollY=pos;
返回0;
}
打破
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
返回DefWindowProc(hwnd、msg、wParam、lParam);
}
无效推送控制(HWND HWND)
{
如果(所有控件>最大控件){
断言(!“没有额外控制的空间”);
}
allControls[allControls_indx++]=hwnd;
}
无效创建选项卡(HWND HWND)
{
hTab=
CreateWindow(WC_TABCONTROLW,NULL,
WS|u CHILD | WS|u VISIBLE | WS|u CLIPSIBLINGS | WS|u TABSTOP,
100, 80, 400, 250,
hwnd,
(h)1,
    mTop = my_min(mTop, rt.top);
    mTop = mTop ? my_min(mTop, rt.top) : rt.top;