Thread: Text Only Toolbars

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    Text Only Toolbars - resolved

    Hello, I want to mimic a menu on my window using a toolbar, the main problem I'm having is the size issues. Whenever I create a toolbar it seems to always leave space for a bitmap, even if I don't specify a bitmap. I'm using some modified code I got from www.foosyerdoos.fsnet.co.uk just for testing purposes. Please could somebody look at this code and tell me why there is a big gap between the top of the button and the text?


    Code:
    #pragma comment(lib,"comctl32.lib")
    
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <commctrl.h>
    
    HINSTANCE appInst;         
    HWND      hwndTool;
    
    #define WINWIDTH  300
    #define WINHEIGHT 300
    
    
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
    	switch (Message)
        {
        case WM_CREATE:
            {
    			INITCOMMONCONTROLSEX iccx;
    			TBADDBITMAP          tbAddBmp;
    			TBBUTTON             tbb[5];
    
    			iccx.dwSize=sizeof(INITCOMMONCONTROLSEX);
    			iccx.dwICC=ICC_BAR_CLASSES;
    			InitCommonControlsEx(&iccx);
    
    			hwndTool = CreateWindowEx(0,
    				TOOLBARCLASSNAME,
    				NULL,
    				WS_CHILD|WS_VISIBLE|
    				TBSTYLE_FLAT|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_NORESIZE,
    				0,
    				0,
    				WINWIDTH,
    				40,
    				hwnd,
    				NULL,
    				appInst,
    				NULL);
    
    			SendMessage(hwndTool,TB_BUTTONSTRUCTSIZE,(WPARAM)sizeof(TBBUTTON),0);
    
    			tbAddBmp.hInst=HINST_COMMCTRL;
    			tbAddBmp.nID=IDB_STD_SMALL_COLOR;
    		//	SendMessage(hwndTool,TB_ADDBITMAP,0,(WPARAM)&tbAddBmp);
    
    		//	tbb[0].iBitmap=STD_FILENEW;
    			tbb[0].idCommand=-1;
    			tbb[0].fsState=TBSTATE_ENABLED;
    			tbb[0].fsStyle=TBSTYLE_BUTTON;
    			tbb[0].dwData=0;
    			tbb[0].iString=SendMessage(hwndTool,TB_ADDSTRING,0,(LPARAM)TEXT("New"));
    
    		//	tbb[1].iBitmap=STD_FILEOPEN;
    			tbb[1].idCommand=-1;
    			tbb[1].fsState=TBSTATE_ENABLED;
    			tbb[1].fsStyle=TBSTYLE_BUTTON;
    			tbb[1].dwData=0;
    			tbb[1].iString=SendMessage(hwndTool,TB_ADDSTRING,0,(LPARAM)TEXT("Open"));
    
    		//	tbb[2].iBitmap=STD_FILESAVE;
    			tbb[2].idCommand=-1;
    			tbb[2].fsState=TBSTATE_ENABLED;
    			tbb[2].fsStyle=TBSTYLE_BUTTON;
    			tbb[2].dwData=0;
    			tbb[2].iString=SendMessage(hwndTool,TB_ADDSTRING,0,(LPARAM)TEXT("Save"));
    
    			//tbb[3].iBitmap=STD_PRINTPRE;
    			tbb[3].idCommand=-1;
    			tbb[3].fsState=TBSTATE_ENABLED;
    			tbb[3].fsStyle=TBSTYLE_BUTTON;
    			tbb[3].dwData=0;
    			tbb[3].iString=SendMessage(hwndTool,TB_ADDSTRING,0,(LPARAM)TEXT("PrintPre"));
    			//
    		//	tbb[4].iBitmap=STD_PRINT;
    			tbb[4].idCommand=-1;
    			tbb[4].fsState=TBSTATE_ENABLED;
    			tbb[4].fsStyle=TBSTYLE_BUTTON;
    			tbb[4].dwData=0;
    			tbb[4].iString=SendMessage(hwndTool,TB_ADDSTRING,0,(LPARAM)TEXT("Print"));
    
    			SendMessage(hwndTool,TB_ADDBUTTONS,5,(LPARAM)(LPTBBUTTON)&tbb);
    			return 0;
            }
    		case WM_SIZE:
    			{
    				MoveWindow(hwndTool,0,0,LOWORD(lParam),40,false);
    				return 0;
    			}
        case WM_DESTROY:
            {
    			PostQuitMessage(0);
    			return 0;
            }
        default:
            return DefWindowProc(hwnd,Message,wParam,lParam);
        }
    }
    
    
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    	HWND    hwnd;
    	MSG     Msg;
    	WNDCLASSEX wcx;
    
    	appInst = hInstance;
    	
    	wcx.cbSize           = sizeof(WNDCLASSEX);
    	wcx.style            = CS_HREDRAW|CS_VREDRAW;
    	wcx.lpfnWndProc      = (WNDPROC)WndProc;
    	wcx.cbClsExtra       = 0;
    	wcx.cbWndExtra       = 0;
    	wcx.hInstance        = hInstance;
    	wcx.hIcon            = LoadIcon(0,IDI_APPLICATION);
    	wcx.hCursor          = LoadCursor(0,IDC_ARROW);
    	wcx.hbrBackground    = (HBRUSH)(GetStockObject(WHITE_BRUSH));
    	wcx.lpszMenuName     = NULL;
    	wcx.lpszClassName    = "ToolBarExample";
    	wcx.hIconSm          = NULL;
    
    	if (!RegisterClassEx(&wcx))
        {
    
    		MessageBox( NULL,"Failed to register wnd class","ERROR",MB_OK|MB_ICONERROR);
    		return FALSE;
        }
    
    	hwnd=CreateWindowEx(0,
    		"ToolBarExample",
    		"Toolbar Example",
    		WS_OVERLAPPEDWINDOW,
    		GetSystemMetrics(SM_CXSCREEN)/4,
    		GetSystemMetrics(SM_CYSCREEN)/4,
    		WINWIDTH,
    		WINHEIGHT,
    		NULL,
    		NULL,
    		hInstance,
    		NULL);
    
    	if (!hwnd)
        {
    		MessageBox( NULL,
    			"Failed to create wnd",
    			"ERROR",
    			MB_OK|MB_ICONERROR);
    		return FALSE;
        }
    
    	ShowWindow(hwnd,nCmdShow);
    	UpdateWindow(hwnd);
    
    	while (GetMessage(&Msg,NULL,0,0)>0)
        {
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
        }
    	return Msg.wParam;
    }
    I have read many pages at msdn relating to toolbars and have not yet found a solution to my problem. Any help will be greatly appreciated.

    Thanks.

    ps. sorry for the long code, I posted the whole thing because I wanted it to be as hassle free as possible to understand my problem.
    Last edited by b00l34n; 03-11-2005 at 02:50 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can use the TB_SETBITMAPSIZE message:
    Code:
    SendMessage(hwndTool, TB_SETBITMAPSIZE, 1, 1);
    Also, since you're not setting the iBitmap member, make sure you zero out the TBBUTTON stuctures before use to avoid passing random values for iBitmap:
    Code:
    TBBUTTON             tbb[5] = { 0 };
    I found the TB_SETBITMAPSIZE message by scanning the list of messages available for the toolbar control. I also found that you can achieve the same result by setting the iBitmap member to I_IMAGENONE and giving the toolbar the TBSTYLE_LIST style, but I_IMAGENONE only works on 2000/ME/XP.
    ps. sorry for the long code, I posted the whole thing because I wanted it to be as hassle free as possible to understand my problem.
    Posting compilable code made it easy to copy and poste the code to try out ideas.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Excellent, Thank you very much.

    I did see the I_IMAGENONE flag on msdn, but I was hoping there was a way that worked on 98 too.

    once again, Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM