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
23
votes
7 answers

Unit testing MFC UI applications?

How do you unit test a large MFC UI application? We have a few large MFC applications that have been in development for many years, we use some standard automated QA tools to run basic scripts to check fundamentals, file open etc. These are run by…
titanae
  • 2,259
  • 2
  • 21
  • 31
22
votes
2 answers

How to set compile flag /bigobj in visual studio?

I have a cpp file that contains a huge array. I get a fatal error (exceeded object file format limit) and the suggestion to compile with the flag /bigobj Where do I set this flag in visual studio?
ragnarius
  • 5,642
  • 10
  • 47
  • 68
22
votes
1 answer

How to Set the Initial Focus of the control in the particular window?

I created an application in which I use window procedure to keep track of all the controls in the window. My question is, how do I initially set the focus to the first created control in the window?
karthik
  • 17,453
  • 70
  • 78
  • 122
22
votes
7 answers

Static or dynamic linking the CRT, MFC, ATL, etc

Back in the 90s when I first started out with MFC I used to dynamically link my apps and shipped the relevant MFC DLLs. This caused me a few issues (DLL hell!) and I switched to statically linking instead - not just for MFC, but for the CRT and…
Rob
  • 76,700
  • 56
  • 158
  • 197
22
votes
7 answers

Best way to design for localization of strings

This is kinda a general question, open for opinions. I've been trying to come up with a good way to design for localization of string resources for a Windows MFC application and related utilities. My wishlist is: Must preserve string literals in…
Nick
  • 6,808
  • 1
  • 22
  • 34
22
votes
2 answers

DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR When, How and Why?

I found that Windows has some new Windows Data Types DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR can you tell me when, how and why to use them?
ju.
  • 483
  • 1
  • 5
  • 7
21
votes
3 answers

VS 2008, MFC: add OnInitDialog - how?

This is my first MFC application away from VC6, and I feel a little bit stupid: How do I add the OnInitDialog handler? (I know how to add it manually, but that's a pain in the long run). double-clicking the dialog - nothing. right click the dialog -…
peterchen
  • 40,917
  • 20
  • 104
  • 186
21
votes
3 answers

What is the correct way to programmatically quit an MFC application?

Using windows MFC C++. I have a third party app that calls a user-defined method in my CWinApp derived class. This method is called after InitInstance(). If there is an error in this method, such that an exception is thrown and caught in a…
User
  • 62,498
  • 72
  • 186
  • 247
21
votes
4 answers

MFC does not support WINVER less than 0x0501

I have a C++ project that references many other projects/libraries. This is for an application that was created many years ago. About every once a year it is updated and a new version is done. I've used Visual Studio 6 to update and build new…
user3242337
  • 253
  • 1
  • 2
  • 5
21
votes
3 answers

MBCS Error building MFC C++ project with Visual Studio

I opened my existing MFC project using Visual Studio and when I build I get the following error message: Error 1 error MSB8031: Use of MBCS encoding in MFC projects require an additional library to be downloaded and installed. Please see…
Raman Sharma
  • 4,551
  • 4
  • 34
  • 63
21
votes
2 answers

Prerequisites for learning MFC programming

I know a bit of C++ and C and the project I am working with now is a whole lot of MFC programming. Can someone experienced tell me the prerequisites for learning MFC. Also, What the are best sources to learn from ? Any particular book or video…
Bounty Collector
  • 615
  • 7
  • 19
20
votes
7 answers

Is GetLastError() kind of design pattern? Is it good mechanism?

Windows APIs uses GetLastError() mechanism to retrieve information about an error or failure. I am considering the same mechanism to handle errors as I am writing APIs for a proprietary module. My question is that is it better for API to return the…
zar
  • 11,361
  • 14
  • 96
  • 178
20
votes
7 answers

How can I get the value (string) of the current selection in a combobox?

How can I get the value (string) of the current selection in a combobox? (Not the integer index)
railsmail
20
votes
7 answers

Exceptions silently caught by Windows, how to handle manually?

We're having problems with Windows silently eating exceptions and allowing the application to continue running, when the exception is thrown inside the message pump. For example, we created a test MFC MDI application, and overrode OnDraw: void…
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
20
votes
5 answers

Get path to My Documents

From Visual C++, how do I get the path to the current user's My Documents folder? Edit: I have this: TCHAR my_documents[MAX_PATH]; HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, my_documents); However, result is…
Smashery
  • 57,848
  • 30
  • 97
  • 128