我刚刚学习 win32 ui,并从 Microsoft 文档中抓取了一个示例工具栏。当我运行这个简单的示例时,结果是工具栏太低。我不明白这是为什么。
这是我的代码:
#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);
}
}
这大概就是它产生的结果。为什么 Windows 在工具栏上方留了这么多空间?
问题似乎是我没有从 WM_CREATE 或 WM_PAINT 消息返回 0,并且仍在调用 DefaultWindowProc()。
工作代码:
case WM_CREATE:
MainWindowToolbar( Window );
return 0;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(Window, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(Window, &ps);
return 0;