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
13
votes
4 answers

What is the lifetime of a CWnd obtained from CWnd::FromHandle?

According to msdn, when I get a CWnd* with CWnd::FromHandle, The pointer may be temporary and should not be stored for later use. What is meant by "later use" is not clear to me. Is it only the scope of the current method? As far as I know, there…
remio
  • 1,242
  • 2
  • 15
  • 36
13
votes
3 answers

CEdit control MFC, placing cursor to end of string after SetWindowText

I am using VC9, I've a CEdit control whose contents are reset to default test (say - "fill-in") at the click of a button and then I call SetFocus for the CEdit control. The problem is that the cursor blinks at the start of the default text, and i…
user179430
13
votes
6 answers

Add tooltip to a CStatic

I haven't been able to find a concise chunk of code that allows me to add/display tooltips to a CStatic (and CLed) control. Obviously, the standard code to do so does not apply for this type of control. Can someone post code snippets?
Jason
  • 2,147
  • 6
  • 32
  • 40
13
votes
3 answers

How to detect "Use MFC" in preprocessor

For a static Win32 library, how can I detect that any of the "Use MFC" options is set? i.e. #ifdef ---BuildingForMFC--- .... #else ... #endif
peterchen
  • 40,917
  • 20
  • 104
  • 186
13
votes
1 answer

How to host a WPF form in a MFC application

I'm looking for any resources on hosting a WPF form within an existing MFC application. Can anyone point me in the right direction on how to do this?
mkinkade
  • 315
  • 2
  • 10
13
votes
3 answers

how to get numeric value from edit control

Sorry if this is too trivial, but I can't figure out how to get numeric value entered into edit control. MFC edit control represented by CEdit class. Thank you.
pic11
  • 14,267
  • 21
  • 83
  • 119
12
votes
1 answer

C# .NET User Control inside native app. Resource chain problems

I am wrapping up an MFC extension DLL (MFCXDLL_2) to make its functionality available for C# programmers. The wrapper is a “Regular DLL using shared MFC DLL” with “Common Language Runtime Support (/clr)”. (Mixed mode). The classes in MFCXDLL_2…
kjella
  • 211
  • 1
  • 10
12
votes
1 answer

How to Remove the Readonly attribute of a File MFC

In my MFC application I have set the read only attribute on a particular file. I have done this by using the SetFileAttributes() function. At some point I have to remove that attribute of that file again. Can anyone explain how to do this?
JijeshKV
  • 670
  • 2
  • 7
  • 26
12
votes
1 answer

Difference between DECLARE_DYNAMIC and DECLARE_DYNCREATE?

Could you please let me know what is difference between DECLARE_DYNAMIC and DECLARE_DYNCREATE? Where exactly we can use them?
Hara
  • 1,467
  • 4
  • 18
  • 35
12
votes
4 answers

Event when a particular Slider is changed

I am writing a C++ MFC Dialog based Application and my program has lots of sliders. I want the program to call a function depending on which Slider is being changed by the user. I tried using GetPos() but not much success so far. Any easier way of…
Neophile
  • 5,660
  • 14
  • 61
  • 107
12
votes
2 answers

C++ MFC Get current date and time

I've been programming in VB.NET for most of my very programming career. I have a C++ project provided to me, which I need to make a few modifications, and I am feeling hopelessly lost and confused. It is a Visual Studio 2008 MFC project in C++. an…
Adam
  • 187
  • 1
  • 2
  • 14
12
votes
3 answers

Always-in-front dialogs

Is there a way to create a modeless dialog box in C++ MFC which always stays on top of the other windows in the application? I'm thinking sort of like the Find dialog in Visual Studio 2005 - where it stays on top, but you can still edit the…
Smashery
  • 57,848
  • 30
  • 97
  • 128
12
votes
1 answer

confused about MFC/.net/WPF

I want to create a desktop application for windows, I know there's one type of application based on Win32 API and another based on the .net framework. So what is with the MFC, WPF, WinForm etc.? From what I've seen they are graphical APIs, so which…
brian14708
  • 1,941
  • 2
  • 20
  • 28
12
votes
1 answer

MFC CComboBoxEx GDI Resource Leak

I've a very simple MFC dialog, with a single CComboBoxEx control. .rc IDD_MFCAPPLICATION1_DIALOG DIALOGEX 0, 0, 160, 200 STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME EXSTYLE WS_EX_APPWINDOW CAPTION…
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
12
votes
4 answers

AfxGetInstanceHandle() triggers an assertion failure

I am using MFC in my C++ program (using Visual Studio 2008). I have to call AfxGetInstanceHandle() at the begining of my program. This function triggers a break point: AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle() {…
0x26res
  • 11,925
  • 11
  • 54
  • 108