Thread: Tooltip won't display over toolbar button

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798

    Tooltip won't display over toolbar button

    I can't for the life of me work out why this tooltip won't work. All I want is for the tip to display over a toolbar button. The toolbar button definition is here:

    Code:
    tbb[0].iBitmap = STD_FILENEW;
    		tbb[0].fsState = TBSTATE_ENABLED;
    		tbb[0].fsStyle = TBSTYLE_BUTTON;
    		tbb[0].idCommand = ID_NEW;
    and the code I am using to create the tooltip is here:

    Code:
    hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL, TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, 
    				CW_USEDEFAULT, CW_USEDEFAULT, hwnd, NULL, hInstance, NULL);
    
    		SetWindowPos(hwndTip, HWND_TOPMOST,0, 0, 0, 0,
                 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
    
    		SendMessage(hwndTip, TTM_ACTIVATE, TRUE, 0);
    		TipInfo.cbSize = sizeof (TipInfo);
    		TipInfo.uFlags = TTF_SUBCLASS;
    		
    		TipInfo.hinst = hInstance;
    		TipInfo.hwnd = hwnd; /*Have also tried htool (handle to the toolbar) and hwndRB (handle to the rebar in which the toolbar is situated)*/
    	
    		
    		TipInfo.uId = ID_NEW;
    		TipInfo.lpszText = "New";
    		
    		SendMessage(hwndTip, (UINT)TTM_ADDTOOL, (WPARAM)0, (LPARAM) &TipInfo);
    EDIT: Sorry for the shoddy formatting. The code is cut and pasted from MSVC++ and i don't know why it came out like this.

    Everything compiles and runs just fine but no tooltip is displayed over the button. Is there anything that i am doing wrong or anything that I have missed out? Any help is appreciated, thanks.

  2. #2
    id id
    Guest
    My guess would be the lack of WS_POPUP when you create the tooltip control. Please also verify that InitCommonControlsEx has been called. Anyway here is some code that I use:

    Code:
    //set up tooltips
    hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL,
    	WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
    	CW_USEDEFAULT, CW_USEDEFAULT,
    	CW_USEDEFAULT, CW_USEDEFAULT,
    	hDlg, NULL, hMainInstance,
    	NULL);
    
    SetWindowPos(hwndTip, HWND_TOPMOST,0, 0, 0, 0,
    	SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
    
    SendMessage(hwndTip,TTM_SETMAXTIPWIDTH,0,200);
    
    ZeroMemory(&ti, sizeof(ti) );
    
    ti.cbSize = sizeof(ti);
    ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    ti.hwnd = hDlg;
    ti.hinst = hMainInstance;
    
    ti.uId = (UINT) GetDlgItem(hDlg, CNTRL_FOLDER);
    ti.lpszText = MAKEINTRESOURCE(TT_FOLDER);
    SendMessage(hwndTip,TTM_ADDTOOL, 0, (LPARAM) &ti);

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Thanks.

    InitCommonControlsEx() is called further up. I had a look at your code and I have managed to get a tooltip displayed when I hover the cursor over the toolbar. This is the code:

    Code:
    hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, 
    				CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
    		SetWindowPos(hwndTip, HWND_TOPMOST,0, 0, 0, 0,
                 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
    
    		SendMessage(hwndTip, TTM_ACTIVATE, TRUE, 0);
    		ZeroMemory(&TipInfo, sizeof(TipInfo) );
    
    		TipInfo.cbSize = sizeof (TipInfo);
    		TipInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    		
    		TipInfo.hinst = hInstance;
    		TipInfo.hwnd = htool;
    	
    		
    		TipInfo.uId = (UINT) htool;
    		TipInfo.lpszText = "New";
    		
    		SendMessage(hwndTip, TTM_ADDTOOL, (WPARAM)0, (LPARAM) &TipInfo);
    However what I actually want is to have a different tooltip for each toolbar button. But when I remove the TTF_IDISHWND and change

    Code:
    TipInfo.uId = (UINT) htool;
    to

    Code:
    TipInfo.uId = (UINT) ID_NEW;
    it no longer displays a tooltip. Does anyone know how to display a tooltip just for the current toolbar button and not for the whole toolbar? Thanks.

  4. #4
    id id
    Guest
    Sorry, that is for individual controls. Tooltips are set up for a toolbar in a different way.

    The toolbar control must be created with the TBSTYLE_TOOLTIPS style.
    Code:
    hwndTB = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
    	WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,0, 0, 0, 0,
    	hDlg,NULL, hInst, NULL);

    Call this or the text will be displayed on the toolbar
    Code:
    //Prevents text from displaying
    SendMessage(hwndTB, TB_SETMAXTEXTROWS,0,0);
    You must fill in the iString member of the TBBUTTON struct.
    MSDN:
    iString
    Zero-based index of the button string, or a pointer to a string buffer that contains text for the button.
    Now add strings to your toolbar control and associate them with your buttons.
    Code:
    tbb[0].iString = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) "test");
    tbb[1].iString = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM)  "another test");
    And Bob's your uncle.

    Here is a full toolbar creation function I did a few years ago. It could be vastly improved but it might give you some ideas.
    Code:
    HWND CreateAToolBar(void)
    {
    
    HWND hwndTB;
    INT index, stdidx;
    
    //Buttons
    TBBUTTON tbb [ ] =
    {
    {STD_FILENEW, M_FILE_NEW, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    {STD_FILEOPEN, M_FILE_OPEN, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    {STD_FILESAVE, M_FILE_SAVE, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    
    {5, 0, TBSTATE_ENABLED, BTNS_SEP, 0,0L, 0},
    
    {TBIMG_REFRESH, M_FILE_REFRESH, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    
    {5, 0, TBSTATE_ENABLED, BTNS_SEP, 0,0L, 0},
    
    {STD_CUT, M_EDIT_CUT, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    {STD_COPY,M_EDIT_COPY, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    {STD_PASTE, M_EDIT_PASTE, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    {STD_UNDO, M_EDIT_UNDO, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    
    {5, 0, TBSTATE_ENABLED, BTNS_SEP, 0,0L, 0},
    
    {TBIMG_FIND, M_SRCH_FND, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    
    {5, 0, TBSTATE_ENABLED, BTNS_SEP, 0,0L, 0},
    
    {TBIMG_CALC,M_CALC, TBSTATE_ENABLED, BTNS_BUTTON, 0,0L, 0},
    
    };
    
    //Button Strings
    LPSTR toolStrings[ ] = {
    "New", "Open", "Save","", "Refresh", "", "Cut", "Copy", "Paste","Undo","","Find","","Calculator",
    };
    
    
    //Create a toolbar that has ToolTips associated with it.
    hwndTB = CreateWindowEx(0, TOOLBARCLASSNAME, (LPSTR) NULL,
    	WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,0, 0, 0, 0,
    	hDlg,(HMENU) M_MAIN, hInst, NULL);
    
    
    // Send the TB_BUTTONSTRUCTSIZE message, which is required for
    // backward compatibility.
    SendMessage(hwndTB, TB_BUTTONSTRUCTSIZE,(WPARAM) sizeof(TBBUTTON), 0);
    
    //Prevents text from displaying
    SendMessage(hwndTB, TB_SETMAXTEXTROWS,0,0);
    
    // Associate strings with buttons
    for (index = 0;index < 14;index++) {
    	if (*toolStrings[index] != *"")
    		tbb[index].iString = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) toolStrings[index]);
    	else tbb[index].iString = 0;
    }
    
    //Add standard images
    TBADDBITMAP tb;
    tb.hInst = HINST_COMMCTRL;
    tb.nID = IDB_STD_SMALL_COLOR;
    SendMessage (hwndTB, TB_ADDBITMAP, 0, (LPARAM)&tb);
    
    //Add custom images
    tb.hInst = hInst;
    tb.nID = BITMAP_ICONS;
    stdidx = SendMessage (hwndTB, TB_ADDBITMAP, 2, (LPARAM)&tb);
    
    //update bitmap indexes for custom images
    tbb[4].iBitmap += stdidx; //refresh
    tbb[13].iBitmap +=stdidx; //calculator
    tbb[11].iBitmap +=stdidx; //Find - Windows does provide a find image
    	//but it is a magnifying glass and not the standard binoculars.
    
    //Add buttons
    //Don't add calculator button or last separator if we
    //couldn't find the calculator.
    SendMessage(hwndTB, TB_ADDBUTTONS, (WPARAM) (calcFound ? 14 : 12), (LPARAM) (LPTBBUTTON) &tbb);
    
    //Size and show window
    SendMessage(hwndTB, TB_AUTOSIZE, 0, 0);
    ShowWindow(hwndTB, SW_SHOWNORMAL);
    
    //Get bottom of toolbar so we can size  edit box
    RECT toolRect;
    SendMessage(hwndTB,TB_GETRECT,0,(WPARAM) &toolRect);
    toolBottom = toolRect.bottom;
    
    
    return hwndTB;
    }

  5. #5
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Thank you SO much. It worked perfectly. I have been looking for a straight answer for ages on the web without success. Thanks again.

  6. #6
    id id
    Guest

    Thumbs up

    Assumably, you could also do it this way but I haven't tried.

    static LPSTR toolStrings[ ] = {
    "New", "Open",};
    tbb[0].iString = toolStrings[0];
    tbb[1].iString = toolStrings[1];

    Any chance you could help me with my problem?
    http://cboard.cprogramming.com/showt...threadid=40753

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. toolbar button question
    By supernater in forum Windows Programming
    Replies: 1
    Last Post: 05-25-2009, 08:24 PM
  3. Hide toobar button by tooltip text
    By Br5an in forum Windows Programming
    Replies: 0
    Last Post: 07-11-2005, 01:26 PM
  4. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM