I am just learning win32 ui and scraped an example tool bar off of Microsoft's documentation. When I run this plain example, the result is that the tool bar is too low. I can't figure out why this is.
Here is my code:
#include <Windows.h>#include <CommCtrl.h>#include <cstdio>HIMAGELIST g_hImageList = NULL;const int IDM_NEW = 201;const int IDM_OPEN = 202;const int IDM_SAVE = 203;HWND MainWindowToolbar( HWND Window ){ // Declare and initialize local constants. const int ImageListID = 0; const int numButtons = 3; const int bitmapSize = 16; RECT MainWindowRect; GetClientRect(Window, &MainWindowRect); const DWORD buttonStyles = BTNS_AUTOSIZE; // Create the toolbar. HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0, Window, NULL, GetModuleHandle(NULL), NULL); if (hWndToolbar == NULL) return NULL; // Create the image list. g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps. ILC_COLOR16 | ILC_MASK, // Ensures transparent background. numButtons, 0); // Set the image list. SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ImageListID, (LPARAM)g_hImageList); // Load the button images. SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL); // Initialize button info. // IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants. TBBUTTON tbButtons[numButtons] = { { MAKELONG(STD_FILENEW, ImageListID), IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" }, { MAKELONG(STD_FILEOPEN, ImageListID), IDM_OPEN, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"}, { MAKELONG(STD_FILESAVE, ImageListID), IDM_SAVE, 0, buttonStyles, {0}, 0, (INT_PTR)L"Save"} }; // Add buttons. SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0); SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons); // Resize the toolbar, and then show it. SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); ShowWindow(hWndToolbar, TRUE); return hWndToolbar;}HWND TreeViewHandle;LRESULT CALLBACK MainWindowCallback( HWND Window, UINT Message, WPARAM WParam, LPARAM LParam){ LRESULT Result; switch(Message) { case WM_CREATE: { MainWindowToolbar( Window ); } break; case WM_SIZE: { OutputDebugString("WM_SIZE\n"); } break; case WM_DESTROY: { PostQuitMessage(0); } break; case WM_CLOSE: { OutputDebugString("WM_CLOSE\n"); } break; case WM_ACTIVATEAPP: { OutputDebugString("WM_ACTIVATEAPP\n"); } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(Window, &ps); FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1)); EndPaint(Window, &ps); } break; } return DefWindowProc(Window, Message, WParam, LParam);}int CALLBACK WinMain( HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR lpCmdLine, int nShowCmd){ WNDCLASS WindowClass = {}; WindowClass.style = CS_HREDRAW | CS_VREDRAW; WindowClass.lpfnWndProc = MainWindowCallback; WindowClass.hInstance = Instance; WindowClass.lpszMenuName = "mainMenu"; WindowClass.lpszClassName = "MainWindow"; RegisterClass(&WindowClass); HWND Window = CreateWindowEx( 0, WindowClass.lpszClassName,"Win32 Test App", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, Instance, 0 ); if (Window == nullptr) { printf("Error CreateWindow()\n"); exit(1); } ShowWindow(Window, nShowCmd); MSG Message; while ( GetMessage(&Message, 0, 0, 0) > 0 ) { TranslateMessage(&Message); DispatchMessage(&Message); }}
This is roughly what it produces. Why does windows put so much space above the tool bar?