ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Creating First Visual C++ MFC Project - SDI Application

Updated on December 22, 2016
sirama profile image

I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 Yrs. 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.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)