Balloon Tooltips

In the Windows XP login screen, the password text box will warn you with a balloon tooltip if you accidentally turn Caps Lock on. The balloon tooltip appears to be a Windows tooltip common control with the TTS_BALLOON style.

To replicate this functionality, I decided to write a function called ShowMsgBalloon() which, given a control and the various balloon tooltip parameters, creates and shows the balloon tooltip below the control.

The key insight to making ShowMsgBallon() work as intended was to use the TTF_TRACK option to create a tracking tooltip. This will immediately show the tooltip without requiring the user to position the mouse over the control. The main downside to using TTF_TRACK is that the tooltip will not move with the control if the window is moved; you need to manually move the tooltip using TTM_TRACKPOSITION as required. One could probably make this automatic by subclassing the tooltip’s parent control and handling WM_WINDOWPOSCHANGED messages.

Here is the source code to ShowMsgBalloon(). When you are done with the balloon, call DestroyWindow() on the returned HWND. Note: you may want your application to use comctl32.dll version 6 as it will lead to a nicer visual style, including a close button.

#include <windows.h>
#include <commctrl.h>
// Options to ShowMsgBallon() (see dwOpts parameter).  These are the
// standard icon types for balloon tooltips.
#define SMB_ICON_INFO    (1 << 0)
#define SMB_ICON_WARNING (1 << 1)
#define SMB_ICON_ERROR   (1 << 2)
// Given the options passed to ShowMsgBalloon(), determine what
// parameter to send to TTM_SETTITLE for the balloon tooltip’s icon.
static DWORD
GetTitleIcon(DWORD dwOpts)
    if (dwOpts &amp; SMB_ICON_INFO)
        return TTI_INFO;
    else if (dwOpts &amp; SMB_ICON_WARNING)
        return TTI_WARNING;
    else if (dwOpts &amp; SMB_ICON_ERROR)
        return TTI_ERROR;
        return 0;
// Create and show a balloon tooltip immediately below the control
// hwndCtrl with the given title, message, and options.
ShowMsgBalloon(HWND hwndCtrl, LPCTSTR szTitle, LPCTSTR szMsg,
               DWORD dwOpts)
    HWND hwndRet = NULL;
    HWND hwndTT = NULL;
    TOOLINFO ti = { 0 };
    RECT rc;
    // Even though TTS_CLOSE is always specified, a close button will
    // only be shown if your application has a manifest that requires
    // comctl32.dll version 6.
    hwndTT = CreateWindow
    if (hwndTT == NULL)
        goto Cleanup;
    // By using TTTOOLINFO_V1_SIZE rather than sizeof(TOOLINFO),
    // we don’t require users to be using comctl32 version 6.
    ti.cbSize = TTTOOLINFO_V1_SIZE;
    ti.uFlags = TTF_TRACK;
    ti.hwnd = hwndCtrl;
    ti.lpszText = const_cast<lptstr>(szMsg);
    if (!SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) &amp;ti))
        goto Cleanup;
    if (!SendMessage(hwndTT, TTM_SETTITLE, GetTitleIcon(dwOpts),
                     (LPARAM) szTitle))
        goto Cleanup;
    // Position the tooltip below the control
    if (!GetWindowRect(hwndCtrl, &amp;rc))
        goto Cleanup;
    SendMessage(hwndTT, TTM_TRACKPOSITION, 0,
                MAKELONG(rc.left + 10, rc.bottom));
    // Show the tooltip
    if (!SendMessage(hwndTT, TTM_TRACKACTIVATE, TRUE, (LPARAM) &amp;ti))
        goto Cleanup;
    hwndRet = hwndTT;
    hwndTT = NULL;
    if (hwndTT != NULL)
    return hwndRet;

Update 2008-11-01 3:08PM: If you are targeting comctl32.dll version 6 or later, I recommend using the EM_SHOWBALLOONTIP message. Comctl32.dll version 6 or later also automatically shows the caps lock warning balloon for edit boxes with the ES_PASSWORD window style.


Escaping Strings in XPath 1.0

XPath is a language for selecting nodes from an XML document. XPath is used extensively in XSLT and other XML technologies. I also vastly prefer using XPath (e.g. with XPathNavigator) over the XML DOM when manipulating XML in a non-streaming fashion.

In XPath, strings must be delimited by either single or double quotes. Given a quote character used to delimit a string, one can’t represent that same quote character within the string. This means that if you decide to use single quotes to delimit your XPath string, you couldn’t represent the string O’Reilly; use double quotes, and you can’t represent “Hello”.

However, given a quote delimiter, you can represent the other quote character. We can use this observation along with the concat XPath function to devise a general quoting rule for XPath strings. It’s easiest to show this via a series of examples:

Original String Quoted XPath String
a 'a' (or "a")
O'Reilly "O'Reilly"
"Hello" '"Hello"'
"Hello, Mr. O'Reilly" concat('"Hello, Mr. O', "'Reilly", '"')

Below is a piece of C++ code which implements these quotation rules:

QuoteXPathString(const std::string& xpath)
    // If we don’t have any single or double-quote characters, quote the
    // expression in single quotes.
    std::string::size_type pos = xpath.find_first_of("’\"");
    if (pos == std::string::npos)
        return "’" + xpath + "’";
    // If we cannot find the alternate quotation character, quote the
    // expression in the alternate quotation character.
    char chOther = (xpath[pos] == ‘"’ ? ‘\’‘ : ‘"’);
    pos = xpath.find(chOther, pos + 1);
    if (pos == std::string::npos)
        return chOther + xpath + chOther;
    // The string has both quotation characters.  We need to use concat()
    // to form the string.
    std::stringstream ss;
    ss << "concat("
       << chOther
       << xpath.substr(0, pos)
       << chOther;
    do {
        chOther = (xpath[pos] == ‘"’ ? ‘\’‘ : ‘"’);
        std::string::size_type pos2 = xpath.find(chOther, pos + 1);
        ss << ‘,’
           << chOther
           << xpath.substr(pos, pos2 – pos)
           << chOther;
        pos = pos2;
    } while (pos != std::string::npos);
    ss << ")";
    return ss.str();

Usage looks like:

std::string lastName = …; // May come from user input
std::string xpath = "//Customer[LastName = " +
    QuoteXPathString(lastName) + "]";