Question

I am using my own tooltip to display quick help about a currently selected item in my autocomplete listbox used in my editor. Just like VS does it for source code editor, when a new selection happens, the tooltip permanently and immediately pops up next to the current selection, and remains there until new selection of autocomplete listbox is gone. This is a WTL project if that matters.

The way I create amd display my tooltip:

    m_hwndTooltip = CreateWindowEx(WS_EX_TOPMOST,
                            TOOLTIPS_CLASS,
                            NULL,
                            TTS_NOPREFIX | TTS_ALWAYSTIP,       
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            NULL,
                            NULL
                           );

    // INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
    m_toolTipInfo.cbSize = TTTOOLINFOA_V2_SIZE;
    m_toolTipInfo.uFlags = TTF_TRACK;
    m_toolTipInfo.hwnd = NULL;
    m_toolTipInfo.hinst = NULL;
    m_toolTipInfo.uId = 0;  // ??
    m_toolTipInfo.lpszText = (LPTSTR) messsssssssage.c_str();       
    // Tool_tip control will cover the whole window
    m_toolTipInfo.rect.left = 0;
    m_toolTipInfo.rect.top = 0;
    m_toolTipInfo.rect.right = 0;
    m_toolTipInfo.rect.bottom = 0;

    ::SendMessage(m_hwndTooltip, TTM_SETMAXTIPWIDTH, 0, MAX_TOOLTIP_WIDTH); //set max width in pixels, AND(!!) enable multi-line support

    // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
    ::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_toolTipInfo);

    ::SendMessage(m_hwndTooltip, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(x, y));
    ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_toolTipInfo);

This is all fine, works like a charm.

However, when I pass too high Y coord which would make tooltip out of the screen on the bottom side (eg: Screen height: 1000, and I pass Y: 950, and tooltip will be 100 height), then the Tooltip appears on the Y=0, so the screen top position, instead of repositioning to 900. However, this works horizontally, so if it would be out on the right side (so too high X is passed), it moves left the Tooltip until it can fit on the screen.

This is very strange and weird?!

Can anyone give me a tip what is the problem here? Not to mention, the Tooltip size is determined by the win tooltip automatically, based on the message to display + max width + font used + line numbers, so I cannot calculate a correct X,Y position before displaying it, so I need to rely on the Tooltip control.

Was it helpful?

Solution

As no reply, and I found the solution, I will share it for others:

It seems the initially 0,0 is a kind of fallback when tooltip fails to fit on the screen.

So what I do, instead of

 m_toolTipInfo.rect.top = 0;
 m_toolTipInfo.rect.buttom = 0;
   --->
 m_toolTipInfo.rect.top = rcWorkingArea.bottom - 5;
 m_toolTipInfo.rect.bottom = rcWorkingArea.bottom - 5;

If tooltip cannot fit on the screen bottom side, this value will be used.

Still strange why does it work automatically with X coord...but never mind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top