Questions tagged [mfc]

This tag should be used for questions concerning Microsoft Foundation Class Library (MFC), a C++ framework for Windows desktop GUI application programming. You should specify a tag for the version of C++ or Visual Studio being used. Due to the size of MFC, additional tags such as [com], [wininet], [winapi] or other subject matter tags are helpful.

The Microsoft Foundation Class (MFC) framework provides an object-oriented abstraction layer on top of a large subset of the Win32 API. MFC offers a range of classes and templates covering almost all features used in developing a Windows desktop application. It provides models of architecture for the application in which it will be built, namely document - model - view available in three options:

  • SDI applications (single paper interface: one window),
  • MDI (multiple document interface: multiple windows)
  • Dialog-Based applications (a modal dialog box interface).

There are several Visual Studio supplied application templates which provide a starting place for the look and feel of a new application. New templates with new functionality and behavior seen in Microsoft applications (docking windows) such as Visual Studio have been introduced over the years.

MFC uses a single inheritance model of the C++ language (i.e. no multiple inheritance); all the classes form a hierarchy. The newer MFC classes are identified by the prefix CMFC, as in CMFCPropertyPage, or by adding an Ex suffix, as in the CPropertyPageEx class.

Almost all MFC classes inherit from CObject and all window classes or control classes inherit from the CWnd class, which is the base class for all windows and includes all the basic treatments performed on a window such resizing or moving the window.

This MSDN chart give us an idea of class hierarchy:
http://msdn.microsoft.com/en-us/library/ws8s10w4.aspx

For Windows UI development, MFC is a very thin layer over Win32 API, and hence is equivalently as fast as native Win32 GUI application. Some classes, for example, sockets and networking, may be considered slightly thicker. For COM and related stuff, it is considered quite heavy (and hence ATL is preferred).

The MFC model can feel restrictive in that when the application architecture does not quite fit the MFC paradigm of desktop GUI application, the framework can be difficult to extend without bypassing the Visual Studio class wizards and opting for hand coding.

For new development, STL containers should be preferred over the original MFC containers (CList, CArray, etc.). Why STL containers are preferred over MFC containers?

MFC multi-threading support is some what heavy and C++17/C++20 multi-threading and coroutines may be a better approach for background worker threads. However MFC has good support for user interface threads which require a message pump as well as support for the WinAPI thread synchronization library.

While earlier versions of Visual Studio provided an easy selection to install the MFC components as one of several standard packages, later versions appear to have MFC as more of an option that must be turned on.

More details at http://en.wikipedia.org/wiki/Microsoft_Foundation_Class_Library

13003 questions
135
votes
6 answers

LPCSTR, LPCTSTR and LPTSTR

What the difference between LPCSTR, LPCTSTR and LPTSTR? Why do we need to do this to convert a string into a LV / _ITEM structure variable pszText: LV_DISPINFO dispinfo; dispinfo.item.pszText = LPTSTR((LPCTSTR)string);
nothingMaster
  • 1,353
  • 2
  • 9
  • 6
119
votes
3 answers

What's the fundamental difference between MFC and ATL?

Assuming I am only using them for "normal" GUI programs (no COM, no ActiveX, nothing fancy), what is the fundamental difference I will see between ATL and MFC, to help me figure out which one to use? I've done some searches on the web, but…
user541686
  • 205,094
  • 128
  • 528
  • 886
91
votes
16 answers

How do you convert CString and std::string std::wstring to each other?

CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CStrings as keys, so I want to convert the CString into a std::string. Writing a CString hash function seems to…
user25749
  • 4,825
  • 14
  • 61
  • 83
77
votes
6 answers

How do I decide whether to use ATL, MFC, Win32 or CLR for a new C++ project?

I'm just starting my first C++ project. I'm using Visual Studio 2008. It's a single-form Windows application that accesses a couple of databases and initiates a WebSphere MQ transaction. I basically understand the differences among ATL, MFC, Win32…
John M Gant
  • 18,970
  • 18
  • 64
  • 82
61
votes
9 answers

Convert CString to const char*

How do I convert from CString to const char* in my Unicode MFC application?
Attilah
  • 17,632
  • 38
  • 139
  • 202
58
votes
1 answer

How do I call a static method of another class

I have a class, lets say CAppPath which has a static method: public: static CString GetAppPath(); and in CAppPath.cpp it's defined as: CString CAppPath::GetAppPath() { return "C:\..\MypAth"; } Now I have another class CXMLHandler, and I…
Simsons
  • 12,295
  • 42
  • 153
  • 269
56
votes
9 answers

error Please #define _AFXDLL or do not use /MD[d] occurs even after making changes in Project Properties

I am working on Win32 project in Visual Studio 2011. It is generating MFC error when I includes afx.h or afxwin.h. To resolve this, I have made the following changes in the Project Properties tab : 1) Use of MFC : Use MFC in a shared DLL 2) C++ ->…
SayaliK
  • 563
  • 1
  • 4
  • 6
52
votes
12 answers

Convert MFC CString to integer

How to convert a CString object to integer in MFC.
rahul
50
votes
2 answers

Which Font is the default for MFC Dialog Controls?

The picture below (enlarged, so you better see the differences) shows Font differences between dynamically created Edit controls (the upper two examples) and Edit Controls created from the Dialog Editor (the lower example). How can I make the font…
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
50
votes
11 answers

C++ MFC vs .NET?

My colleagues are using Visual Studio 2002 and uses the C++ MFC. I am developing in C #. It has not been any problems before, but now questioning our customers if we really should develop in different environments. My colleagues think (of course)…
magol
  • 6,135
  • 17
  • 65
  • 120
47
votes
4 answers

What's with the "Afx" in StdAfx.h?

I'm just curious what Afx stands for. And what about Fx in FxCop?
Qwertie
  • 16,354
  • 20
  • 105
  • 148
45
votes
3 answers

What is the difference between BOOL and bool?

In VC++ we have the data type “BOOL” which can assume the value TRUE or FALSE, and we have the data type “bool”, which can assume the value true or false. What is the difference between them and when should each data type be used?
Umesha MS
  • 2,861
  • 8
  • 41
  • 60
36
votes
4 answers

Error LNK2019 when using GetFileVersionInfoSize()

I just included this bit in my already working code, but I am getting an LNK2019 error. I'll paste the error after pasting the code. The Class CAboutDlg has: public: CStatic m_VersionInfoCtrl; virtual BOOL OnInitDialog(); }; The Function…
Neophile
  • 5,660
  • 14
  • 61
  • 107
36
votes
5 answers

App does not run with VS 2008 SP1 DLLs, previous version works with RTM versions

Since our switch from Visual Studio 6 to Visual Studio 2008, we've been using the MFC90.dll and msvc[pr]90.dlls along with the manifest files in a private side-by-side configuration so as to not worry about versions or installing them to the…
crashmstr
  • 28,043
  • 9
  • 61
  • 79
35
votes
4 answers

How to detect a CListCtrl selection change?

I want to execute some code when the user selects a row in a CListCtrl (report view, I don't care about the other viewing modes). How do I catch this event? is there some message I can map or a method like "OnSelectionChanged" or something like…
LLucasAlday
  • 2,349
  • 11
  • 34
  • 41
1
2 3
99 100