Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a simple main() with an MFC app

Status
Not open for further replies.

BobbyB

Programmer
Feb 19, 2002
44
CA
Is there a way that I used a simple main statement like this and then call my mfc app or a dlgbox from it?

void main(){
... // some code

myDlgBox d;
d.DoModal();

... // some code
}

class myApp : public CWinApp{
... // some code
}

class myDlgBox : public CDialog{
... // some code
}

The main problem come from the fact that an MFC application needs
myApp theApp;
to start... but this immediatly start the dlg... and if I don't put this line... the program crash before executing any line of the main.

thanks...
 
A simple way to do this is to go to "File" choose "New\ Projects\Wi32 Console Application" now from the dialog box that appear on the screen choose "An application that supports MFC".Now if you try to write your code,it should work.

Here is a little example:

// HelloWorldMFC.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "HelloWorldMFC.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T(&quot;Fatal Error: MFC initialization failed&quot;) << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
MessageBox(NULL,(LPCTSTR)strHello ,&quot;MFC&quot;,MB_OK);
}

return nRetCode;
}
 
You can also create a bare-bones dialog application if you click on Files->New->Projects->MFC Wizard (not usre if wiard is MFC or application wizard). Then choose the &quot;dialog box&quot; radio box from the first page of wizard as the type of apllication you want to build.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top