Visual c++ 子类DlgItem()调试断言失败

Visual c++ 子类DlgItem()调试断言失败,visual-c++,mfc,Visual C++,Mfc,我已经创建了一个带有自定义控件的dailog框。我在应用程序中使用的是ultimate grid。() 我在运行项目时出错(调试断言失败) BOOL CCustomControlDlg::OnInitDialog() { CDialog::OnInitDialog(); // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABO

我已经创建了一个带有自定义控件的dailog框。我在应用程序中使用的是ultimate grid。()

我在运行项目时出错(调试断言失败)

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

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here
    // Add "About..." menu item to system menu.
    m_grid.AttachGrid(this, IDC_CUSTOM1);//    ERROR LINE

    return TRUE;  // return TRUE  unless you set the focus to a control
}

//Attach grid implementation
BOOL CUGCtrl::AttachGrid(CWnd * wnd,UINT ID){

    if( SubclassDlgItem(IDC_CUSTOM1,wnd))  // ERROR LINE
    {
        long style = GetWindowLong(m_hWnd,GWL_STYLE);
        style = style|WS_CLIPCHILDREN|WS_TABSTOP;
        SetWindowLong(m_hWnd,GWL_STYLE,style);

        // if the parent window is specified
        if(wnd!= NULL)
        {
            LOGFONT logFont;
            CFont *pTempFont = wnd->GetFont();
            pTempFont->GetLogFont( &logFont );

            // ceate a font object based on the font information retrieved from
            // parent window.  This font will be used as grid's default font.
            int nIndex = AddFont( logFont.lfHeight, logFont.lfWidth, logFont.lfEscapement,
                     logFont.lfOrientation, logFont.lfWeight, logFont.lfItalic,
                     logFont.lfUnderline, logFont.lfStrikeOut, logFont.lfCharSet,
                     logFont.lfOutPrecision, logFont.lfClipPrecision, 
                     logFont.lfQuality, logFont.lfPitchAndFamily, logFont.lfFaceName );

            SetDefFont( nIndex );

            // create a font that will be used for the heading cells.  This object
            // is almost identical to the grid's default font, except its weight
            // was increased by 200.
            nIndex = AddFont( logFont.lfHeight, logFont.lfWidth, logFont.lfEscapement,
                     logFont.lfOrientation, logFont.lfWeight + 200, logFont.lfItalic,
                     logFont.lfUnderline, logFont.lfStrikeOut, logFont.lfCharSet,
                     logFont.lfOutPrecision, logFont.lfClipPrecision, 
                     logFont.lfQuality, logFont.lfPitchAndFamily, logFont.lfFaceName );

            CUGCell cell;
            GetHeadingDefault( &cell );
            cell.SetFont( GetFont( nIndex ) );
            SetHeadingDefault( &cell );
        }

        CreateChildWindows();
        // When WS_EX_RTLREADING style was specified for the place holder
        // window, then set the grid to be in RTL layout mode.
        style = GetWindowLong( m_hWnd, GWL_EXSTYLE );
        if ( style&WS_EX_RTLREADING )
            SetGridLayout( 1 );

        OnSetup();
        OnSheetSetup(0);

        // Allow drawing after the grid is initialized
        m_GI->m_paintMode = TRUE;
        // Adjust the grid's components to fit current setup
        AdjustComponentSizes();

        return TRUE;
    }

    return FALSE;
}

问题是,你将两个不同的C++对象附加到同一个控件(<代码> IDCuCuth1 < /COD>)。
DoDataExchange()
中的条目通过调用
DDX\u控件(pDX、IDC\u CUSTOM1、m\u drawpad)隐式执行子类化
,而
OnInitDialog()
中的代码更为明确(
m\u grid.AttachGrid(这个,IDC\u CUSTOM1);
),但本质上做了相同的事情

要解决此问题,您有许多选项,具体取决于您的目标:

  • 如果不需要连接到
    IDC\U CUSTOM1
    m\u drawpad
    对象,请删除
    DDX\U控件()
    调用
    DoDataExchange()
  • 如果不需要将
    m\u grid
    对象附加到
    IDC\u CUSTOM1
    ,请删除
    m\u grid.AttachGrid()
    内部调用
    OnInitDialog()
  • 在对话框资源中添加一个额外的控件占位符,并对其中任何一个对象使用该占位符,以防两者都需要

“按重试以调试应用程序”-这将使您直接进入失败的调试断言,包括尝试验证的表达式。在这种情况下,可能是因为您将多个C++类附加到同一个控件(或多次附加它)。您是否已经将另一个C++类附加到ID为代码>控件IDCuCuth1?我们可以看到您的
DoDataExchange()
实现和对话框构造函数吗?类CAboutDlg:public CDialog{public:CAboutDlg();//对话框数据35; ifdef AFX_DESIGN_TIME enum{IDD=IDD\u ABOUTBOX};#endif protected:virtual void DoDataExchange(CDATA Exchange*pDX);//DDX/DDV支持//实现受保护:声明_MESSAGE_MAP()};void CAboutDlg::DoDataExchange(CDataExchange*pDX){cddialog::DoDataExchange(pDX);}void ccustomcontrolldlg::DoDataExchange(CDataExchange*pDX){cddialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(ccustomcontrolldlg)//注意:ClassWizard将在这里添加DDX和DDV调用DDX_控制(pDX,IDC_自定义1,m绘图板);//}AFX_数据_映射},并添加请求的代码。你是对的。DoDataExchange()中存在问题。
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

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