April 28th, 2014

Showing a balloon tip at a specific position, then removing it

Today’s Little Program shows a balloon tip at a specific position, then manually removes it.

Start with our scratch program and make these changes:

#pragma comment(linker, \
    "\"/manifestdependency:type='Win32' "\
    "name='Microsoft.Windows.Common-Controls' "\
    "version='6.0.0.0' "\
    "processorArchitecture='*' "\
    "publicKeyToken='6595b64144ccf1df' "\
    "language='*'\"")
HWND g_hwndTT;
TOOLINFO g_ti;
BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
  g_hwndTT = CreateWindow(TOOLTIPS_CLASS, nullptr,
            WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
            0, 0, 0, 0, hwnd, nullptr, g_hinst, nullptr);
  g_ti.uFlags = TTF_TRACK;
  g_ti.hwnd = hwnd;
  g_ti.lpszText = TEXT("Hi there");
  SendMessage(g_hwndTT, TTM_ADDTOOL, 0, (LPARAM)&g_ti);
  return TRUE;
}
void OnChar(HWND hwnd, TCHAR ch, int cRepeat)
{
  POINT pt;
  switch (ch) {
  case TEXT(' '):
    if (GetCursorPos(&pt)) {
      SendMessage(g_hwndTT, TTM_TRACKPOSITION, 0, MAKELPARAM(pt.x, pt.y));
      SendMessage(g_hwndTT, TTM_TRACKACTIVATE, TRUE, (LPARAM)&g_ti);
    }
    break;
  case 27: // ESCAPE
    SendMessage(g_hwndTT, TTM_TRACKACTIVATE, FALSE, 0);
    break;
  }
}
  HANDLE_MESSAGE(hwnd, WM_CHAR, OnChar);

When our main window is created, we also create a balloon-style tooltip and add a tracking tool. Normally, the tooltip control appears and disappears automatically, at a position of the tooltip’s choosing. Tracking tooltips are managed manually, so you can specify exactly when and where they appear, and you also manually remove them from the screen. At startup, we add the tool but do not show the balloon tooltip yet.

When the user presses the space bar, we get the current cursor position and tell the tracking tooltip to appear at exactly that location, then we activate tracking mode. The result: The balloon tip appears, and the tip of the balloon points directly at the mouse cursor.

When the user presses the ESC key, we deactivate tracking mode, which removes the tooltip from the screen.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.