CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Tooltip

  1. #1
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Tooltip

    Is there anything wrong in this piece of code?

    Code:
    void CreateToolTip(HWND hwndParent, RECT rectTip, LPWSTR lpTipString){
        HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST,
    		TOOLTIPS_CLASS, NULL,
            WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,		
            CW_USEDEFAULT, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT,
            hwndParent, NULL, hInst,NULL);
    
        SetWindowPos(hwndTT, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
    
        TOOLINFO ti = { 0 };
        ti.cbSize = sizeof(TOOLINFO);
        ti.uFlags = TTF_SUBCLASS|TTF_IDISHWND;
        ti.hwnd = hwndParent;
        ti.hinst = hInst;
        ti.lpszText = lpTipString;
        ti.rect = rectTip;
    
        SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);	
    }
    I call this function in my WM_CREATE / WM_INITDIALOG, but I never see a tooltip if
    the mouse hovers the rect. I do call InitCommonControlsEx() before.

    ?

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Tooltip

    At first glance:

    1. Get rid of SetWindowPos().
    2. Replace the CreateWindowEx with a normal CreateWindow - no TopMost.
    3. Set the ID in the TOOLINFO struct.
    4. Don't forget to activate the tooltips.
    Code:
    
    SendMessage(hwndTT, TTM_ACTIVATE, TRUE, 0);
    
    5. If you are using any sort of wrapper (MFC, .NET, ATL), don't forget to enable tooltips on the parent window.


    -Erik

  3. #3
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Tooltip

    Use CreateWindowEx. Using CreateWindow may render tooltips inoperable in older versions of windows.
    You can use CreateWindow, but to be on the safe side add WS_EX_TOOLWINDOW extended style. You do not have to use WS_EX_TOPMOST but using it should not do any harm.

    You can indeed get rid of SetWindowPos call but it is irrelevant.

    Fill TOOLINFO properly. This is a cause of your problem. If you are adding rectangle as a tool, you cannot use TTF_IDISHWND flag. If you add more rectangles, than you would have to assign ID that is unique for each rectangle. I your case it is 0.

    In conclusion, removing TTF_IDISHWND flag is a fix you need.
    Code:
        TOOLINFO ti = {0};
        ti.cbSize = sizeof(TOOLINFO);
        ti.uFlags = TTF_SUBCLASS;//|TTF_IDISHWND;
        ti.hwnd = hwndParent;
        ti.hinst = hInst;
        ti.lpszText = lpTipString;
        ti.rect = rectTip;
    
        SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)&ti);
    You do not have to activate tooltips, since they are created as active. Use TTM_ACTIVATE to disable/enable tooltips if needed later in the code.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Re: Tooltip

    Thanks for the answers, I adapted my the code but still, no tooltip is showing up.
    It looks like this now:

    Code:
    case WM_INITDIALOG:{
    		...
    		InitCommonControls();
                    CreateToolTip(hWnd, rectStatus, L"Test", TT_TEST);
                    ...
    I tried InitCommonControlsEx() also, but I don't think that is the problem.
    TT_TEST is defined as 20001.
    rectStatus is defined global as RECT rectStatus = {70, 310, 480, 350}; which is a rectangle
    within my dialog client window.


    Code:
    void CreateToolTip(HWND hwndParent, RECT rectTip, LPWSTR lpTipString, UINT uId){
        HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST|WS_EX_TOOLWINDOW,
    		TOOLTIPS_CLASS, NULL,
            WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,		
            CW_USEDEFAULT, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT,
            hwndParent, NULL, hInst,NULL);
    
        TOOLINFO ti		= { 0 };
        ti.cbSize		= sizeof(TOOLINFO);
        ti.uFlags		= TTF_SUBCLASS;
        ti.hwnd		= hwndParent;
        ti.hinst		= hInst;
        ti.lpszText		= lpTipString;
        ti.rect		= rectTip;
    	ti.uId		= uId;
    
        SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);	
    }
    I don't get a tooltip when I hover the mouse over rectTip.

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Tooltip

    Something must be wrong with your rectangle coordinates.
    Just for a test initialize it to 0, 0, 200, 100. This will allow you to test left upper corner area.

    If you still have problems, post your code. I always test my solution before posting and it works for me.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Lightbulb Re: Tooltip

    I'm pretty much helpless on that too.
    The rectangle is ok, I'm drawing it with FillRect().

    I'm using exactly the code I posted, I Just changed the InitCommonControls() with InitCommonControlsEx() with almost every flag.

    Well, It also does not work when I setup a tooltip for any control in my dialog. Just nothing happens. Other applications on my machine can create tooltips so it must be something on my code or environment.

    I'm compiling a win32 api application, written in C. Using MSVS 2008.

    Help

  7. #7
    Join Date
    Jan 2008
    Posts
    178

    Re: Tooltip

    It has nothing to do with Rectangle coordinates.
    See Google Groups where the reason why it dosn't work has been explained hundreds of times...

  8. #8
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Re: Tooltip

    Great comment.
    Are you kidding me? Just paste a link to whatever you mean if you know for sure what
    the problem is about.

  9. #9
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Tooltip

    Quote Originally Posted by me earlier View Post
    If you still have problems, post your code.
    As I said it works for me. Tested on Vista and XP.
    From the code snippet you have posted, I do not see any reason for failure. I am not able to determine what is wrong without seeing code for entire application.
    Maybe something you do in a main loop prevents working?
    Last edited by JohnCz; November 30th, 2008 at 11:30 AM.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured