ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Creating First Visual C++ MFC Project - SDI Application

Updated on December 22, 2016
sirama profile image

I am a software engineer. I have been come across C++,MFC, .net technologies. I like playing video games & reading books.

1. MFC Framework

MFC stands for "Microsoft Foundation Class" libraries. These libraries are useful for creating rich windows based application. The foundation classes were introduced in early 90 and lot of software development firm used it. Till today, there is a lack of engineers available to maintain the legacy code (Huge volume of were written in MFC till 2006).

In this hub, I will explain creating your first MFC Application and in the next coming hub pages, I will give more step-by-step based examples. Here, I used Visual Studio 2005 for this example.


2. Create A Single Document Interface Application

1) First, Start Visual Studio 2005

2) Once the Visual Studio Editor is opened, Click on File => New => Project as shown below


New Project
New Project | Source

The Create New Project action will end up in a "New Project" dialog. Follow the below steps:

1) From the Project Types, expand Visual C++ and select MFC.

2) Select "MFC Applications" from the right side template screen.

3) Give a Name for the project. For this walk-through, I gave a name MyFirstSDI

4) Click the Browse Button and select a folder for the project to be created

5) Make sure "Create Directory for Solution" is checked.

All the above steps are shown in the below screen-shot:

6) Finally, click ok project


Create New MFC Application
Create New MFC Application | Source

Now, a project wizard starts and we have to interact with it in the next coming steps. This wizard collects information from the user to create an initial startup application.

1) Read the instruction and click next. Note that default is MDI (Multiple Document Interface) Application and in the wizard steps, we are going to ask for SDI Application. Have a look at the screenshot for this step:

MFC Application Wizard - Welcome Page
MFC Application Wizard - Welcome Page | Source

2) The MFC Application Wizard is at Application Type Screen now. Select "Single Document" from application type section and leave everything as it is. Click the "Next >" button. This step is shown below:


Select Application Type
Select Application Type | Source

4) The Wizard is in "Compound Document Support" screen. Accept the defaults and click next.

5) The Wizard is in "Document Template Strings" screen. Here also accept defaults and click next.

6) The Wizard is in "Database Support" screen. Accept the default settings and click next.

7) The Wizard is in "User Interface Features" screen. There are multiple options here and I will explain those in a different hub. Click the next button here as well.

8) The Wizard is in "Advanced Feature" screen. Click next here. and in the Final Wizard page Generated Classes

9) We are in the Final Wizard page called "Generated Classes". Click the "Finish Button" here. The screenshot is below:


Final Wizard Page
Final Wizard Page | Source

This Final Page shows Application Wizard Generated classes for the SDI application. There are four classes for required for the Single Document Interface Application. They are:

  1. Document Class
  2. View Class
  3. Application Class
  4. Frame Window Class

You can see all these four classes in the screenshot above (Marked as 1). When you click on the class, information such as Class Name and from where it is derived, header and implementation files (.h, .cpp) are displayed in the below area (Marked as 2). Clicking the Finish button ends the Wizard and generates all the classes.

3. Class View & Solution Explorer View

After the wizard closes, you will see list files created by the wizard in the Solution explorer pane. The solution explorer view shows the list files organised under a specific folder. Have a look at the below screenshot:

Class View
Class View | Source

The screenshot is showing five tabs. By default, one will get Solution Explorer and ClassView tabs. When you click ClassView tab(Marked as 1), the view changes to class view (Marked as 2). In this view, all the classes generated by the Class Wizard is shown. When you click the Class Name, the bottom pane shows the class members.

When you click the Solution Explorer tab (Marked as 3), the view switches to Solution Explorer view. In this view, all the files created by the class wizard is shown. The files are grouped under folder names. For Example, all the header files are placed in the "Header Files" folder (Marked as 4).

4. Add Code to OnDraw() member of CView

Now we will add some code to the AppWizard generated File called MyFirstSDIView.cpp. To do this, move to "Class View" and double click the class CMyFirstSDIView. This step is shown below:

Edit OnDraw() member function
Edit OnDraw() member function | Source

By doing the above step, Visual studio put you in the implementation for the OnDraw() member function of the CView derived class. Add the code as shown in the below screenshot:

OnDraw Code
OnDraw Code | Source

The "OnDraw()" member function gets called whenever the window requires a drawing. For example, let us say a WordPad document is minimised and then maximised. In this case, the document requires a redraw as it comes to the maximized state.

Here, first used the "Device Context" CDC member function "TextOut()" to output the text in the windows display area. The first two members state the location of text from the window's top left corner. The last parameter shows the string that needs to be drawn in the client area of the window. After drawing the text, CDC member function Rectangle() is called.

After drawing the text, CDC member function Rectangle() is called. Here, the first two parameter specifies the top left corner of the rectangle relative to the top-left corner of the window and last two parameters specifies the bottom right corner.

Refer Listing 1.1

5. Compile and Run the Application

Now the application is ready with the modified code. Follow the below procedure to create the executable and run the application (Refer the screenshot - Compile and Run).

  1. From the menu invoke Build=> Build Solution (Marked as 1,2)
  2. The above step will compile all the files required for the project and generates the Executable file. Once the build process complete, you should see 0 Error and 0 warning in the "Output Window" as shown in Screenshot. One can run the application even there exists warning. But, running the application not with when there is at least one error.
  3. After the build process, you can run the application by clicking the "Green Play" icon (Marked as 4)

Compile and Run
Compile and Run | Source

Running the application will open the SDI application as shown in the below screenshot:

The Example SDI Application
The Example SDI Application | Source
SDI Application Window (Resized)
SDI Application Window (Resized) | Source

6. Summary

This SDI Application's menu items does not work as we haven't written any code for that. The NotePad and Wordpad are good examples for SDI Application and MFC library is having rich functionalities to develop such an application.

Code Listings

Listing 1.1

// CMyFirstSDIView drawing

void CMyFirstSDIView::OnDraw(CDC* pDC)
{
	CMyFirstSDIDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	pDC->TextOut(0, 0, _T("My First SDI Application"));
	pDC->Rectangle(30,30,80,80);
}

© 2016 sirama

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.