2

我使用这段代码创建了一个 rebar 控件,并在 rebar 中引入了一个带工具栏的带。
但是当窗口出现时,我看不到工具栏。当我检查钢筋的高度时,在这行代码中:int height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;我发现钢筋的高度只有 4 个像素。

#include <windows.h> 
#include <stdlib.h>
#include <CommCtrl.h>
#pragma comment(lib, "comctl32.lib")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE instance;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    instance = hInstance;

    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style           = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc    = WndProc; 
    wcex.cbClsExtra     = 0; 
    wcex.cbWndExtra     = 0;  
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));  
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName   = NULL; 
    wcex.lpszClassName  = L"Example"; 
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    RegisterClassEx(&wcex);

    HWND hWnd = CreateWindow(L"Example", L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        500, 500, NULL, NULL, hInstance, NULL);

    // Initialize common controls.
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC   = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
    InitCommonControlsEx(&icex);

    HWND hwndRebar = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 
                    0, 0, 100, 50, hWnd, NULL, instance, NULL);

    // create toolbar
    HWND hWndToolbar = CreateWindowEx(0 , TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_TOOLTIPS,
            0, 0, 0, 0, hwndRebar, (HMENU)0, instance, NULL);

    HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR16 | ILC_MASK, 3, 0);

    SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)hImageList);
    SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    TBBUTTON tbb[4] = 
    {
        {0,0,TBSTATE_ENABLED,TBSTYLE_BUTTON},
        {1,1,TBSTATE_ENABLED,TBSTYLE_BUTTON},
        {2,2,TBSTATE_ENABLED,TBSTYLE_BUTTON},
    };

    SendMessage(hWndToolbar, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&tbb);

    SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
    ShowWindow(hWndToolbar , SW_SHOW);

    // Initialize band info.
    REBARBANDINFO rbBand = { sizeof(REBARBANDINFO) };
    rbBand.fMask  = RBBIM_STYLE | RBBIM_TEXT | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE | RBBIM_COLORS;

    rbBand.fStyle = RBBS_GRIPPERALWAYS;  

    // Get the height of the toolbar.
    DWORD dwBtnSize = (DWORD)SendMessage(hWndToolbar, TB_GETBUTTONSIZE, 0,0);

    // Set values unique to the band with the toolbar.
    rbBand.clrFore = RGB(233, 233, 233);
    rbBand.clrBack = RGB(200, 200, 200);
    rbBand.lpText = TEXT("");
    rbBand.hwndChild = hWndToolbar;
    rbBand.cyChild = LOWORD(dwBtnSize);
    rbBand.cyMinChild = LOWORD(dwBtnSize);
    rbBand.cxMinChild = 3 * HIWORD(dwBtnSize);
    // The default width is the width of the buttons.
    rbBand.cx = 0;

    // Add the band
    SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);

    // show the main window
    ShowWindow(hWnd, nCmdShow);

    // check the rebar size
    WINDOWPLACEMENT wp;
    GetWindowPlacement(hwndRebar, &wp);
    int height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;

    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);  
        DispatchMessage(&msg); 
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CREATE: 
            return 0;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
}
4

2 回答 2

3

从评论中可以看出,解决方案是:

REBARBANDINFO rbBand;
rbBand.cbSize = REBARBANDINFO_V3_SIZE;
// initialize the rest here

这似乎会影响旧版本的 Windows(特别是 XP),因为原始代码在 Windows 7 上编译和运行良好。

这在对 MSDN 页面的评论中提到:http: //msdn.microsoft.com/en-us/library/windows/desktop/bb774393.aspx

于 2013-05-22T14:51:42.207 回答
1

如前所述,需要设置 REBARBANDINFO 结构的 cbSize 成员。

请注意以下有关提供的代码的内容:

  1. 工具栏按钮上的图像不显示。创建图像列表后,您必须立即进行以下调用以加载图像:

    SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
    
  2. TBBUTTON 数组声明的大小为 4,但它只填充了 3 个项目。从技术上讲,它应该声明为:

    TBBUTTON tbb[3]
    
于 2014-04-26T06:38:47.817 回答