CBT Message Box

A Win32 API centering enhancement for message boxes.

.

Environment: 32-bit Windows

Introduction

Message boxes are normally placed in the center of the desktop window. It is often desirable to create messages boxes over the parent app window. This cannot be done by using the standard Win32 API call:

INT MessageBox(HWND,LPTEXT,LPCAPTION,UITYPE);

The code below centers a message box over a parent window by using a CBT (computer-based training) hook. All message box calls are made with a substitute function called "CBTMessageBox," which inserts the CBT hook prior to activating a message box.

The CBTProc function processes a thread-specific hook. The needed notification code is HCBT_ACTIVATE, which is issued whenever a new window is about to be activated (made visible). The wParam holds the forthcoming window handle value. The parent window handle can be found by using the GetForegroundWindow() function.

Centering is done by finding the center point of the parent window and by calculating the upper left corner starting point of the message box. The starting point will be adjusted if the message box dimensions exceed the desktop window region.

The CBT hook will remain active until the necessary HCBT_ACTIVATE code is issued. Because several other CBT codes may be waiting in the hook queue when the SetWindowsHookEx call is made, it is useful to have the CBTProc continue the hook chain if another code is issued first. The CallNextHookEx function helps to ensure proper chaining of hooks issued by other apps.

The CBT hook process can be used to center any window to another. Just declare the hhk=SetWindowsHookEx function before activating a window.

Steps to Add CBTMESSAGEBOX to WIN32 API Code

(#include <windows.h>)

//////////////////////////////////
//1) Declare functions

INT CBTMessageBox(HWND,LPSTR,LPSTR,UINT);
LRESULT CALLBACK CBTProc(INT, WPARAM, LPARAM);

//////////////////////////////////
//2) Declare hook handle as global

HHOOK hhk;

//////////////////////////////////
//3) Add functions

INT CBTMessageBox(HWND hwnd, LPSTR lpText, LPSTR lpCaption,
                  UINT uType)
{
  hhk = SetWindowsHookEx(WH_CBT, &CBTProc, 0,
                         GetCurrentThreadId());
  return MessageBox(hwnd, lpText, lpCaption, uType);
}

LRESULT CALLBACK CBTProc(INT nCode, WPARAM wParam, LPARAM lParam)
{
  HWND  hParentWnd, hChildWnd;    // msgbox is "child"
  RECT  rParent, rChild, rDesktop;
  POINT pCenter, pStart;
  INT   nWidth, nHeight;

  // notification that a window is about to be activated
  // window handle is wParam
  if (nCode == HCBT_ACTIVATE)
  {
    // set window handles
    hParentWnd = GetForegroundWindow();
    hChildWnd  = (HWND)wParam;

    if((hParentWnd != 0) &&
       (hChildWnd != 0) &&
       (GetWindowRect(GetDesktopWindow(), &rDesktop) != 0) &&
       (GetWindowRect(hParentWnd, &rParent) != 0) &&
       (GetWindowRect(hChildWnd, &rChild) != 0))
    {
       // calculate message box dimensions
       nWidth  = (rChild.right - rChild.left);
       nHeight = (rChild.bottom - rChild.top);

       // calculate parent window center point
       pCenter.x = rParent.left+((rParent.right
                 - rParent.left)/2);
       pCenter.y = rParent.top+((rParent.bottom
                 - rParent.top)/2);

       // calculate message box starting point
       pStart.x = (pCenter.x - (nWidth/2));
       pStart.y = (pCenter.y - (nHeight/2));
    
       // adjust if message box is off desktop
       if(pStart.x < 0) pStart.x = 0;
       if(pStart.y < 0) pStart.y = 0;
       if(pStart.x + nWidth > rDesktop.right)
          pStart.x = rDesktop.right - nWidth;
       if(pStart.y + nHeight > rDesktop.bottom)
          pStart.y = rDesktop.bottom - nHeight;

       // move message box
       MoveWindow(hChildWnd,
                  pStart.x, pStart.y,
                  nWidth, nHeight,
                  FALSE);
    }
    // exit CBT hook
    UnhookWindowsHookEx(hhk);
  }
  // otherwise, continue with any possible chained hooks
  else CallNextHookEx(hhk, nCode, wParam, lParam);
  return 0;
}

//////////////////////////////////
//4) Substitute CBTMessageBox(HWND,LPTEXT,LPCAPTION,UITYPE);
//instead of MessageBox(HWND,LPTEXT,LPCAPTION,UITYPE);
//to produce a centered message box.

Downloads

Download source - 2 Kb


Comments

  • Neat result and Fun to learn

    Posted by Bni on 04/20/2005 04:30pm

    Make the reader discovering new usage for hook procedure

    Reply
  • Nice work!

    Posted by Legacy on 01/28/2004 12:00am

    Originally posted by: Yasuhiko Yoshimura

    Thank you very much!

    Reply
  • Very useful

    Posted by Legacy on 09/18/2003 12:00am

    Originally posted by: H.Danler

    I used parts of your code to center a child window over a parent window. Works very good. Thanks a lot.

    Reply
  • Beautiful work!

    Posted by Legacy on 02/12/2003 12:00am

    Originally posted by: Igor Galitsky

    Thanks.

    Reply
  • A question....

    Posted by Legacy on 02/04/2003 12:00am

    Originally posted by: Time

    can I know a window is dialog or not dialog? If we can do that I think a global hook will good.


    ========================
    SkinMagic SDK for C/C++
    http://www.appspeed.com
    ========================

    Reply
  • Good Example, Creative!!

    Posted by Legacy on 01/31/2003 12:00am

    Originally posted by: Zhefu Zhang

    Good job

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds