Final toolbar item icon not showing

Home / c# / Final toolbar item icon not showing

I am wrapping the Win32 toolbar. Everything works except that whenever the user of the wrapper class adds a separator, the last toolbar item’s icon does not show.

Consider this client code:toolbarBtn tbb[] = { toolbarBtn { ID_FILE_NEW, IDI_NEW },
sep { }, toolbarBtn { ID_EDIT_FIND, IDI_FIND },
toolbarBtn { ID_EDIT_UNDO, IDI_UNDO } };

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

The toolbarBtn objects represent a toolbar button. The sep object is a separator, and inherits from class toolbarBtn. The following statement calls the constructor of the toolbar class, creating it. For this code, this is what I get as graphics output:

As you can see by hover, the final button exists! But for a reason icon does not show, and the order is also changed of the icons. And I know that the icon itself is not problem, because i can put any sequence of toolbarBtns, but as long as there is a sep object, then the last icon never displays.

Here is a implementing of relevant functions/methods: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,
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);

I think your problem may be with this:sep { },

Your are essentially doing this:sep {0, 0, 0, 0}

So your separator will not have TBSTYLE_SEP set. You should probably initialize the separator like this:sep {0, 0, 0, TBSTYLE_SEP}
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *