我似乎无法将列标题添加到继承的 MFC 对话框中的列表框。怎么了?

发布于 2024-08-07 09:43:34 字数 6480 浏览 2 评论 0原文

我有一个继承自 CDialog 类的 CStdDlg。在 CStdDlg 对话框中,我有一个列表框 (m_lcList1)、编辑框 (m_ceEdit1)、一个单选按钮 (m_rbButton2) 以及“确定”、“取消”和“按钮 1”按钮。

我正在尝试创建另一个名为 CDerivedDlg 的类,该类继承自 CStdDlg。我想使用 CStdDlg 中的所有内容,但来自 CDerivedDlg 的内容。 这是一个愚蠢的测试应用程序,但我需要一个与真实应用程序完全相同的东西。

我将在下面展示所有代码。问题:每当我尝试向列表框中添加列标题时,问题就会不断出现。 m_hWnd = NULL

谁能告诉我出了什么问题吗?我真的很感激。谢谢。

// CStdDlg.cpp file

#include "stdafx.h"  
#include "testdlg.h"
#include "StdDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
CStdDlg::CStdDlg(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
    : CDialog(CStdDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CStdDlg)
    //}}AFX_DATA_INIT
}
void CStdDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CStdDlg)
    DDX_Control(pDX, IDC_EDIT1, m_ceEdit1);
    DDX_Control(pDX, IDC_RADIO2, m_rbButton2);
    DDX_Control(pDX, IDC_LIST1, m_lcList1);
    //}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CStdDlg, CDialog)
BEGIN_MESSAGE_MAP(CStdDlg, CDialog)
    //{{AFX_MSG_MAP(CStdDlg)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStdDlg message handlers
BOOL CStdDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();

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

//CStdDlg.h file

#if !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StdDlg.h : header file
//
#include <afxwin.h>

/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog

class CStdDlg : public CDialog
{
    DECLARE_DYNAMIC(CStdDlg)
// Construction
public:

    CStdDlg(UINT nIDTemplate,CWnd* pParent = NULL);   // standard constructor
    CString GetTitle() { 
        CString csTTL;
        GetWindowText(csTTL);
        return csTTL;
    }
// Dialog Data
    //{{AFX_DATA(CStdDlg)
    enum { IDD = IDD_STDDLG };
    CEdit   m_ceEdit1;
    CButton m_rbButton2;
    CListCtrl   m_lcList1;
    //}}AFX_DATA


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

// Implementation
protected:
    void ShowMsg() { AfxMessageBox("ShowMsg from StdDlg"); }
    // Generated message map functions
    //{{AFX_MSG(CStdDlg)
    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_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)

===================================================================================
//CDerivedDlg.cpp file

// StdDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testdlg.h"
#include "CDerivedDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog


CDerivedDlg::CDerivedDlg(CWnd* pParent /*=NULL*/)
    : CStdDlg(CDerivedDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDerivedDlg)
    //}}AFX_DATA_INIT
}


void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDerivedDlg)
    //}}AFX_DATA_MAP
}


IMPLEMENT_DYNAMIC(CDerivedDlg, CStdDlg)
BEGIN_MESSAGE_MAP(CDerivedDlg, CStdDlg)
    //{{AFX_MSG_MAP(CDerivedDlg)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg message handlers


//void CDerivedDlg::OnOK() 
//{
//  // TODO: Add extra validation here
//  AfxMessageBox("CDerived Class OK button pressed");
//  CDialog::OnOK();
//}

BOOL CDerivedDlg::OnInitDialog() 
{
    CStdDlg::OnInitDialog();
    SetWindowText("Derived Test Window");
    m_lcList1.InsertColumn(0,"This is a test");    *******THIS IS WHERE IT CRASHES*****


    m_rbButton2.ShowWindow(SW_HIDE);
    m_ceEdit1.ShowWindow(SW_HIDE);



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


void CDerivedDlg::OnButton1() 
{
//  // TODO: Add extra validation here
    AfxMessageBox("CDerived Button1 pressed");
    AfxMessageBox(GetTitle());

}

void CDerivedDlg::OnOK() 
{
    AfxMessageBox("CDerived Class OK button pressed");

}
===================================================================================
// CDerivedDlg.h file

#if !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CDerivedDlg.h : header file
//
#include "StdDlg.h"

/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog

class CDerivedDlg : public CStdDlg
{
    DECLARE_DYNAMIC(CDerivedDlg)
// Construction
public:
    CDerivedDlg(CWnd* pParent = NULL);   // standard constructor
    virtual void SetTTL(CString csTitle) { this->SetWindowText(csTitle); }

// Dialog Data
    //{{AFX_DATA(CDerivedDlg)
    enum { IDD = IDD_STDDLG };
    //}}AFX_DATA


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

// Implementation
protected:

    // Generated message map functions
    //{{AFX_MSG(CDerivedDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnButton1();
    virtual void OnOK();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

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

#endif // !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================

I have a CStdDlg thats inherits from a CDialog class. In the CStdDlg dialog, I have a list box (m_lcList1), edit box (m_ceEdit1), a radio button(m_rbButton2) and buttons OK, Cancel and Button1.

I am trying to create another class named CDerivedDlg that inherits from CStdDlg. I want to use everything in CStdDlg but from the CDerivedDlg.
This is a silly test application here, but I need something exactly like this is a real application.

I'll show all the code below. PROBLEM: The problem keeps bombing whenever I try to add a column header to the list box. m_hWnd = NULL

Can anyone tell me what's wrong? I would truly appreciate it. Thank you.

// CStdDlg.cpp file

#include "stdafx.h"  
#include "testdlg.h"
#include "StdDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
CStdDlg::CStdDlg(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
    : CDialog(CStdDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CStdDlg)
    //}}AFX_DATA_INIT
}
void CStdDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CStdDlg)
    DDX_Control(pDX, IDC_EDIT1, m_ceEdit1);
    DDX_Control(pDX, IDC_RADIO2, m_rbButton2);
    DDX_Control(pDX, IDC_LIST1, m_lcList1);
    //}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CStdDlg, CDialog)
BEGIN_MESSAGE_MAP(CStdDlg, CDialog)
    //{{AFX_MSG_MAP(CStdDlg)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStdDlg message handlers
BOOL CStdDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();

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

//CStdDlg.h file

#if !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StdDlg.h : header file
//
#include <afxwin.h>

/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog

class CStdDlg : public CDialog
{
    DECLARE_DYNAMIC(CStdDlg)
// Construction
public:

    CStdDlg(UINT nIDTemplate,CWnd* pParent = NULL);   // standard constructor
    CString GetTitle() { 
        CString csTTL;
        GetWindowText(csTTL);
        return csTTL;
    }
// Dialog Data
    //{{AFX_DATA(CStdDlg)
    enum { IDD = IDD_STDDLG };
    CEdit   m_ceEdit1;
    CButton m_rbButton2;
    CListCtrl   m_lcList1;
    //}}AFX_DATA


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

// Implementation
protected:
    void ShowMsg() { AfxMessageBox("ShowMsg from StdDlg"); }
    // Generated message map functions
    //{{AFX_MSG(CStdDlg)
    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_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)

===================================================================================
//CDerivedDlg.cpp file

// StdDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testdlg.h"
#include "CDerivedDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog


CDerivedDlg::CDerivedDlg(CWnd* pParent /*=NULL*/)
    : CStdDlg(CDerivedDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDerivedDlg)
    //}}AFX_DATA_INIT
}


void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDerivedDlg)
    //}}AFX_DATA_MAP
}


IMPLEMENT_DYNAMIC(CDerivedDlg, CStdDlg)
BEGIN_MESSAGE_MAP(CDerivedDlg, CStdDlg)
    //{{AFX_MSG_MAP(CDerivedDlg)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg message handlers


//void CDerivedDlg::OnOK() 
//{
//  // TODO: Add extra validation here
//  AfxMessageBox("CDerived Class OK button pressed");
//  CDialog::OnOK();
//}

BOOL CDerivedDlg::OnInitDialog() 
{
    CStdDlg::OnInitDialog();
    SetWindowText("Derived Test Window");
    m_lcList1.InsertColumn(0,"This is a test");    *******THIS IS WHERE IT CRASHES*****


    m_rbButton2.ShowWindow(SW_HIDE);
    m_ceEdit1.ShowWindow(SW_HIDE);



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


void CDerivedDlg::OnButton1() 
{
//  // TODO: Add extra validation here
    AfxMessageBox("CDerived Button1 pressed");
    AfxMessageBox(GetTitle());

}

void CDerivedDlg::OnOK() 
{
    AfxMessageBox("CDerived Class OK button pressed");

}
===================================================================================
// CDerivedDlg.h file

#if !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CDerivedDlg.h : header file
//
#include "StdDlg.h"

/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog

class CDerivedDlg : public CStdDlg
{
    DECLARE_DYNAMIC(CDerivedDlg)
// Construction
public:
    CDerivedDlg(CWnd* pParent = NULL);   // standard constructor
    virtual void SetTTL(CString csTitle) { this->SetWindowText(csTitle); }

// Dialog Data
    //{{AFX_DATA(CDerivedDlg)
    enum { IDD = IDD_STDDLG };
    //}}AFX_DATA


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

// Implementation
protected:

    // Generated message map functions
    //{{AFX_MSG(CDerivedDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnButton1();
    virtual void OnOK();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

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

#endif // !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================

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

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

发布评论

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

评论(2

情愿 2024-08-14 09:43:35

我猜测,由于 DDX_Control() 位于基类上,因此派生类不会将资源控件链接到其各自的类。您可能想尝试将其更改

void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);

为:

void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
    CStdDlg::DoDataExchange(pDX);

I'm guessing that since the DDX_Control() are on the base class, the derived class will not link the resource controls to their respective classes. You might want to try to change this:

void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);

into this:

void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
    CStdDlg::DoDataExchange(pDX);
云之铃。 2024-08-14 09:43:35
m_lcList1.InsertColumn(0,"This is a test");    *******THIS IS WHERE IT CRASHES*****

您必须先创建该控件,然后才能使用它。
请参阅 CListCtrl::Create 和/或CListCtrl::CreateEx 方法。
您必须选择 LVS_REPORT 样式,因为您想要使用列表作为报告视图控件。

m_lcList1.InsertColumn(0,"This is a test");    *******THIS IS WHERE IT CRASHES*****

You have to create the control before you'll be able to use it.
See CListCtrl::Create and/or CListCtrl::CreateEx methods.
You must choose the LVS_REPORT style since you want to use the list as a report view control.

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