Wednesday, 17 August 2011

Message Box with custom commit buttons

Windows API and MFC framework offer ready to use Message Box dialog which can display, depending on a provided flag, one or group of more commit buttons with captions from a predefined set: OK, Yes, No, Cancel, Abort, Retry and Ignore. If dialog contains a question, it is a good practice to make buttons displaying specific responses, e.g. "Run" - "Don't run", "Apply" - "Don't apply" etc. The easiest and quickest way to achieve this is to create a new, custom dialog and add onto it buttons with desired text. Resource editor in Visual Studio creates by default dialog with two buttons, OK and Cancel and you can rename them, providing that new captions will match OK and Cancel functionality. For other buttons, add BN_CLICKED handlers (ON_BN_CLICKED entry in message map). The purpose of this dialog is just to pick the user's answer on a question and not to do any hard work. Caller should read return value of (custom) Message Box and take further actions depending on it.

Return value of the standard message box (one from a predefined set IDOK, IDYES, ...) identifies user's choice and we can return these predefined values or our custom return codes, e.g. ID_RUN, ID_APPLY...We can also reuse resource ID values of our custom buttons for this purpose. Standard Message Box is a modal dialog so our dialog needs to be created as modal (CDialog::DoModal). DoModal returns value that was passed to CDialog::EndDialog. This function should be called in our custom button BN_CLICKED handlers.

In the following example, our custom Message Box has button "Apply". Its BN_CLICKED handler is OnBnClickedButtonApply. Caller identifies user's choice with DoModal return value:

Our Message Box which has "Apply" button:

Caller:

8 comments:

Jack Alexa said...

I'm very Glad to visit your informational Post. Custom Window Boxes Packaging boxes are really necessary for any kind of product. If you want to pack your products in a stylish way then use these Custom Window Boxes with customized designs, styles, colors, shapes, and stocks. We are also offering error-free design and free shipping services all over the USA.

Custom Packaging Boxes said...

Your article has really good reading, thought-provoking information. You make several sound points I find agreeable. Your article is a good example of superior writing from a writer that cares. Thanks for sharing this information. Custom Window Boxes

thomasjack said...

I appreciate your post thanks for sharing the information.
Cake boxes Kentucky
Candle boxes Kentucky

thomasjack said...

Amazing product thanks for sharing with us It is very informative.
custom boxes Kansas
apparel box manufacturer

thomasjack said...

Amazing product thanks for sharing with us It is very informative.
custom boxes Kansas
apparel box manufacturer

markthomson said...

I felt exceptionally cheerful while perusing this site. This was truly extremely instructive site for me. I truly enjoyed it. This was truly a sincere post. Much obliged!. Custom Boxes with logo

Teresa D Dolan said...

Thank you for sharing this article. It's really informative article. I really enjoy your site I am waiting for a new article.
cigarette boxes
vape cartridge packaging

Serina johson said...

Good blog thanks for sharing the information.
Photography Packaging packaging USA
cheap custom Perfume Boxes