Getting the location of the Close button in the title bar

Raymond Chen

Today’s Little Program locates the × button in the corner of the window and, just to show that it found it, displays a balloon tip pointing at it.

Let’s start with the program from last week, the one that displays a balloon tip, then make these changes:

BOOL GetCloseButtonCenter(HWND hwnd, POINT *ppt)
{
 TITLEBARINFOEX info = { sizeof(info) };
 if (!SendMessage(hwnd, WM_GETTITLEBARINFOEX, 0, (LPARAM)&info))
    return FALSE;
 if (info.rgstate[5] & (STATE_SYSTEM_INVISIBLE |
                            STATE_SYSTEM_OFFSCREEN |
                            STATE_SYSTEM_UNAVAILABLE)) return FALSE;
 ppt->x = info.rgrect[5].left +
             (info.rgrect[5].right - info.rgrect[5].left) / 2;
 ppt->y = info.rgrect[5].top +
             (info.rgrect[5].bottom - info.rgrect[5].top) / 2;
 return TRUE;
}
  case TEXT(' '):
    if (GetCloseButtonCenter(hwnd, &pt)) {
      SendMessage(g_hwndTT, TTM_TRACKPOSITION, 0, MAKELPARAM(pt.x, pt.y));

Instead of positioning the balloon at the cursor position, we put it at the center of the Close button. We use the WM_GET­TITLE­BAR­INFO­EX message to obtain information about the window title bar, specifically checking information about the Close button. After verifying that it is visible and on-screen and enabled, we calculate its center point and return success.

The WM_GET­TITLE­BAR­INFO­EX message is new for Windows Vista. Next time, we’ll cook up a method that works on Windows 2000 and Windows XP.

0 comments

Discussion is closed.

Feedback usabilla icon