Table of contents
1.
Introduction
2.
ATL Window Class
3.
Dialog Box
4.
Using ATL Dialog Wizard
5.
Manually Setting Dialog Box
5.1.
Example
6.
Frequently Asked Questions
6.1.
What is ATL?
6.2.
What are the ways to add a dialog box?
6.3.
What are dialog boxes?
6.4.
Which ATL libraries help in implementing dialog boxes?
7.
Conclusion
Last Updated: Mar 27, 2024

How to Implement a Dialog Box?

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Window development used to be very difficult. This is because we have to rewrite the same codes again and again. Then Microsoft Foundation Class (MFC) came into existence. Using MFC, developers can save their codes and use them when required. Thus, MFC saves a lot of time that was previously wasted in rewriting. But everything cannot be saved. Developers have to write some codes from scratch as well. And so, Active Template Library(ATL) comes in handy. The ATL window class is a mediator between writing code from scratch and MFC.  

Introduction

In this article, we'll learn how you can implement a dialog box in the ATL project. We will discuss two methods of adding dialog boxes. These are - Using ATL dialog wizard and Manual setup. But before that, let's have an introduction to what ATL is. . 

ATL Window Class

ATL stands for Active Template Library. Its library contains numerous classes that can be used to manipulate and develop Windows. ATL contains a set of template-based C++ classes that help you create Component Object Model (COM) objects. ATL supports various COM features, including -

features

The presence of these features makes ATL highly recommended for creating software components. Several commonly used ATL Window classes that are useful in implementing dialog boxes are -  

  • CDialogImpl - This class permits you to implement a modeled or modeless dialog box and process messages using a message map.
     
  • CAxDialogImpl - This class permits you to implement a modeled or modeless dialog box that hosts ActiveX controls.
     
  • CSimpleDialog - This class implements a dialog box with a few basic functionalities.
     

We are done with learning the ATL Window Class. Now, we will have a look at the Dialog box. 

Dialog Box

Dialog boxes are the pop-ups used to interact with the user. Dialog boxes also help in taking input from the user. There are three types of dialog boxes. These are - 

  • Alert Dialog Box - It is typically used to give users a warning message.

alert dialog box

  • Prompt Dialog Box - It is used if the user wants to enter a value before proceeding to the next page.

prompt dialog box

  • Confirm Dialog Box - It allows users to confirm or approve anything. To proceed, the user must click "OK" or "Cancel" when a confirmation window appears. It will return true if the user hits the OK button. If the user presses the Cancel button, confirm() returns false, and null is displayed. 

confirm dialog box

Using ATL Dialog Wizard

To add a dialog box, your project must be an ATL or MFC project. To set up a dialog box using ATL Dialog Wizard, you need to perform the following steps:

  • Create an ATL or MFC project. 
  • Right-click on the project folder. 
  • Click on add a class option. 
  • In the template panel, click the ATL dialog option. 
  • Click open to display the ATL dialog box.  

After this, the ATL dialog wizard adds a dialog box from CDialogImpl, CAxDialogImpl, or CSimpleDialog to your project. The dialog box created using CAxDialogImpl can host both Windows controls and ActiveX. While the remaining two can host only Window controls. 

Let’s now discuss how we can manually set up dialog boxes.  

Manually Setting Dialog Box

The steps for manually setting up the dialog box include - 

  • Derive a class from either library, i.e., CDialogImpl, CAxDialogImpl, or CSimpleDialog.
  • Create an instance of the derived class.
  • Declare a message map to handle messages of dialog boxes. A message map is used to provide a handler function for the given message or command.
  • Specify the dialog box template. 

Example

class DialogBox : public CDialogImpl<DialogBox>
{
public:
  enum { IDD = IDD_MYDLG };

  BEGIN_MSG_MAP(DialogBox)
     MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
     COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnBnClickedCancel)
  END_MSG_MAP()

  LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/,
     BOOL& /*bHandled*/)
  {
     // Initialization code
     return 1;
  }
public:
  LRESULT OnBnClickedCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
};

Frequently Asked Questions

What is ATL?

ATL stands for Active Template Library. It is a library containing numerous classes that can be used to manipulate and develop Windows.

What are the ways to add a dialog box?

There are two ways to add a dialog box to the ATL project. These are - using ATL Dialog Wizard and manually adding it.

What are dialog boxes?

Dialog boxes are the pop-ups used to interact with the user. Dialog boxes also help in taking input from the user. 

Which ATL libraries help in implementing dialog boxes?

Three ATL libraries help in dialog box implementation. These are -  CDialogImpl, CAxDialogImpl, and CSimpleDialog.

Conclusion

In this article, we have discussed the implementation of Dialog Boxes. We also discuss the ATL window class. And, at last, we discussed two approaches that can be used for implementing dialog boxes in ATL projects.  

Do not stop learning! We recommend you read these articles -. 

  1. Debugging tips for ATL
  2. ATL Control Containment
  3. ATL Services
  4. ATL COM property pages
     

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.
Keep learning, and Keep Growing!

Do upvote our blogs if you find them engaging and knowledgeable.

Live masterclass