Updated April 3, 2023
Introduction to MFC C++
MFC C++ stands for Microsoft Foundation Library that is the library based on an object-oriented approach and is used to create and develop the apps that run on the Windows operating system. It began in 1992 by Microsoft organization and soon became very popular. Although Microsoft develops many other applications, the MFC is still the most preferred by the developers.
What is MFC C++?
- MFC is the library standing for Microsoft Foundation Class that comes along with many constants, functions, classes, and data types that make the creation of applications simple for the operating systems that are a part of the Microsoft Windows family. IN MFC applications, various classes are organized in the hierarchical structure of the tree to implement the necessary operations and functionalities. For example, the CObject class is the ancestor of all the classes of C++ that you write in your MFC application. All other classes are direct or indirectly child classes of CObject.
- MFC is compatible with most of the applications such as C++ 11, Boost, and many others. However, it has the feature of non-portability as it uses the graphical user interface as the origin. Moreover, there are a lot of differences that got created between the C++ and the MFC, some of which are the use of lambda functions, user-defined literals, manipulation of date and time, manipulation of strings, the semantics of moving, iterator-based loops, and lambda functions. The MFC architecture is hence binded to the old methodologies of coding that have that kind of pattern. That is why the performance of the application in case of any improvements made in the quality affects the experience of the DX developer as it slows down.
MFC C++ Future
MFC C++ future has extensive implementation and uses in the applications of the real-time scenarios such as the implementation of deployment of pipes that are automated, cloud computing, infrastructure as code, and immutable containers. The only extra efforts that we need to take care to remove the GUI that is the graphical user interface of MFC C++ so that it can also run on the other operating systems such as Linux, and you will have to implement the coding in the configuration as code so that we can reap the following benefits from it in future.
- The signals and the logic of business can be removed from the loop by simply replacing the MFC that provides the loop of window message processing with the simple while loop inside which we can place our main function that is the updated one and which implements our business logic.
- The C++ libraries that are standard in nature can be used instead of MFC containers.
- We can make our system that are tools that we will be using the cross-platform. So, for example, the vcxproj file can be replaced with CMake, which is provided and supported by the visual studio on the windows operating system and many other operating systems and developing environments such as VSCode.
- We can make a choice for the version and the compiler that we will be using for our development. Note that the latest features of C++ programming languages are only supported by the Windows operating system of the latest release. This version is usually the newer version that version that is set by default in your installed application in your system.
In order to implement the above changes, you can make use of the following code and compare it with the existing code on the system. Thereafter, we can iterate over the code of mapping in your MFC.
Code:
CMap<CString, LPCSTR, cSampleBar *, cSampleBar *> educbaSampleMap;
cSampleBar sampleFoo(3.1415);
educbaSampleMap.SetAt("LongBar"), &sampleFoo);
POSITION educbaSamplePosition = educbaSampleMap.GetStartPosition();
while (educbaSamplePosition != NULL)
{
cSampleBar * educbaPointerBar;
CString string;
// Retrieve value and key of string (educbaPointerBar)
educbaSampleMap.GetNextAssoc(educbaSamplePosition, string, educbaPointerBar);
// Code to write for iterating using loop statements
}
In case of C++ 17 version the constructs can be compared to following code snippet –
std::educbaUnorderedMap<std::string, CBar> educbaSampleMap;
educbaSampleMap.emplace(“educbaLongBar”, 3.1415);
for(auto & [string, bar] : educbaSampleMap)
{
// Code to write for iterating using loop statements
}
Creating an MFC Application
In order to create a new application of MFC, you can follow the below steps provided if you want to create a dialog-based MFC app.
- Click on the Main Menu and then choose the File option and then click on New. Next, proceed with choosing the project option.
- Further, you can search for MFC by simply searching for it in the search box and then choose the MFC App option from the list from the result.
- You can make the changes in the default settings as per your requirement and then click on Create so that you can access the MFC application wizard.
- This is the screen where you can make the modifications in your configurations as per need and then click on Finish.
The screen of configurations looks as shown below.
Output:
Create a New MFC Project
In order to create a new project of MFC C++, you need to follow the below-mentioned steps:
- Click on the menu that is provided at the top and then click on the File option; further, you will have to click on New Project.
- Thereafter, you have to click on project types and select the option Visual C++.
- You will then have to click on the Win32 project from the list of templates provided.
- You can mention any name that you want to assign to your project and then click on the OK button at the bottom.
The interface will look as shown below.
Output:
Example of MFC C++
Given below is the example mentioned:
You can refer to the official documentation of Microsoft, where various sample examples are provided on Github.
To do so, you can visit the following link – https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-samples?view=msvc-160.
Let us consider one example where we will create an MFC C++ application.
Code:
// EducbaSampleMFC.h
// Class for application
class CEducbaSampleMFC : public CEducbaWinApplication
{
public:
virtual BOOL InitInstance();
};
// class for implementing frame in windows
class CEducbaFrame : public CEducbaWindows
{
public:
CEducbaFrame();
protected:
// Further two messages will be the part of MFC dispatch system of library
afx_msg void SampleLeftButtonPress(UINT sampleFlags, CPoint point);
afx_msg void OccurrenceOfPaint();
MSG_MAP_DECLARATION()
};
EducbaSampleMFC.cpp implementation file for the EducbaSampleMFCExample application:
#include <afxwin.h>
#include "educba_sample_app.h"
CEducbaSampleMFC sampleObj; // object of CEducbaSampleMFC class
BOOL CEducbaSampleMFC::InitInstance()
{
MFCMainWindow = new CEducbaFrame();
MFCMainWindow->ShowWindow(m_nCmdShow);
MFCMainWindow->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CEducbaFrame, CEducbaWindows)
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
END_MESSAGE_MAP()
CEducbaFrame::CEducbaFrame()
{
Create(NULL, "EducbaSampleMFCExample Application");
}
void CEducbaFrame::SampleLeftButtonPress(UINT sampleFlags, CPoint point)
{
TRACE("Entering CEducbaFrame::SampleLeftButtonPress - %lx, %d, %d\n", (long) sampleFlags, point.x, point.y);
}
void CEducbaFrame::OccurrenceOfPaint()
{
CPaintDC dc(this);
dc.TextOut(0, 0, "Educba is the best site to increase your knowledge!");
}
Output:
Conclusion
MFC C++ stands for Microsoft Foundation Library that is the library based on an object-oriented approach and began in 1992 by Microsoft organization and soon became very popular. It is used for creating and developing the apps that run on Windows operating system.
Recommended Articles
This is a guide to MFC C++. Here, we discuss the introduction, future, creation of an MFC application, and creating a new project, and examples. You may also have a look at the following articles to learn more –