c++ - 未显示最终工具栏项目图标

标签 c++ winapi toolbar

我正在包装 Win32 工具栏。一切正常,只是每当包装类的用户添加分隔符时,最后一个工具栏项的图标不会显示。

考虑这个客户端代码:

    toolbarBtn tbb[] = { toolbarBtn { ID_FILE_NEW, IDI_NEW },
                            toolbarBtn { ID_FILE_OPEN, IDI_OPEN },
                            sep { },
                            toolbarBtn { ID_FILE_SAVEAS, IDI_SAVE },
                            toolbarBtn { ID_FILE_PRINT, IDI_PRINT },
                            sep { },
                            toolbarBtn { ID_EDIT_UNDO, IDI_UNDO },
                            toolbarBtn { ID_EDIT_REDO, IDI_REDO } };

this->tb = toolbar { *this, tbb, sizeof tbb / sizeof *tbb };

toolbarBtn 对象表示工具栏按钮。 sep 对象是一个分隔符,继承自 toolbarBtn 类。以下语句调用工具栏类的构造函数,创建它。对于这段代码,这是我得到的图形输出:

enter image description here

如您悬停所见,最后两个按钮存在!但是由于某种原因图标没有显示,并且图标的顺序也发生了变化。它应该是“新建”、“打开”、[分隔符]、“保存”、“打印”、[分隔符]、“撤消”和“重做”。但是“另存为”和“重做”不显示。而且我知道图标本身没有问题,因为我可以放置任何 toolbarBtn 序列,但只要有 sep 对象,那么最后一个图标就永远不会显示。

下面是相关函数/方法的实现:

toolbarBtn::toolbarBtn(int id, icon ico, BYTE state, BYTE style)
{
    ZeroMemory(this, sizeof *this);
    this->ico = ico;
    this->tbb.idCommand = id;
    this->tbb.fsState = state;
    this->tbb.fsStyle = style;
    this->tbb.iBitmap = 0;  // field will be changed by toolbar c'tor
}

// count # of buttons; no separators counted
size_t nActualButtons(const toolbarBtn btns[], size_t n)
{
    size_t n1 = n;
    for (size_t i = 0; i < n; ++i)
        if (btns[i].getTBB().fsStyle & TBSTYLE_SEP)
            --n1;
    return n1;
}
toolbar::toolbar(overlappedwindow parent, const toolbarBtn btns[], size_t n, 
                 int id)
{
    this->hwnd = CreateWindow(TOOLBARCLASSNAME, NULL, 
        WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT, 0, 0, 0, 0,
        parent.gethwnd(), (HMENU) id, GetModuleHandle(NULL), NULL);
    if (this->hwnd == NULL)
        message(L"%s: %s", __FUNCTIONW__, geterror());

    // Send the TB_BUTTONSTRUCTSIZE message, which is required for
    // backward compatibility.
    SendMessage(this->hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);

    HIMAGELIST imglist = ImageList_Create(16, 16, ILC_COLOR32, n, 0);
    if (!imglist)
        message(L"%s: %s", __FUNCTIONW__, geterror());

    for (size_t i = 0; i < n; ++i) {
        if (btns[i].getTBB().fsStyle & TBSTYLE_SEP)
            continue;     // dont add separators to image list
        if (ImageList_AddIcon(imglist, btns[i].getIcon().gethandle()) == -1)
            message(L"%s: %s", __FUNCTIONW__, geterror());
    }

    SendMessage(this->hwnd, TB_SETIMAGELIST, (WPARAM) 0, (LPARAM) imglist);

    TBBUTTON *tbb = (TBBUTTON *) calloc(n, sizeof (TBBUTTON));
    for (size_t i = 0; i < n; ++i) {
        tbb[i] = btns[i].getTBB();
        tbb[i].iBitmap = (tbb[i].fsStyle & TBSTYLE_SEP) ? 0 : i;
        if (tbb[i].fsStyle & TBSTYLE_SEP)
            tbb[i].idCommand = 0;
    }
    SendMessage(this->hwnd, TB_ADDBUTTONS, n, (LPARAM) tbb);
    free(tbb);
}

最佳答案

我认为你的问题可能与此有关:

sep { },

你基本上是这样做的:

sep {0, 0, 0, 0}

因此您的分隔符不会设置 TBSTYLE_SEP。您可能应该像这样初始化分隔符:

sep {0, 0, 0, TBSTYLE_SEP}

关于c++ - 未显示最终工具栏项目图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45760148/

相关文章:

c++ - 为什么 weak_ptr 没有比较运算符==?

C++11 gcc : explicit qualification in declaration? 标准引用?

windows - 调整示例 DLL 代码时获取无效图像

wpf - 免费矢量图标

css - -webkit-transform : transformY? 的替代方案

c++ - 在 C++ 中获取硬编码内存地址的内容

c++ - 如何使用 Visual Studio Express 2005 制作完全静态链接的 .exe?

c++ - wsprintfW 只打印几十个?

c++ - 如何限制用户只能选择某些驱动器?

c++ - 为什么 BTNS_DROPDOWN 样式会导致整个工具栏向下移动几个像素?