VC Customized Personalized MessageBox solution _c language

Source: Internet
Author: User

I believe anyone who has studied VC knows the use of the MessageBox () function:

int MessageBox (
 hwnd hwnd,     //Handle to owner window
 LPCTSTR lptext,   //text in message box
 LPCTSTR LP Caption,//message box title
 UINT utype     //message box style
);

Although you can specify some styles in the parameter utype, there is not much you can define in your program for the appearance of the MessageBox. The reason is that when the MessageBox () function is invoked, it has its own message loop internally (all modal dialogs have their own message loops), and when returned the MessageBox dialog window has been destroy, so you have no way to get the window handle of the MessageBox dialog box. But you can tailor your messagebox to your different needs in the following ways:

If you just want to use your own icon to replace the system MessageBox provide a few of the limited icon, with the Messageboxindirect () function on it:

int Messageboxindirect (
 CONST lpmsgboxparams lpmsgboxparams//message box parameters
);

typedef struct { 
 UINT   cbsize; 
 HWND   hWndOwner; 
 HINSTANCE hinstance; 
 LPCTSTR  lpsztext; 
 LPCTSTR  lpszcaption; 
 DWORD   dwstyle; 
 LPCTSTR  Lpszicon; 
 Dword_ptr Dwcontexthelpid; 
 Msgboxcallback Lpfnmsgboxcallback; 
 DWORD   Dwlanguageid; 
} Msgboxparams, *pmsgboxparams;

See the MSGBOXPARAMS structure of the Lpszicon bar, in the use of the process you should prepare a suitable msgboxparams structure, if you want to use their own icon, you must use MB_USERICON this flag.

Above we also talked about can't customize the MessageBox dialog box because there is no way to get its window handle, but we really have no way? Of course, there is a way to use the hook (hook) mechanism. There are a variety of hooks in the Windows system, but here we only use the HK_CBT type hooks. The HK_CBT hook process is called by the system at Wm_move, Wm_size, Wm_active, Wm_create, Wm_destroy, so HK_CBT hooks can be used here. Let's look at how to implement the MessageBox customization process.

1. Install HK_CBT hooks;
2. Call the MessageBox () function;
3. Remove the HK_CBT hook.

The whole process is simple, isn't it? Here we introduce the first and third steps.

Install HK_CBT hooks:

Hhook Hmsgboxhook = SetWindowsHookEx (
 wh_cbt,        //Type of Hook 
 cbtproc,        //Hook procedure (in the below)
 NULL,         //Module handle. Must be NULL (= Docs)
 getcurrentthreadid ()  //Only install to this thread!!!
);

It is important to Setwindowhookex () the next two parameters of the function, using it to distinguish between a global hook or a thread hook, where we simply install a thread hook. So we set the module handle to null and set the thread ID to the ID of this thread.

There is a hook procedure in the Setwindowhookex () function, which is a callback function of the window call, and there is a hook chain in the Windows system, and we need to be careful to ensure that the chain is complete when we write the hook procedure. So our hook procedure to call the CallNextHookEx () function. Here's our hook procedure:

LRESULT CALLBACK cbtproc (int ncode, WPARAM WPARAM, LPARAM LPARAM)
{
  hwnd hwnd;
  if (Ncode < 0) return
    CallNextHookEx (Hmsgboxhook, Ncode, WParam, LParam);
  Switch (ncode)
  {case
  hcbt_activate:
    //Now WParam is message box's handle
    hwnd = (hwnd) WParam;
    We already have the handle to message box, where we can customize the message box!
    return 0;
  }
  Call the next hook, if there are one return
  CallNextHookEx (Hmsgboxhook, Ncode, WParam, LParam);

Remove HK_CBT hooks:

Just call the UnhookWindowsHookEx () function.

Well, we'll write a function in the three steps above, as follows:

int Msgboxex (HWND hwnd, TCHAR *sztext, TCHAR *szcaption, UINT utype)
{  
  int ret;
  Install a thread hook, so we can customize it
  Hmsgboxhook = SetWindowsHookEx (
    wh_cbt, Cbtproc 
    , 
    null,
   getcurrentthreadid ()
    );
  Display a standard message box
  RET = MessageBox (hwnd, Sztext, Szcaption, utype);
  Remove the window Hook
  UnhookWindowsHookEx (hmsgboxhook);
  return ret;
}

In fact, you can also hook up the wm_create message, but it is more complicated to deal with. This is an example of the earlier Windows Platform SDK.

You may say, what is the use of customizing a MessageBox, I would like to have the following uses:

1. You can add a check box control to MessageBox with CreateWindowEx () and subclass MessageBox to handle the check box message

2. Change MessageBox, button, or icon through subclasses to match the interface style of your entire program

As long as you have the handle to the MessageBox dialog box, you can do a lot, a lot ...

In addition, if you know the mechanism of a modal dialog box, you can write a "MessageBox" to replace the system MessageBox in your program. You can refer to Jeffrey Richter's Windows 95 Programming Guide, which gives the pseudo code of the modal dialog box. The traditional electronic version of this book can be downloaded from the personal website of the Czech Republic. This method is also relatively simple (add a message loop, enable/disable owner window), the sample code is not implemented here. Readers can make reference to the relevant information to improve.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.