Scrollbars redux: Part 12

Date:October 16, 2003 / year-entry #99
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20031016-00/?p=42133
Comments:    0
Summary:Reader Jeff Miller added Word-style scroll tips to our sample scrollbar program. Here's how he did it: HWND g_hwndTT; /* tooltip control */ TOOLINFO g_ti; /* tooltip info struct */ BOOL g_fInScroll; /* Scroll state */ void CancelScrollTip() { if (g_fInScroll) { g_fInScroll = FALSE; SendMessage(g_hwndTT, TTM_TRACKACTIVATE, FALSE, (LPARAM)&g_ti); } } void UpdateScrollTip() { if...

Reader Jeff Miller added Word-style scroll tips to our sample scrollbar program. Here's how he did it:

HWND g_hwndTT;              /* tooltip control */
TOOLINFO g_ti;              /* tooltip info struct */
BOOL g_fInScroll;           /* Scroll state */

void CancelScrollTip()
{
    if (g_fInScroll) {
        g_fInScroll = FALSE;
        SendMessage(g_hwndTT, TTM_TRACKACTIVATE,
                    FALSE, (LPARAM)&g_ti);
    }
}

void UpdateScrollTip()
{
    if (!g_hwndTT) return;
    if (!g_fInScroll) {
        g_fInScroll = TRUE;
        DWORD dwPos = GetMessagePos();
        SendMessage(g_hwndTT, TTM_TRACKPOSITION, 0,
                    MAKELPARAM(GET_X_LPARAM(dwPos) +
                               GetSystemMetrics(SM_CXVSCROLL)
                               GET_Y_LPARAM(dwPos)));
        SendMessage(g_hwndTT, TTM_TRACKACTIVATE, TRUE,
                    (LPARAM)&g_ti);
    }
    // Reset the text to LPSTR_TEXTCALLBACK so it will get
    // re-fetched
    SendMessage(g_hwndTT, TTM_SETTOOLINFO, 0,
                (LPARAM)&g_ti);
}

LRESULT
OnNotify(HWND hwnd, int id, LPNMHDR pnm)
{
    switch (pnm->code) {
    case TTN_GETDISPINFO:
        {
            NMTTDISPINFO *pnmtdi = (NMTTDISPINFO*)pnm;
            wnsprintf(pnmtdi->lpszText, 80,
                      TEXT("Line: %d"), g_yOrigin);
        }
        break;
    }
    return 0;
}

// change case statement in OnVscroll:
    case SB_THUMBTRACK:     ScrollTo(hwnd, GetTrackPos(hwnd, SB_VERT));
                            UpdateScrollTip(); break;
// add case statement to OnVscroll:
    case SB_ENDSCROLL:      CancelScrollTip(); break;

// add to OnCreate

    /* Create and initialize the tooltip */
    g_hwndTT = CreateWindow(TOOLTIPS_CLASS, NULL,
             WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
             CW_USEDEFAULT, CW_USEDEFAULT,
             CW_USEDEFAULT, CW_USEDEFAULT,
             hwnd, NULL, NULL, NULL);
    if (g_hwndTT) {
        g_ti.cbSize = sizeof(g_ti);
        g_ti.uFlags = TTF_TRACK;
        g_ti.hwnd = hwnd;
        g_ti.lpszText = LPSTR_TEXTCALLBACK;
        SendMessage(g_hwndTT, TTM_ADDTOOL, 0, (LPARAM)&g_ti);
    }

// add to WndProc
    case WM_NOTIFY: return OnNotify(hwnd, (int)wParam,
                                    (LPNMHDR)lParam);

A tracking tooltip is used to display the scroll tip because we don't want the tooltip to do automatic positioning or automatic show/hide.

When a tracking scroll occurs (SB_THUMBTRACK), we update the scroll tip, displaying it if necessary. When the tracking scroll ends (SB_ENDSCROLL), we remove the tip.

The above code mimics the Word XP style, where the tooltip appears near the original thumb position but does not follow the thumb as the user scrolls. If you prefer that the tooltip follow the thumb, remove the if (!g_fInScroll) test from the UpdateScrollTip function.



*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index