Hello,

Great forum by the way. Highly focused and packed full of useful posts.

I have scoured all previous posts on toolbars, studied the tutorials, studied all the source code I can lay my hands on, studied all the relevant Microsoft documentation but I am still unable to add text to a toolbar created with CreateWindowEx. Please help me.

The toolbar and the 32x32 button bitmaps display ok.

Here is my code (some what condensed):


ToolbarButtons TBBUTTON <0, MI_BUTTON00, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0,NULL,NULL>
TBBUTTON <1, MI_BUTTON01, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0,NULL,NULL>
TBBUTTON <2, MI_BUTTON02, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0,NULL,NULL>
TbString db 20 dup(?),0

invoke CreateWindowEx, NULL, ADDR ToolbarClass,NULL,\
WS_CHILD + WS_VISIBLE + WS_BORDER + CCS_NORESIZE,\
0,0,600,46, hWnd, CID_TOOLBAR, hInstance, NULL

; ----- Display button text

invoke LoadString,hInstance,IDS_BUTTON00, ADDR TbString,20 ; Load string into buffer
invoke SendMessage, hToolbar, TB_ADDSTRING, hInstance, ADDR TbString ; Add buffer to string list
mov ToolbarButtons[0].iString , eax
invoke LoadString,hInstance,IDS_BUTTON01, ADDR TbString,20 ; Load string into buffer
invoke SendMessage, hToolbar, TB_ADDSTRING, hInstance, ADDR TbString ; Add buffer to string list
mov ToolbarButtons[1].iString , eax

; ----- Add buttons to toolbar with an indent of 3 pixels

invoke SendMessage, hToolbar, TB_SETBUTTONSIZE, 0, bSize
invoke SendMessage, hToolbar, TB_SETINDENT,3,0
invoke SendMessage, hToolbar, TB_ADDBUTTONS,3, ADDR ToolbarButtons

Many thanks

GreenScreenAlternator
Posted on 2002-07-28 04:57:31 by GreenScreenAlternator
Hi GreenScreenAlternator,

Welcome to the forum; it's great to have you here.

I too had this problem.

First, I don't think structure arrays in assembly work as one might expect, so I am avoiding them until I know more.

So, initialise the TBBUTTON structure like this:

ToolbarButtons TBBUTTON <>

Then since you are using a buffer to hold the button text, the hInst parameter of the TB_ADDSTRING message should be set to zero, like this:

invoke SendMessage, hToolbar, TB_ADDSTRING, 0, ADDR TbString

And before invoking the TB_ADDBUTTONS message, fill in the TBBUTTON structure like this:

invoke SendMessage, hToolbar, TB_ADDSTRING, 0, ADDR TbString
mov ToolbarButtons.iString, eax
mov ToolbarButtons.iBitmap, 0
mov ToolbarButtons.idCommand, MI_BUTTON00
mov ToolbarButtons.fsState, TBSTATE_ENABLED
mov ToolbarButtons.fsStyle, TBSTYLE_BUTTON

This method seems to work ok in my program. Good luck!
Posted on 2002-08-01 13:59:57 by GreenScreenAlternator
Glad that you could help yourself!
BTW, do you smoke the same stuff as cmax?
He sometimes does similar things :grin:
Posted on 2002-08-01 14:13:03 by bazik
I think it's called a cr*ck!!! :grin:
Posted on 2002-08-01 14:13:44 by stryker
GreenScreenAlternator, it is good to see you here.
Posted on 2002-08-01 14:20:38 by bitRAKE
GreenScreenAlternator,
to answer your original question:

-- your structure definitions look fine, there shouldn't be any problem here
-- you are right about the hInst in the call to TB_ADDSTRING


Where you are going wrong is in lines like this:


mov ToolbarButtons[1].iString , eax


Assembly does not treat arrays of structures the way C++ or other high level languages would. In an HLL, ToolbarButtons[1] would point to element 1 of an array. It ASM, it points to the start of ToolbarButtons plus one byte. Hence, on the second call to TB_ADDSTRING you are partially overwriting the first result at ToolbarButtons[0].iString as they overlap by 3 bytes.

If you want to access an element of an array you can do this:


TBB_SIZE EQU sizeof TBBUTTON
mov ToolbarButtons[1*sizeof TBBUTTON].iString,eax
or
mov ToolbarButtons[1*TBB_SIZE].iString,eax


I hope this answers your question, and welcome to the forum.

--Chorus
Posted on 2002-08-04 07:25:56 by chorus
Thanks Chorus,

How simple!

It worked.
Posted on 2002-08-04 11:32:40 by GreenScreenAlternator