views:

249

answers:

3

i have written the following application using MFC in visual c++ that includes two resources (a menu and a dialogbox) (created using the resource editor)...the program works absolutely fine except that it displays only one resource ie. it displays only the menu but it does not display the dialogbox... what to do?? this is the code...

#include<afxwin.h>
#include"stdafx.h"
#include"resource.h"

class mydialog:public CDialog
{
private:
    int id;

public:
    mydialog(int n):CDialog(n)
    {
     id=n;
    }

    int OnInitDialog()
    {
     CDialog::OnInitDialog();
     if(id==IDD_DIALOG1)
      CenterWindow(GetDesktopWindow());
     else
      CenterWindow();
     return TRUE;
    }

    void OnOK()
    {
     CDialog::OnOK() ;
     MessageBox(TEXT("You have Pressed the OK Button"),TEXT("OnOK handler"));
    }
};
class myframe:public CFrameWnd
{
public:
    myframe()
    {
     Create(0,TEXT("Simple Dialog Box"),WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
    }
    void about()
    {
     mydialog d(IDD_DIALOG1);
     d.DoModal();
    }
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND(101,about)
END_MESSAGE_MAP()

class myapp:public CWinApp
{
public:
    int InitInstance()
    {
     myframe *p;
     p=new myframe;
     p->ShowWindow(3);
     m_pMainWnd=p;
     return 1;
    }
};
myapp a;
A: 

Hey, without compiling the code and running it I can see a problem here:

myframe()
{
    Create(0,TEXT("Simple Dialog Box"),WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}

Where you are creating a window using IDR_MENU1 resource which is a menu. This means that the main window of your app is the menu.

Also, the whole code does not look MFC-ish at all. I would suggest creating an MFC app from Visual Studio template - it will set up the main window properly for you.

Igor Zevaka
No, the IDR_MENU1 parameter here is telling the window to use that resource as the menu for the window, so that is not a problem.
Steve Beedie
Ah, of course, you re right.
Igor Zevaka
A: 

The dialog will only be displayed when the command with id 101 is executed. Presumably this would be a menu item which is associated with the main window. If your menu is defined as:

IDR_MENU1 MENU 
BEGIN
    POPUP "HELP"
    BEGIN
        MENUITEM "About",  ID_HELP_ABOUT
    END
END

And ID_HELP_ABOUT is defined with the value 101, then your about function will get called when you select that menu item, showing the dialog.

I'm not sure exactly what you are trying to achieve here, and would echo the other suggestions here by saying to start out with the MFC wizard generated code and take it from there.

Steve Beedie
A: 

include

include"resource.h"

class myframe:public CFrameWnd

{

public:

myframe()

{
 Create(0,HELLO MFC",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}

};

class myapp:public CWinApp

{

public:

int InitInstance()

{
 myframe *p;

 p=new myframe;

 p->ShowWindow(1);

 m_pMainWnd=p;

 return 1;
}

};

myappa;

dibakar
i can't run it plz healp me
dibakar
and how i run this program
dibakar