• Advertisement
Sign in to follow this  

Dynamic TBBUTTON array

This topic is 3553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I'm creating a windows shell wrapper class in c++ using the Win32 API. I'm trying to make it so that creating a toolbar button takes a single line of code. Actually, the toolbar buttons are queued in a TBBUTTON array and then created all at once later with a function call. For a visual:
// Queue 3 toolbar buttons
Shell->QueueToolbarButton(...);
Shell->QueueToolbarButton(...);
Shell->QueueToolbarButton(...);

// Create them
Shell->CreateToolbarButtons();

The problem is, I want to dynamically allocate a TBBUTTON array instead of having a static one. I've tried using a pointer and the 'new' operator, and I've tried using vectors.
TBBUTTON *tbb;

//...

tbb = new TBBUTTON[num_buttons];

// delete[] is called later, of course

Also tried this...
vector<TBBUTTON> tbb;

//...

TBBUTTON tbbTemp;

/* tbbTemp is filled here */

tbb.reserve(num_buttons);
tbb.push_back(tbbTemp);

And when I go to create the buttons later, nothing appears. Keep in mind, these are just snippets. I can post my current attempt at solving this problem if you want to see more code. I just want to know if there is a better way of doing this. I don't want to create a static TBBUTTON array because I want to just queue each toolbar button and resize the TBBUTTON array dynamically as I go along. As this is a windows shell, different applications will have different numbers of toolbar buttons, and I want to make creating new apps as easy as possible. If you can help me, thanks in advance. - Ryan

Share this post


Link to post
Share on other sites
Advertisement
When you use an array allocated with new or a vector how are you then using TB_ADDBUTTONS? An array allocated with new should be passed in as the LPARAM while a vector can be passed in like this:


vector<TBBUTTON> buttons;
...
SendMessage(hwndToolbar, TB_ADDBUTTONS, (WPARAM)buttons.size(), (LPARAM)(&buttons[0]));



Also, if SendMessage() returns FALSE after calling it with TB_ADDBUTTONS you should ensure that you are filling your TBBUTTON structures appropriately and that you have sent TB_BUTTONSTRUCTSIZE before calling TB_ADDBUTTONS.

Share this post


Link to post
Share on other sites
Amazing... simply amazing...

System crashed and I had to do a system restore.

Lost everything.

If it matters at all, I was passing the vector as the LPARAM of SendMessage(), like you showed, it resulted in a blank toolbar.

Thanks for your time anyway, guess I'll have to restart it.

- Ryan (who has the worst luck in the world)

Share this post


Link to post
Share on other sites
Well, after a couple days, I got my shell class back to where it was before. Colin Jeanne, turns out you were right. Here was the problem...


// You suggested this line for TB_ADDBUTTONS
SendMessage(hwndToolbar, TB_ADDBUTTONS, (WPARAM)buttons.size(), (LPARAM)(&buttons[0]));

// While instead I was doing this
SendMessage(hwndToolbar, TB_ADDBUTTONS, (WPARAM)buttons.size(), (LPARAM)(&buttons)); // note the missing [0]



I thought that adding in the [0] would only put the first element on the toolbar, but all of them appeared on there just fine. I'm not exactly sure why, but it worked, even though I would like to know how the latter failed. Thank you very much. Now I don't have to keep changing a global variable every time I want to add another button.

Share this post


Link to post
Share on other sites
&buttons means "the address of the vector class" which is really not what you need.

&buttons[0] means "the address of the first element of the vector" and since vectors' elements are guaranteed to be stored contiguously (since TR1 I believe but more or less implied in the original standard) you know that this is a safe way of making the vector class look like an array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement