locked
MFC version of Hello World!

    Question

  • I have a C version of Hello World! that leverages the Message Box. I was wondering is there anything as brief over in the MFC world?

    I found one on Google that went to the extreme of its own dialog. Is there nothing simpler?

     


    Vote if answered or helpful, I am running for Office (joke)! IT/Developer, Windows/Linux/Mainframe I also am a true vegan and I am very good with economics and I used to play chess at 2400++ I have lots of papers on my site for power supplies and video card problems, see the resources section
    Monday, September 27, 2010 6:41 PM

All replies

  • Could you be so kind and point us to a dialog sample you have mentioned?
    Anyway in MFC you need an application class and at least one window.
    Download my sample from http://www.mediafire.com/?7clb5uue7c45xdx
    I am not sure what criteria you use to classify code as simple, but this is the simplest MFC app resembling Hello World Win32 sample.

    JohnCz - MVP VC++
    Monday, September 27, 2010 8:07 PM
  • If you start with a wizard generated MFC app skeleton, delete all the wizard code in InitInstance and replace it with a call to AfxMessageBox.  You are done.
    Monday, September 27, 2010 8:25 PM
  •   #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);
      }
    
    


    Vote if answered or helpful, I am running for Office (joke)! IT/Developer, Windows/Linux/Mainframe I also am a true vegan and I am very good with economics and I used to play chess at 2400++ I have lots of papers on my site for power supplies and video card problems, see the resources section
    Monday, September 27, 2010 8:44 PM
  • VF: Is your latest post intended as a question or an answer? 

     

    Tuesday, September 28, 2010 12:51 AM
  • I posted that code for JohnCz who wanted to see the program.

     


    Vote if answered or helpful, I am running for Office (joke)! IT/Developer, Windows/Linux/Mainframe I also am a true vegan and I am very good with economics and I used to play chess at 2400++ I have lots of papers on my site for power supplies and video card problems, see the resources section
    Tuesday, September 28, 2010 12:55 AM
  • This application does not use dialog. It simply creates button in a frame window. It does not show much besides just that.
    It is indeed simple but is showing how to write a bad code (C++ and MFC):

    1. Calling functions that can fail in a constructor is a very bad idea. Constructor does not have any return value. It cannot fail. If frame or button window creation fails this application is not going to recover and it will run without even being noticed by user, since window is never going to show.
    2. It introduces memory leaks since button object is allocated on the heap and never released. I have seen this approach in many samples. Button should be declared as a member variable of the frame class and hp allocation is not needed at all. For an object allocated on the stack frame memory is released automatically. Fortunately, frame window is designed to clean after itself.

    This sample does not demonstrate how MFC implements a message handling; that should be shown to see a difference between Win32 API and MFC application. Why creating a button that does not do anything?
    I am not sure if you had a chance to download my sample; it shows at least how to write text in a WM_PAINT handler.
    With little improvements, this code would at least show a proper way of writing more robust code with some error checking.

    #include <afxwin.h>
    
    // main window declaration
    class HelloWindow : public CFrameWnd
    {    
    	CButton m_HelloButton;
    public:   
    	HelloWindow();
    	virtual OnCreate(LPCREATESTRUCT lpCreateStruct);
    };
    
    class HelloApplication : public CWinApp
    {
    public:   
    	virtual BOOL InitInstance();
    };
    
    HelloApplication HelloApp;
    
    // app:
    BOOL HelloApplication::InitInstance()
    {  
    	HelloWindow* pWnd = new HelloWindow();
    	ASSERT(pWnd);
    	if(NULL == pWnd)
    	{
    		return FALSE; // terminate app hete, Use TRACE to trace an error
    	}
    	
    	BOOL bCreared = pWnd->Create(NULL,       
    		"Hello World!",        
    		WS_OVERLAPPEDWINDOW/*|WS_HSCROLL*/, // scrolling? not needed        
    		CRect(100, 100, 300, 200));    
    	
    	if(!bCreared)
    	{
    		return FALSE; // terminate app hete, Use TRACE to trace an error
    	}
    
    	m_pMainWnd = pWnd;
    	
    	m_pMainWnd->ShowWindow(m_nCmdShow);   
    	m_pMainWnd->UpdateWindow();   
    	return TRUE;
    }
    
    // main window
    
    HelloWindow::HelloWindow()
    {    
    }
    
    HelloWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    	{
    		return -1;
    	}
    
    	BOOL bCreated = m_HelloButton.Create("Hello World!", WS_CHILD | WS_VISIBLE, CRect(20,20,120,40), this, 1);
    
    	if(!bCreated)
    	{
    		return -1;
    	}
    
    	return 0;
    }
    
    

    JohnCz - MVP VC++
    Tuesday, September 28, 2010 3:07 AM
  • Create an empty project in VS (check "Empty project" box somewhere ;-) ). Add following file to it:

    #define WINVER 0x500
    #include <afxwin.h>
    
    class CHelloWorldApp : public CWinApp
    {
     BOOL InitInstance()
     {
      AfxMessageBox(_T("Hello World"));
      return FALSE;
     }
    };
    
    CHelloWorldApp theApp;
    
    
    In Project->General, specify "Use of MFC".

    That short enough? ;-)

    Goran.


    • Proposed as answer by tnvbalaji Thursday, September 30, 2010 8:59 AM
    Wednesday, September 29, 2010 11:44 AM