user366312
user366312

Reputation: 16884

How can I get MFC support apart from creating MFC application using App Wizard?

I know that when we use App Wizard for creating an MFC application in VC++, the wizard automatically adds the required libs to the project.

I want to create an MFC application manually. How to do that?

Upvotes: 3

Views: 6687

Answers (2)

Snuff Movie
Snuff Movie

Reputation: 392

You can create an MFC app manually, there are a lot of dependencies and fussing around. But it can be fun to do. Here is a small tutorial.

Create the following HelloMFC file:

#include <afxwin.h>

  class HelloApplication : public CWinApp
  {
  public:     
   virtual BOOL InitInstance();
  };

  HelloApplication HelloApp;

  class HelloWindow : public CFrameWnd
  {        
   CButton* m_pHelloButton;
  public:     
   HelloWindow();
  };


  BOOL HelloApplication::InitInstance()
  {        
   m_pMainWnd = new HelloWindow();       
   m_pMainWnd->ShowWindow(m_nCmdShow);      
   m_pMainWnd->UpdateWindow();      
   return TRUE;
  }


  HelloWindow::HelloWindow()
  {        
   Create(NULL,             
    "Hello World!",               
    WS_OVERLAPPEDWINDOW|WS_HSCROLL,                
    CRect(0,0,140,80));        
   m_pHelloButton = new CButton();
   m_pHelloButton->Create("Hello World!",WS_CHILD|WS_VISIBLE,CRect(20,20,120,40),this,1);
  }

To Compile this at the command line, there's are all the libraries that need to be linked. You will notice that there is no WinMain or main in the code above. MFC burried its main in the libraries. It is defined in appmodul.cpp which you can find in your MFC\SRC directory.

Anyway, here is how you can compile the above code:

cl.exe hellomfc.cpp /EHsc /I atlmfc\include /I Includes /I Includes\Winsdk atlmfc\lib\amd64\nafxcw.lib Libs\libcmt.lib Libs\Ke rnel32.Lib Libs\User32.Lib Libs\Gdi32.Lib Libs\MSImg32.Lib Libs\ComDlg32.Lib Lib s\WinSpool.Lib Libs\AdvAPI32.Lib Libs\Shell32.Lib Libs\ComCtl32.Lib Libs\ShLwApi .Lib Libs\Uuid.lib atlmfc\lib\amd64\atls.lib Libs\Ole32.Lib Libs\OleAut32.Lib Li bs\oldnames.lib Libs\WS2_32.Lib Libs\MsWSock.Lib Libs\OleAcc.Lib Libs\comsuppw.l ib Libs\GdiPlus.lib Libs\Imm32.Lib Libs\WinMM.Lib Libs\MsXml2.Lib Libs\OleDlg.L ib Libs\Urlmon.Lib /link/SUBSYSTEM:WINDOWS

Note: the above obviously depends on the specific location of your library files, but those are the required libraries.

Upvotes: 5

LihO
LihO

Reputation: 42085

When you create a new MFC application, you will find this code in the precompiled header:

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

which is how the MFC header files get included. Even if you try to create a new Win32 Console Application and ask the wizard to include MFC support, you will again find these lines in the precompiled header:

#include <afx.h>
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

So if you want to create a console application that just somehow uses the MFC classes, then just create a new empty project, go to its properties and in General change Use of MFC from Use Standard Windows Libraries to Use MFC in a Static Library. Then you just need to include these headers and you are ready to go. ;)

Example:

#include <iostream>
#include <afx.h>
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

int main()
{
    CByteArray a;
    a.Add(7);
    std::cout << (int)a[0];
}

Upvotes: 6

Related Questions