Visual C++ 6.0 - 派生 CDialog 类中的 OnInitDialog 不起作用

发布于 2024-08-10 13:05:04 字数 3302 浏览 2 评论 0原文

我创建了一个简单的 MFC 应用程序向导对话框项目。我使用类向导基于 CDialog 创建了一个名为 CMyDlg 的新类。然后我转到消息映射屏幕并双击 WM_INITDIALOG 条目以自动创建 CMyDlg::OnInitDialog() 处理程序。

我遇到的问题是 CMyDlg::OnInitDialog() 不会调用。我在那里设置了一个断点,但它根本不会调用。调用父对话框的 OnInitDialog() 方法,但不会调用 CMyDlg::OnInitDialog() 方法。

有什么特别需要做的事情吗?

我已经设法实现了一种解决方法,即从父对话框的 OnInitDialog() 方法发送我自己的消息,并在 CMyDlg 中处理它,但是..我确信这不是这样做的方法..

// MyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DeriveDlgTest.h"
#include "MyDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg( UINT nIDTemplate, CWnd* pParent /*=NULL*/)
    : CDialog(nIDTemplate, pParent)
{
  // PDS: THIS GETS CALLED
}

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CMyDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
}


void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMyDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    //{{AFX_MSG_MAP(CMyDlg)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog() 
{
  // PDS: THIS DOES NOT GET CALLED
    CDialog::OnInitDialog();


    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}


#if !defined(AFX_MYDLG_H__ECC7F6AC_FEB3_419D_AFE2_6B6DE8196D74__INCLUDED_)
#define AFX_MYDLG_H__ECC7F6AC_FEB3_419D_AFE2_6B6DE8196D74__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyDlg.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

class CMyDlg : public CDialog
{
// Construction
public:
    CMyDlg(CWnd* pParent = NULL);   // standard constructor
  CMyDlg( UINT nIDTemplate, CWnd* pParent = NULL);   // standard constructor
// Dialog Data
    //{{AFX_DATA(CMyDlg)
    enum { IDD = IDD_DERIVEDLGTEST_DIALOG };
        // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA


// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:

    // Generated message map functions
    //{{AFX_MSG(CMyDlg)
    virtual BOOL OnInitDialog();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYDLG_H__ECC7F6AC_FEB3_419D_AFE2_6B6DE8196D74__INCLUDED_)

谢谢大家。我已将虚拟项目上传到下面的链接。尝试构建项目,您会发现 CMyDlg::OnInitDialog() 从未被调用。

我按照上面的建议删除了 IDD 枚举和构造函数,但它根本没有任何区别。没有CMyDlg dlg; dlg.DoModal() 作为主对话框本身调用,它派生自 CMyDlg,而不是通常的 CDialog 类。

我仍然没有解决这个问题,因此我们将不胜感激。

干杯

链接文本

I have create a simple MFC appwizard dialog project. I used the Class Wizard to create a new class called CMyDlg based on CDialog. Then I went to the Message Map screen and doubleclicked on the WM_INITDIALOG entry to automatically create a CMyDlg::OnInitDialog() handler.

The problem I have is that CMyDlg::OnInitDialog() will not call. I have put a breakpoint in there and it simply will not call. The parent dialog's OnInitDialog() method gets called, but it will not call the CMyDlg::OnInitDialog() method.

Is there something special than needs to be done?

I have managed to implement a workaround which is to send a message of my own from the parent dialog's OnInitDialog() method and have it handled in CMyDlg but.. I'm sure this is not the way to do it..

// MyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DeriveDlgTest.h"
#include "MyDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg( UINT nIDTemplate, CWnd* pParent /*=NULL*/)
    : CDialog(nIDTemplate, pParent)
{
  // PDS: THIS GETS CALLED
}

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CMyDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
}


void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMyDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    //{{AFX_MSG_MAP(CMyDlg)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog() 
{
  // PDS: THIS DOES NOT GET CALLED
    CDialog::OnInitDialog();


    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}


#if !defined(AFX_MYDLG_H__ECC7F6AC_FEB3_419D_AFE2_6B6DE8196D74__INCLUDED_)
#define AFX_MYDLG_H__ECC7F6AC_FEB3_419D_AFE2_6B6DE8196D74__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyDlg.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

class CMyDlg : public CDialog
{
// Construction
public:
    CMyDlg(CWnd* pParent = NULL);   // standard constructor
  CMyDlg( UINT nIDTemplate, CWnd* pParent = NULL);   // standard constructor
// Dialog Data
    //{{AFX_DATA(CMyDlg)
    enum { IDD = IDD_DERIVEDLGTEST_DIALOG };
        // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA


// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:

    // Generated message map functions
    //{{AFX_MSG(CMyDlg)
    virtual BOOL OnInitDialog();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYDLG_H__ECC7F6AC_FEB3_419D_AFE2_6B6DE8196D74__INCLUDED_)

Thanks Guys. I've uploaded the dummy project to the link below. Try building the project and you will find that CMyDlg::OnInitDialog() is never called.

I removed the IDD enum and constructor as advised above but it didn't make any difference at all. There is not CMyDlg dlg; dlg.DoModal() call as the main dialog itself it derived from CMyDlg as opposed to the usual CDialog class.

I still haven't solved this issue so any help would be appreciated.

Cheers

link text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

靖瑶 2024-08-17 13:05:04

你得出
来自 CMyDlgCDeriveDlgTestDlg 但在 CDeriveDlgTestDlg::OnInitDialog() 内部,您明确指示编译器跳过基类并执行 CDialog::OnInitDialog (),因此永远不会调用 CMyDlg::OnInitDialog()

You derive
CDeriveDlgTestDlg from CMyDlg but inside CDeriveDlgTestDlg::OnInitDialog() you explicitly direct compiler to jump over base class and execute CDialog::OnInitDialog(), so CMyDlg::OnInitDialog() is never called.

风情万种。 2024-08-17 13:05:04

如果您使用的是 MFC 对话框,则不得处理 WM_INITDIALOG 消息。
MFC CDialog 类有一个名为 OnInitDialog() 的虚拟方法,您只需重写该方法即可调用该方法。
您可以从 VS 中的“覆盖”选项卡而不是“窗口消息”选项卡自动创建该方法。

You must not handle the WM_INITDIALOG message if you're using an MFC dialog.
The MFC CDialog class has a virtual method named OnInitDialog() which you must simply override and that one will get called.
You can create that method automatically from the "overrides" tab instead of the "window messages" tab in VS.

白鸥掠海 2024-08-17 13:05:04

如果您使用的是发布版本而不是调试版本,则可能会在设置断点时遇到问题 - 它们可能会设置在错误的行上,或者完全被忽略。仔细检查您是否正在使用调试版本,或者找到其他方法来确定是否已到达代码。我没有发现您的代码有任何明显的错误。

If you're using a Release build rather than Debug, you might have trouble setting breakpoints - they might get set on the wrong line, or ignored entirely. Either double check to see that you're using a Debug build, or find another way to determine that the code is or isn't being reached. I don't see anything obviously wrong with your code.

墨离汐 2024-08-17 13:05:04

如果您想使用 CMyDlg 作为其他对话框类的基础,则不能在 CMyDlg 类中设置 IDD。应在从 CMyDlg 派生的类上设置 IDD。

所以你应该删除这个:

enum { IDD = IDD_DERIVEDLGTEST_DIALOG };

并替换标准构造函数:

// in the .h file:
//CMyDlg(CWnd* pParent = NULL);
CMyDlg(LPCSTR szIDTemplate, CWnd* pParent = NULL );


// in the .cpp file:
CMyDlg::CMyDlg(LPCSTR szIDTemplate,CWnd* pParent /*=NULL*/)
    : CDialog(szIDTemplate, pParent)
{
}

编辑:我刚刚看到了你的链接代码。您在派生类中注意到这一点了吗?

BOOL CDeriveDlgTestDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

您正在调用 CDialog::OnInitDialog(),而不是 CMyDlg::OnInitDialog()

事实上,您应该将 CDeriveDlgTestDlg 中出现的所有提及 CDialog 的内容替换为 CMyDlg。这样做就可以了。

If you want to use CMyDlg as a base for other dialog classes, you cannot have the IDD set in your CMyDlg class. The IDD should be set on the class derived from CMyDlg.

So you should delete this:

enum { IDD = IDD_DERIVEDLGTEST_DIALOG };

and replace the standard constructor:

// in the .h file:
//CMyDlg(CWnd* pParent = NULL);
CMyDlg(LPCSTR szIDTemplate, CWnd* pParent = NULL );


// in the .cpp file:
CMyDlg::CMyDlg(LPCSTR szIDTemplate,CWnd* pParent /*=NULL*/)
    : CDialog(szIDTemplate, pParent)
{
}

Edit: I just saw your link code. Have you noticed this in your derived class?

BOOL CDeriveDlgTestDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

You are calling CDialog::OnInitDialog(), not CMyDlg::OnInitDialog()!

In fact, you should replace all mentions of CDialog thar appear in CDeriveDlgTestDlg with CMyDlg. Do this and you're good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文