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
11
votes
1 answer

Using nullptr in API function calls?

Using C++ with Visual Studio 2010. I'm in the process of converting my NULL's to nullptr's. With my code this is fine. However if I make a call to WINAPI such as: __checkReturn WINOLEAPI OleInitialize(IN LPVOID pvReserved); normally I would have…
User
  • 62,498
  • 72
  • 186
  • 247
11
votes
2 answers

MFC CMenu tooltip not being displayed

I tried using something like this to set the tool tip of a CMenu item (as described here) but it is just being displayed in a single line and the line break is not visible. // read control id UINT id = menu->GetMenuItemID(1235); // modify caption…
Norman
  • 123
  • 5
11
votes
4 answers

How to kill a MFC Thread?

I spawn a thread using AfxBeginThread which is just an infinite while loop: UINT CMyClass::ThreadProc( LPVOID param ) { while (TRUE) { // do stuff } return 1; } How do I kill off this thread in my class destructor? I think something…
Nick
  • 13,238
  • 17
  • 64
  • 100
11
votes
8 answers

Converting VC++6/Win32 project to VS2010 C++/Win32 project: Known issues

Do you know of any resources highlighting known or possible issues during conversion of a VC++6/Win32 project to VS2010 C++/Win32 project type? I'm interested in all kinds of issues: Compiler options compatibility Compile-time issues Link-time…
Maxim Gueivandov
  • 2,370
  • 1
  • 20
  • 33
11
votes
1 answer

How to execute child console programs without showing the console window from the Win32 GUI program?

(I've searched SO answers and found no clear solution to this problem.) I'm working on a MFC GUI program. This program runs various child programs including console program and shell command script(.cmd). Initially it displayed one GUI window and…
9dan
  • 4,222
  • 2
  • 29
  • 44
11
votes
3 answers

Binary fields encoding/serialization format in a proprietary XML file (Roche LC480 .ixo file)

I recently received an example export file generated by the Roche LightCycler 480 instrument. It uses a proprietary XML format, for which I haven't found a specification yet. From such types of files, I would like to extract some information…
Alex
  • 13,024
  • 33
  • 62
11
votes
3 answers

MFC CView (CFormView) destruction crash

As per this stackoverflow question: What is the correct way to programmatically quit an MFC application? I am using AfxGetMainWnd()->PostMessage(WM_CLOSE,0,0); to exit an MFC program. (SDI, CFrameWnd containing a CSplitterWnd with two…
Smash
  • 3,722
  • 4
  • 34
  • 54
11
votes
3 answers

How do you scale a CBitmap object?

I've loaded a CBitmap object from a resource ID, and I'm now wanting to scale it to 50% its size in each dimension. How might I go about this?
Smashery
  • 57,848
  • 30
  • 97
  • 128
11
votes
7 answers

How to load .png , .jpeg images using MFC?

Hi i want to load png images and jpeg images. can anyone help me?
Ashish
  • 8,441
  • 12
  • 55
  • 92
11
votes
1 answer

MFC multiple radio buttons groups on dialog

I have 2 groups of radio boxes on a dialog. How can I specify which button is in which group? Because right now when I select one, all the others get unselected, even the ones from the other group.
melculetz
  • 1,961
  • 8
  • 38
  • 51
11
votes
2 answers

Visual Studio 2012: C++ compiler ignoring user-specified include directories

I have the common error fatal error C1083: Cannot open include file: 'afxres.h': No such file or directory. Search engines show up lots of hits for this, but none of the suggested solutions work for me. Normally this would appear to be a problem…
Kylotan
  • 18,290
  • 7
  • 46
  • 74
11
votes
3 answers

What is the relationship of CloseWindow and WM_CLOSE

I'm a bit confused currently: Are WM_CLOSE and ::CloseWindow in any way "related" or are for completely different things? The docs for Closing Windows don't mention the API function CloseWindow at all. Should CloseWindow be really called…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
11
votes
4 answers

How to create a modal dialog in tkinter?

I have a MFC application which runs some embedded Python scripts. I am trying to make one of the dialogs this embedded script creates modal, but I am not having much success. Can anyone point me the way to make a modal dialog? Do I need to use a…
Mac
  • 3,397
  • 3
  • 33
  • 58
11
votes
1 answer

Confused between logical coordinates and device coordinates in Windows API

I have been looking into a Visual Studio C++ Windows application project which used two functions SetWindowExt (...) and SetViewportExt (...). I am confused about what these two functions do and why they are necessary. Searching about these…
Viku
  • 2,845
  • 4
  • 35
  • 63
11
votes
5 answers

How can an MFC application terminate itself?

What is the proper way for an MFC application to cleanly close itself?
Mike
  • 1,276
  • 4
  • 16
  • 27