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
12
votes
3 answers

Creating HBITMAP from memory buffer

I have an application which loads some blob data out of a database which can represent png formatted or raw binary data for various bitmaps and icons. This is being stored in a std::vector I'm using CImageList objects to display…
AJG85
  • 15,849
  • 13
  • 42
  • 50
12
votes
6 answers

Internationalization in MFC

It's finally (after years of postponing) the time to localize my app in a few other languages other than English. The first challenge is to design the integration into my C++ / MFC application that has dozens of dialogs and countless strings. I came…
Cosmin
  • 6,623
  • 3
  • 26
  • 28
12
votes
4 answers

How to load a png resource into picture control on a dialog box?

I tried the following code on OnInitDialog() but nothing was shown. m_staticLogo.SetBitmap(::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_LOGO))); where m_staticLogo is the static picture control and IDB_LOGO is the resource ID of the png file.
david.healed
  • 1,289
  • 7
  • 16
  • 31
12
votes
18 answers

Developing as a programmer

I have been learning C++ for three months now and in that time created a number of applications for my company. I consider myself fairly comfortable with C++ / MFC and STL, however I don't just want to be an OK programmer, I want to be a good…
Konrad
  • 39,751
  • 32
  • 78
  • 114
12
votes
1 answer

Adding a tooltip to CMenu item(s)

A while ago, I have tried to add a tooltip for testing purposes on a CMenu item. Now I would need it, and I'm facing the same issue again. This question and answer(s): MFC : How to add tooltip in Cmenu items? doesn't help me at all, as this "newline…
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
12
votes
1 answer

Cannot open include file 'afxwin.h':no such header fileor directory in vs 2013 c++

So,I am trying to port some old code to newer version of vs so I imported the sln of the project into my vs 2013 and I get this error and some others and I have gone through some of the other questions on this forum.So I understand that this has…
12
votes
2 answers

How to create an imagelist from a PNG?

I've seen here that you can create an image list with transparency. It works... sort of. I used this to create an image list for a list control. The results were a little disappointing: The one on the left is how it should look. The one on the…
Adrian
  • 10,246
  • 4
  • 44
  • 110
12
votes
1 answer

Entry point for MFC application

I have created new emty project. Then have added cpp and header hello world files taken from Jeff Prosise 'Programming Windows with MFC' book. Have set Use of MFC to Use MFC in a Shared DLL Got error entry point must be defined How to fix this…
vico
  • 17,051
  • 45
  • 159
  • 315
12
votes
3 answers

Convert String^ in c# to CString in c++/CLI

I need a help on one question where I stuck while coding my app in MFC. I am using CLR i.e Common Language Runtime in my application to integrate c# APIs. but now I stuck on converting System::String^ to CString. I am not able to do that. I am using…
A B
  • 1,461
  • 2
  • 19
  • 54
12
votes
3 answers

CEdit control maximum length? (in characters it can display)

What is the maximum length for the text string contained in a CEdit control in MFC? I get a beep when trying to add a character after the character 30001 is this documented anywhere? Can I display longer texts in a CEdit? Should I use another…
LLucasAlday
  • 2,349
  • 11
  • 34
  • 41
12
votes
1 answer

Convert HWND to IntPtr (CLI)

I have a HWND in my C++ MFC code, and I want to pass this HWND to a C# control and get it as IntPtr. What Is wrong in my code, and how can I do it correctly? (I think it's something with wrong using of the CLI pointers, because I get an error that…
user1439691
  • 381
  • 1
  • 6
  • 17
12
votes
7 answers

CFileDialog :: Browse folders

When I try to instantiate a CFileDialog object it shows both the folders and files. How do you create a CFileDialog that browses for folders alone?
Owen
  • 4,063
  • 17
  • 58
  • 78
12
votes
4 answers

Linking library without a header file?

I'm attempting to link a static library in C++ using Visual Studio 2010. Trouble is, the library (and accompanying header ) have a lot of MFC objects in them. I would like to call one of the library's functions without recompiling my project to…
eli
  • 311
  • 1
  • 2
  • 10
12
votes
6 answers

Future proofing a large UI Application - MFC with 2008 Feature pack, or C# and Winforms?

My company has developed a long standing product using MFC in Visual C++ as the defacto standard for UI development. Our codebase contains ALOT of legacy/archaic code which must be kept operational. Some of this code is older than me (originally…
Ali Parr
  • 4,737
  • 3
  • 31
  • 35
11
votes
2 answers

C++ CLI System.String^ to MFC LPCTSTR

How would I convert a System (.net) C++\CLI String^ into a MFC C++ LPCTSTR string. It is very easy to get a LPCTSTR into String^, but so far found nothing on doing it the other way around.
Landin Martens
  • 3,283
  • 12
  • 43
  • 61