Visual C++ .NET: Simple Dialog Box


A dialog box is a rectangular object that hosts other controls necessary for an application. To created a dialog box In a Visual C++ .Net application, you can use the Form class, set the MinimizeBox and the MaximizeBox properties to false, and set the FormBorderStyle property to either FixedSingle or FixedDialog values.

One of the best ways to create or add a dialog box to an application is to create its own class instead of adding it to the main source file.

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Dialog Box Example
  6. In the Location combo box, accept the suggestion or type your own. If you are confused, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add Class...
  9. In the Add Class dialog box, in the Categories section, make sure that Visual C++ is selected. In the Templates list, click Generic C++ Class and click Open
  10. In Generic C++ Class Wizard, in the Class Name, type CClassicDialog
  11. In the Base Class, type Form and click Finish
     
    The Generic C++ Class Wizard dialog box 
  12. You may/should receive a message box stating that the base class is not found in the project and asking you if you want to continue. Click Yes
  13. To make the form a managed object, change its creation in the header file as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    __gc class CClassicDialog : public Form
    {
    public:
    	CClassicDialog(void);
    	~CClassicDialog(void);
    };
  14. To implement the object as a dialog box, in its source file, change its constructor as follows:
     
    #include "classicdialog.h"
    #using <mscorlib.dll>
    
    CClassicDialog::CClassicDialog(void)
    {
    	this->MinimizeBox = false;
    	this->MaximizeBox = false;
    	this->FormBorderStyle = FormBorderStyle::FixedDialog;
    }
    
    CClassicDialog::~CClassicDialog(void)
    {
    }
  15. To finalize the application, on the main menu, click Project -> Add New Item...
  16. In the Add New Item dialog box, in the Templates list, click C++ File
  17. In the Name box, replace <Enter name> with Main and click OK:
     
    #include "ClassicDialog.h"
    
    int __stdcall WinMain()
    {
    	CClassicDialog *Dlg = new CClassicDialog();
    	Application::Run(Dlg);
    
    	return 0;
    }
  18. Press Ctrl + F5 to test the form
     
    A Simple Dialog Box
  19. Close the dialog box
  20. To change some properties of the dialog box, change the header file as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class CClassicDialog : public Form
    {
    public:
    	CClassicDialog(void);
    	~CClassicDialog(void);
    };
  21. To change the caption and the dimensions of the dialog box, change the source file as follows:
     
    #include "classicdialog.h"
    #using <mscorlib.dll>
    
    CClassicDialog::CClassicDialog(void)
    {
    	this->MinimizeBox = false;
    	this->MaximizeBox = false;
    	this->FormBorderStyle = FormBorderStyle::FixedDialog;
    
    	// The caption for the dialog box
    	this->Text = S"Classic Dialog";
    	// The dimensions of the dialog box
    	this->Size = Drawing::Size(420, 345);
    }
    
    CClassicDialog::~CClassicDialog(void)
    {
    }
  22. Press Ctrl + F5 to test the application

 

Related Applications:

Body Tag Formatter

 

Home Copyright © 2002 FunctionX, Inc.