Win32垂直工具栏问题
我正在尝试按照 这些创建垂直工具栏 说明,但它没有按预期工作。我尝试这样创建工具栏:
HIMAGELIST g_hImageList = NULL;
const int ImageListID = 0;
#define TB_TRM_NUM_BUTTONS 2
const int bitmapSize = 16;
const BYTE buttonStyles = 0;
TBBUTTON tbButtons[TB_TRM_NUM_BUTTONS] =
{
{ STD_DELETE, IDM_TRMVIEW_CLEAR, TBSTATE_ENABLED | TBSTATE_WRAP, buttonStyles, {0}, 0, 0},
{ STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED | TBSTATE_WRAP, buttonStyles, {0}, 0, 0},
};
// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, CCS_VERT | WS_CHILD | WS_VISIBLE , 0, 0, 0, 0, hWndParent, (HMENU)(UINT_PTR)ID, GetModuleHandle(0), NULL);
if (hWndToolbar == NULL)
return 0;
// Create the image list.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps.
ILC_COLOR16 | ILC_MASK, // Ensures transparent background.
TB_TRM_NUM_BUTTONS, 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);
// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)TB_TRM_NUM_BUTTONS, (LPARAM)&tbButtons);
创建一个太宽的工具栏:
当我添加 TBSTYLE_WRAPPABLE
时,工具栏变为正确的宽度,但不会垂直包裹按钮:
<一个href="https://i.sstatic.net/1pb0g.png" rel="nofollow noreferrer">
要调整工具栏的大小,我将 TB_AUTOSIZE
发送到工具栏。
我还想向工具栏添加扩展样式:
SendMessage(hWndToolbar, TB_SETEXTENDEDSTYLE, 0, (LPARAM)(TBSTYLE_EX_MIXEDBUTTONS | TBSTYLE_EX_VERTICAL));
但是,这会将工具栏发送到客户区域的另一侧并保持水平。
如何使工具栏正确垂直?
谢谢
I'm trying to create a vertical toolbar as per these instructions but it's not working as expected. My attempt to create the toolbar thus:
HIMAGELIST g_hImageList = NULL;
const int ImageListID = 0;
#define TB_TRM_NUM_BUTTONS 2
const int bitmapSize = 16;
const BYTE buttonStyles = 0;
TBBUTTON tbButtons[TB_TRM_NUM_BUTTONS] =
{
{ STD_DELETE, IDM_TRMVIEW_CLEAR, TBSTATE_ENABLED | TBSTATE_WRAP, buttonStyles, {0}, 0, 0},
{ STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED | TBSTATE_WRAP, buttonStyles, {0}, 0, 0},
};
// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, CCS_VERT | WS_CHILD | WS_VISIBLE , 0, 0, 0, 0, hWndParent, (HMENU)(UINT_PTR)ID, GetModuleHandle(0), NULL);
if (hWndToolbar == NULL)
return 0;
// Create the image list.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps.
ILC_COLOR16 | ILC_MASK, // Ensures transparent background.
TB_TRM_NUM_BUTTONS, 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);
// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)TB_TRM_NUM_BUTTONS, (LPARAM)&tbButtons);
Creates a toolbar that's too wide:
When I add TBSTYLE_WRAPPABLE
the toolbar becomes the correct width but doesn't wrap the buttons vertically:
To resize the toolbar I'm sending TB_AUTOSIZE
to the toolbar.
I'd also like to add extended styles to the toolbar:
SendMessage(hWndToolbar, TB_SETEXTENDEDSTYLE, 0, (LPARAM)(TBSTYLE_EX_MIXEDBUTTONS | TBSTYLE_EX_VERTICAL));
However this sends the toolbar to the otherside of the client area and keeps it horizontal.
How can I get the toolbar to go vertical properly?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 文档 :
创建垂直工具栏的关键是在窗口样式中包含CCS_VERT,并为每个按钮设置TBSTATE_WRAP样式。
CreateWindowEx 函数没有用于指定工具栏大小的参数。工具栏窗口过程自动设置工具栏窗口的大小和位置。高度基于工具栏中按钮的高度。宽度与父窗口客户区的宽度相同。如果您想更改自动大小设置,您可以发送 TB_SETBUTTONSIZE 消息。
According to the Doc:
The key to creating a vertical toolbar is to include CCS_VERT in the window style, and to set the TBSTATE_WRAP style for each button.
The CreateWindowEx function does not have parameters for specifying toolbar size. The toolbar window procedure automatically sets the size and position of the toolbar window. The height is based on the height of the buttons in the toolbar. The width is the same as the width of the parent window's client area. If you want to change the automatic size settings, you could send a TB_SETBUTTONSIZE message.