Win32垂直工具栏问题

发布于 2025-01-11 18:34:40 字数 2086 浏览 0 评论 0原文

我正在尝试按照 这些创建垂直工具栏 说明,但它没有按预期工作。我尝试这样创建工具栏:

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:

enter image description here

When I add TBSTYLE_WRAPPABLE the toolbar becomes the correct width but doesn't wrap the buttons vertically:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为一人 2025-01-18 18:34:40

根据 文档 :

TBSTYLE_WRAPABLE:创建一个可以有多行的工具栏
按钮。工具栏按钮可以“换行”到下一行
变得太窄而无法将所有按钮包含在同一行上。当
工具栏被换行,中断将发生在最右边
分隔符或最右边的按钮(如果没有分隔符)
酒吧。必须设置此样式以在以下情况下显示垂直工具栏控件:
工具栏是垂直钢筋控件的一部分。这种风格不能
CCS_VERT结合使用。

创建垂直工具栏的关键是在窗口样式中包含CCS_VERT,并为每个按钮设置TBSTATE_WRAP样式。

CreateWindowEx 函数没有用于指定工具栏大小的参数。工具栏窗口过程自动设置工具栏窗口的大小和位置。高度基于工具栏中按钮的高度。宽度与父窗口客户区的宽度相同。如果您想更改自动大小设置,您可以发送 TB_SETBUTTONSIZE 消息

According to the Doc:

TBSTYLE_WRAPABLE: Creates a toolbar that can have multiple lines of
buttons. Toolbar buttons can "wrap" to the next line when the toolbar
becomes too narrow to include all buttons on the same line. When the
toolbar is wrapped, the break will occur on either the rightmost
separator or the rightmost button if there are no separators on the
bar. This style must be set to display a vertical toolbar control when
the toolbar is part of a vertical rebar control. This style cannot be
combined with CCS_VERT.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文