Valhalla Legends Forums Archive | C/C++ Programming | Creating tooltips for buttons

AuthorMessageTime
Okee
Hi,

I've been trying to create tooltips for a standard button I have on my win32 form. I created the button using CreateWindowEx. On msdn.microsoft.com I have found a few interesting items regarding tooltips. I've been trying to use the tooltip controls provided.

[url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/tooltip/reflist.asp[/url]

I'm not getting any errors as far as visual c++ 6 compiler is concerned, but the tooltip isn't appearing. I've created a function that, as well as I can tell, should be showing a tooltip when called. Here is my function:

[code]
void CreateMyTooltip (HWND hwnd)
{
               
    INITCOMMONCONTROLSEX iccex;
    HWND hwndTT;               

    TOOLINFO ti;
    unsigned int uid = 0;     
    char strTT[30] = "Halleluja!";
    LPTSTR lptstr = strTT;
    RECT rect;                 

    iccex.dwICC = ICC_WIN95_CLASSES;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCommonControlsEx(&iccex);

    hwndTT = CreateWindowEx(WS_EX_TOPMOST,
        TOOLTIPS_CLASS,
        NULL,
        WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        hwnd,
        NULL,
        hinstMain,
        NULL
        );

    SetWindowPos(hwndTT,
        HWND_TOPMOST,
        0,
        0,
        0,
        0,
        SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

    GetClientRect (hwnd, &rect);

    ti.cbSize = sizeof(TOOLINFO);
    ti.uFlags = TTF_SUBCLASS;
    ti.hwnd = hwnd;
    ti.hinst = hinstMain;
    ti.uId = uid;
    ti.lpszText = lptstr;

    ti.rect.left = rect.left;   
    ti.rect.top = rect.top;
    ti.rect.right = rect.right;
    ti.rect.bottom = rect.bottom;

    SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
SendMessage(hwndTT, TTM_ACTIVATE, TRUE, 0);

}
[/code]

Now, I call this function from within my window process function:
[code]LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)[/code]

Like so..
[code]
case WM_COMMAND:
{
CreateMyTooltip(hWnd);
}break;
[/code]

All I found to include as far as header files was the common controls header. Like I said, no compiler errors and no program crashes. My knowledge of C++ ettiquite (sp?) is poor, but I'm trying.

On msdn, I also found some other information on tooltips at [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/tooltip/usingtooltips.asp[/url] but it looked to be relitively the same info.

Anyone have this 'my tooltip wont show up' problem before? Or anyone know of any way I can get a tooltip to popup when I put my mouse over my button? I know I have it for when I click it right now, I was going to work towards getting it when the mouse moves over the button.

Thanks in advance.

EDIT:

Sorry, I forgot to add what I was getting at with my C++ ettiquite comment. In that window process function I create my button in the WM_CREATE event. My HWND declare and initialization all reside within that case wm_create statement - I call my tooltip function from within another statement (wm_command). Now, I can pretty much tell thats one of my problems because the hwnd im passing to the function cant possibly be the hwnd of my button but I just cant put my finger on how to do this. Its just not clicking.
November 17, 2004, 2:36 AM
Myndfyr
You were close on "etiquette," just backwards on which part had one "t" and which had two.

An observation: you're receiving and processing the WM_COMMAND message.  That is only when you click a button, is it not?

I wonder if a tooltip inherently disappears when you click.  Or, if you're just not clicking your button and therefore not getting your tool tip?

(Unfortunately I'm not at home to test my theory).

Anyway, hope this helps. :)
November 17, 2004, 2:56 AM
Kp
When things don't work, get in the habit of adding error checking code.  Make sure that the CreateWindowEx call for the tooltip occurs and produces a valid HWND.  If it fails, check GetLastError().  Same with other functions.  Not all the functions will produce an error code, but if one is being set, it may give you some hints about why it isn't working.
November 17, 2004, 3:19 AM
Okee
Yeah, I checked. It does create a valid hWnd.

Even if clicking removes the tooltip, which i didnt think about, wouldnt it still popup if i hit space bar to envoke wm_command? how are tooltips set for lets say, listviews and such in those battle.net bots.
November 17, 2004, 3:30 AM
Zakath
The method I use in my listview is, unfortunately, completely different. Listviews support a message called LVN_GETINFOTIP, which is passed to the parent in a WM_NOTIFY. There is, as far as I know, no analagous message for other controls.
November 17, 2004, 4:42 AM

Search