创build螺纹钢控制和带钢筋工具带入钢筋

我使用这个代码来创build一个钢筋控制,并在钢筋中引入带有工具栏的乐队。
但是当窗口出现时,我看不到工具栏。 当我检查钢筋的高度,在这行代码: 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); } } 

从评论如下,解决方案是:

 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

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

请注意以下关于提供的代码以及:

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

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

     TBBUTTON tbb[3]