# Dynamic TBBUTTON array

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

## 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 on other sites
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 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 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_ADDBUTTONSSendMessage(hwndToolbar, TB_ADDBUTTONS, (WPARAM)buttons.size(), (LPARAM)(&buttons[0]));// While instead I was doing thisSendMessage(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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015446
×