May 5th, 2014

Getting the location of the Close button in the title bar

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.

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.