Sign in to follow this  

MFC newb question

This topic is 4039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

um. for console programs there is a main() that can call your classes etc. what is the equivalent of main() in MFC? i'm lost. thanks for any help.

Share this post


Link to post
Share on other sites
You are probably looking for CWinApp::InitInstance, override this in your CWinApp derived class.

But thinking about main() isn't really the way to go with MFC and windows programs in general, they are "event-driven" and the program isn't a sequence of function calls that you can follow from the beginning of main to the end... you basically implement a load of "event handlers" that get called by the MFC framework when something that you are interested in (user presses a key, a control is clicked on or gets focus, etc.) happens.

Finally, are you sure you want to learn MFC? It's big and ugly. If you want to write tools for game coding, you probably want to look into C# or windows forms in managed C++.

Share this post


Link to post
Share on other sites
Yeah, I'm gonna have to second the recommendation to stay away from MFC. It's big, awkward, and ugly, and most of all very old and outdated; and I wouldn't recommend it to anyone who wants to write Windows GUI applications. In six years of using it, I've grown kinda fond of MFC, but only because of the pains it has caused me over the years. [grin]

(For the record, I don't use it anymore now that I've switched over to C# and .NET)

Share this post


Link to post
Share on other sites
Quote:
Original post by Show
doing this for school project =(

Ah, well, if that's the case... [grin]

As Paradigm Shifter said, in MFC apps initialization is usually done in the InitInstance method of your CWinApp-derived class. Typically what you do is create a class that derives from CWinApp, override InitInstance, and in your InitInstance method, you create and display your application's main window.

Then, at file scope in some .cpp file in your project, you declare one instance of your CWinApp-derived class. This object will get constructed as soon as your program starts, and your InitInstance method will be called by the default CWinApp constructor.

Here's a very simple, stripped-down example:

// MyApp.h

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




//MyApp.cpp

#include "stdafx.h"
#include "MyApp.h"

BOOL CMFCTestApp::InitInstance()
{
// Call the base class's InitInstance
CWinApp::InitInstance();

// Create and show the app's main window
CMyAppDialog dlg;
m_pMainWnd = &dlg;
dlg.DoModal();

return FALSE;
}

// Here we declare an instance of CMyApp that will be constructed
// when the program starts. Our InitInstance will be called, and
// our app's main window will be displayed.
CMyApp theApp;




There should be more details on how this works in whatever book/reference material you're using for the class. Hope this helps!

Share this post


Link to post
Share on other sites
Quote:
Original post by Show
um. for console programs there is a main() that can call your classes etc.
what is the equivalent of main() in MFC?
i'm lost.


MFC apps are basically Windows applications that use the MFC library. There is no main(), but there is a WinMain(). WinMain is the main equivalent for Windows programs, as it is the entry point for all Windows programs, including MFC apps. MFC's WinMain cannot be modified (it's in \src\mfc\winmain.cpp file). So the main equivalent is there; it's just hidden from you.

Share this post


Link to post
Share on other sites

This topic is 4039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this