简体   繁体   English

MFC程序中的对话框

[英]dialogbox in a MFC program

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. 我已经在Visual c ++中使用MFC编写了以下应用程序,该应用程序包括两个资源(一个菜单和一个对话框)(使用资源编辑器创建)...该程序运行正常,但只显示一个资源。 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;

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. 使用菜单IDR_MENU1资源创建窗口的IDR_MENU1 This means that the main window of your app is the menu. 这意味着您的应用程序的主窗口是菜单。

Also, the whole code does not look MFC-ish at all. 而且,整个代码看起来根本不像MFC。 I would suggest creating an MFC app from Visual Studio template - it will set up the main window properly for you. 我建议从Visual Studio模板创建MFC应用程序-它会为您正确设置主窗口。

The dialog will only be displayed when the command with id 101 is executed. 该对话框仅在执行ID为101的命令时显示。 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. ID_HELP_ABOUT定义为值101,然后在选择该菜单项时显示对话框,将调用about函数。

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. 我不确定您要在此处实现的目标到底是什么,是否会说出从MFC向导生成的代码并从那里获取代码,将在这里与其他建议相呼应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM