Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode tooltips not showing up

I am trying to display unicode tooltips in my application window, however they do not seem to display. Non-unicode text shows up correctly but as soon as I try doing unicode no tooltip shows up. The following is what I am currently doing, any help is appreciated thank you.

     HWND parentHwnd = pickInfo->getViewer().getCachedHwnd();
  CWnd *pWnd = CWnd::FromHandlePermanent(parentHwnd);
  HINSTANCE hInstance = GetModuleHandle(NULL);

  if (isUnicode)
   m_toolInfoW.lpszText = L"This tooltip does not show up at all.";
  else
   m_toolInfoA.lpszText = "Non unicode text";

  if (!m_bTooltipInitialized){
   ::SendMessage(m_tooltipHwnd, WM_DESTROY, 0,0);

   if(isUnicode)
    m_tooltipHwnd = CreateWindowExW(WS_EX_TOPMOST,
     TOOLTIPS_CLASSW, NULL,
     WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,  
     CW_USEDEFAULT, CW_USEDEFAULT,
     CW_USEDEFAULT, CW_USEDEFAULT,
     parentHwnd, NULL, hInstance, NULL);
   else 
    m_tooltipHwnd = CreateWindowEx(WS_EX_TOPMOST,
     TOOLTIPS_CLASS, NULL,
     WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,  
     CW_USEDEFAULT, CW_USEDEFAULT,
     CW_USEDEFAULT, CW_USEDEFAULT,
     parentHwnd, NULL, hInstance, NULL);

   if (GetLastError() != 0)
    return;

   ::SetWindowPos(m_tooltipHwnd, HWND_TOPMOST,
    0, 0, 0, 0,
    SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

   // Set the max text width before multi-line tooltip is used.
   ::SendMessage(m_tooltipHwnd, TTM_SETMAXTIPWIDTH, 0, m_nMaxWinTooltipWidth);

   if (isUnicode){
    m_toolInfoW.uFlags = TTF_SUBCLASS | TTF_IDISHWND | TTF_TRACK;
    m_toolInfoW.hinst = hInstance;
    m_toolInfoW.hwnd = parentHwnd;
    m_toolInfoW.uId = (UINT_PTR)parentHwnd;
    ::GetClientRect (parentHwnd, &m_toolInfoW.rect);

    ::SendMessage(m_tooltipHwnd, TTM_ADDTOOLW, 0, (LPARAM) (LPTOOLINFOW) &m_toolInfoW);
    ::SendMessage(m_tooltipHwnd, TTM_ACTIVATE, TRUE, (LPARAM)(LPTOOLINFOW) &m_toolInfoW);
   }
   else{
    m_toolInfoA.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
    m_toolInfoA.hinst = hInstance;
    m_toolInfoA.hwnd = parentHwnd;
    m_toolInfoA.uId = (UINT_PTR)parentHwnd;
    ::GetClientRect (parentHwnd, &m_toolInfoA.rect);

    ::SendMessage(m_tooltipHwnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_toolInfoA);
    ::SendMessage(m_tooltipHwnd, TTM_ACTIVATE, TRUE, (LPARAM)(LPTOOLINFO) &m_toolInfoA);
   }
   m_bTooltipInitialized = true;
  }

  if (isUnicode)
   ::SendMessage(m_tooltipHwnd, TTM_UPDATETIPTEXTW, 0, (LPARAM) (LPTOOLINFOW) &m_toolInfoW);
  else
   ::SendMessage(m_tooltipHwnd, TTM_UPDATETIPTEXT, 0, (LPARAM) (LPTOOLINFO) &m_toolInfoA);

  //Repaint the screen so that the area beneath the previous location of the tooltip is restored correctly.
  ::UpdateWindow(pWnd->GetParentOwner()->GetSafeHwnd());
  pWnd = NULL;
like image 332
Martin Avatar asked Dec 22 '22 04:12

Martin


1 Answers

The problem is that you try to use common controls version 6, but you does not get to use it.

More in details,

typedef struct tagTOOLINFOW {
    UINT cbSize;
    UINT uFlags;
    HWND hwnd;
    UINT_PTR uId;
    RECT rect;
    HINSTANCE hinst;
    LPWSTR lpszText;
    LPARAM lParam;
#if (NTDDI_VERSION >= NTDDI_WINXP)
    void *lpReserved;
#endif
} TTTOOLINFOW, NEAR *PTOOLINFOW, *LPTTTOOLINFOW;

for xp+, the header file CommCtrl.h assume you'll use comctl version 6, but if you does not enable it explictly with manifest file, you'll still use the old comctl version 5.x. Then here comes the problem, the size of TOOLINFO of version 5.x is different to version 6.x.

So if you need to use comctl version 5 under windows xp+, you should init TOOLINFO with follwing code,

TOOLINFO ti;
ti.cbSize = sizeof(TOOLINFO) - 4;

Otherwise, you should enable visual-style look with manifest file or prgram directive:

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

Finally, I'd recommand you always enable visual-look in xp+. Here are the comparision of visual effects:

common controls 5.x

common controls 6.x

Note: If you use ANSI/MBCS to compile the program, the sizeof(TOOLINFO) will be 48, which have already remove the lpReserved member. So ANSI version would works, but UNICODE would fail.

like image 102
Jichao Avatar answered Jan 05 '23 14:01

Jichao