Jump to content
  • Advertisement
Sign in to follow this  
Subotron

Visual C++: win32 toolbar doesn't appear right away

This topic is 4270 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

I am trying to implement a toolbar in my Visual C++ project, which kind of worked, except it doesn't appear until I click on the top-left portion of the dialog (the menu or the icon in the top-left corner that brings up a menu). Only when I do this the menu appears. Below is my code, which is executed in my init window function, just after I loaded the dialog:
	const int num_buttons = 3;
	const int num_bitmaps = 1;

	TBBUTTON tb_buttons[num_buttons];
	
	tb_buttons[0].iBitmap   = I_IMAGENONE;
	tb_buttons[0].idCommand = 0;
	tb_buttons[0].fsState   = TBSTATE_ENABLED;
	tb_buttons[0].fsStyle   = BTNS_BUTTON;
	tb_buttons[0].dwData    = 0L;
	tb_buttons[0].iString   = 0;

	tb_buttons[1].iBitmap   = I_IMAGENONE;
	tb_buttons[1].idCommand = 0;
	tb_buttons[1].fsState   = TBSTATE_ENABLED;
	tb_buttons[1].fsStyle   = BTNS_SEP;
	tb_buttons[1].dwData    = 0L;
	tb_buttons[1].iString   = 0;

	tb_buttons[2].iBitmap   = I_IMAGENONE;
	tb_buttons[2].idCommand = 0;
	tb_buttons[2].fsState   = TBSTATE_ENABLED;
	tb_buttons[2].fsStyle   = BTNS_BUTTON;
	tb_buttons[2].dwData    = 0L;
	tb_buttons[2].iString   = 0;

	tb = CreateToolbarEx(	get_dialog(TEXT("main"))->second,
							WS_VISIBLE,
							TB_MAIN,
							num_bitmaps,
							GetModuleHandle(NULL),
							TB_MAIN,
							tb_buttons,
							num_buttons,
							20,
							20,
							16,
							15,
							sizeof(TBBUTTON));

	SendMessage(tb, TB_AUTOSIZE, 0, 0);

	ShowWindow(tb, SW_SHOW);

I tried some variations of this code that I found on the internet, but this doesn't resolve the problem. Who can tell what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Edit: when I clean the resource.h file it works for a second, but when I then change the code the problem is back. Somehow it appears to work fine when the IDs I fill in in the code are wrong (since they are remapped) but after recompiling the problem is back again.

[Edited by - Subotron on April 6, 2007 6:17:26 AM]

Share this post


Link to post
Share on other sites
Hi there, I don't know what the problem is, maybe its a paint / update problem or something? I got a toolbar to work with CreateWindow though. MSDN says CreateToolbarEx is obsolete and CreateWindow should be used, but I would have still thought it should work.





//Excuse any untidyness.-

//common controls have already been included etc.

/////////////////////////////////////////////////////////////////////

///////////
//Toolbar window.

///experiment with different window styles.
DWORD toolbar1 = (WS_CHILD | WS_GROUP | WS_BORDER |
BTNS_AUTOSIZE );
DWORD toolbar2 = (WS_CHILD | WS_GROUP | WS_BORDER | BTNS_AUTOSIZE |
BTNS_BUTTON | TBSTYLE_FLAT); //TBSTYLE_FLAT == transparent.


//MSDN examples use CreateWindowEx
hWndtoolbar = CreateWindow(TOOLBARCLASSNAME, szTitle, toolbar1,
0, 0, 0, 0, globalhWnd, NULL, hInstance, NULL);
//The window size is automattically set by sending a TB_AUTOSIZE
//message, which is done at the bottom.


LPSTR toollabel_1 = "button 1";
DWORD_PTR dpstr_1 = (DWORD_PTR)toollabel_1;
LPSTR toollabel_2 = "button 2";
DWORD_PTR dpstr_2 = (DWORD_PTR)toollabel_2;
LPSTR toollabel_3 = "button 3";
DWORD_PTR dpstr_3 = (DWORD_PTR)toollabel_3;
LPSTR toollabel_4 = "button 4";
DWORD_PTR dpstr_4 = (DWORD_PTR)toollabel_4;

//I_IMAGENONE == no bitmap, this doesn't seem to work at the moment because it should
//mean no bitmap is being used, and only text is used, - but it looks like space is
//being left for a bit map, also the text isn't centred?

toolitems[0].dwData = 0L;
toolitems[0].fsState = TBSTATE_ENABLED;
toolitems[0].fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT;
toolitems[0].iBitmap = I_IMAGENONE;
toolitems[0].idCommand = WM_APP_TOOLBAR_1;
toolitems[0].iString = dpstr_1;

toolitems[1].dwData = 0L;
toolitems[1].fsState = TBSTATE_ENABLED;
toolitems[1].fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT;
toolitems[1].iBitmap = I_IMAGENONE;
toolitems[1].idCommand = WM_APP_TOOLBAR_2;
toolitems[1].iString = dpstr_2;

toolitems[2].dwData = 0L;
toolitems[2].fsState = TBSTATE_ENABLED;
toolitems[2].fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT;
toolitems[2].iBitmap = I_IMAGENONE;
toolitems[2].idCommand = WM_APP_TOOLBAR_3;
toolitems[2].iString = dpstr_3;

toolitems[3].dwData = 0L;
toolitems[3].fsState = TBSTATE_ENABLED;
toolitems[3].fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT;
toolitems[3].iBitmap = I_IMAGENONE;
toolitems[3].idCommand = WM_APP_TOOLBAR_4;
toolitems[3].iString = dpstr_4;


LRESULT lResult = 0;
UINT uNumButtons = 4;

lResult = SendMessage(// returns LRESULT in lResult
(HWND) hWndtoolbar, // handle to destination control
(UINT) TB_BUTTONSTRUCTSIZE, // message ID
(WPARAM) (UINT) sizeof(TBBUTTON),
(LPARAM) 0);

lResult = SendMessage(// returns LRESULT in lResult
(HWND) hWndtoolbar,// handle to destination control
(UINT) TB_ADDBUTTONS,// message ID
(WPARAM) (UINT) uNumButtons,
(LPARAM) (LPTBBUTTON) toolitems);


SendMessage(hWndtoolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndtoolbar, SW_SHOW);
UpdateWindow(hWndtoolbar);









[Edited by - Stevieboy on April 7, 2007 12:16:06 AM]

Share this post


Link to post
Share on other sites
Thanks, I found out the problem was that the message didn't get through to my message handler. The problem is solved now, and I also use CreateWindowEx now since CreateToolbar is obsolete.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!