使用VS6我注意到在CDateTimeCtrl对象中更改日期时,我的某些对话框会崩溃。调查结果显示,对DDX_DateTimeCtrl的调用将12个字节的数据移动到4个字节宽的CTime对象上。谁能解释为什么会发生这种情况?有谁知道如何纠正它?
部首:
#if !defined(AFX_DATEDIALOG_H__B9373304_D774_4447_A4D1_BB4E1FB3404F__INCLUDED_)
#define AFX_DATEDIALOG_H__B9373304_D774_4447_A4D1_BB4E1FB3404F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// DateDialog.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CDateDialog dialog
class CDateDialog : public CDialog
{
// Construction
public:
CDateDialog(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CDateDialog)
enum { IDD = IDD_DIALOG_DATE };
CDateTimeCtrl m_StartDate;
CDateTimeCtrl m_EndDate;
CTime m_tmEndDate;
CTime m_tmStartDate;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDateDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CDateDialog)
afx_msg void OnButtonBack();
afx_msg void OnDatetimechangeEndDate(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnDatetimechangeStartDate(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnCloseupStartDate(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnCloseupEndDate(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
// the range limits for the dates; these are set to the values found
// in m_tmStartDate and m_tmEndDate in OnInitDialog
CTime m_tmStart; // beginning of the range
CTime m_tmEnd; // end of the range
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_DATEDIALOG_H__B9373304_D774_4447_A4D1_BB4E1FB3404F__INCLUDED_)
Code:
// DateDialog.cpp : implementation file
//
#include "stdafx.h"
#include "CustomerSales.h"
#include "DateDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDateDialog dialog
CDateDialog::CDateDialog(CWnd* pParent /*=NULL*/)
: CDialog(CDateDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CDateDialog)
m_tmEndDate = 0;
m_tmStartDate = 0;
//}}AFX_DATA_INIT
}
void CDateDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDateDialog)
DDX_Control(pDX, IDC_START_DATE, m_StartDate);
DDX_Control(pDX, IDC_END_DATE, m_EndDate);
DDX_DateTimeCtrl(pDX, IDC_END_DATE, m_tmEndDate);
DDX_DateTimeCtrl(pDX, IDC_START_DATE, m_tmStartDate);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDateDialog, CDialog)
//{{AFX_MSG_MAP(CDateDialog)
ON_BN_CLICKED(IDC_BUTTON_BACK, OnButtonBack)
ON_NOTIFY(DTN_DATETIMECHANGE, IDC_END_DATE, OnDatetimechangeEndDate)
ON_NOTIFY(DTN_DATETIMECHANGE, IDC_START_DATE, OnDatetimechangeStartDate)
ON_NOTIFY(DTN_CLOSEUP, IDC_START_DATE, OnCloseupStartDate)
ON_NOTIFY(DTN_CLOSEUP, IDC_END_DATE, OnCloseupEndDate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDateDialog message handlers
void CDateDialog::OnButtonBack()
{
// exit with our buton ID
EndDialog(IDC_BUTTON_BACK);
}
void CDateDialog::OnDatetimechangeEndDate(NMHDR* pNMHDR, LRESULT* pResult)
{
if(m_EndDate.GetMonthCalCtrl() == NULL)
{
UpdateData();
if(m_tmStartDate > m_tmEndDate)
m_tmStartDate = m_tmEndDate;
UpdateData(FALSE);
}
*pResult = 0;
}
void CDateDialog::OnDatetimechangeStartDate(NMHDR* pNMHDR, LRESULT* pResult)
{
if(m_StartDate.GetMonthCalCtrl() == NULL)
{
UpdateData();
if(m_tmEndDate < m_tmStartDate)
m_tmEndDate = m_tmStartDate;
UpdateData(FALSE);
}
*pResult = 0;
}
void CDateDialog::OnCloseupStartDate(NMHDR* pNMHDR, LRESULT* pResult)
{
UpdateData();
if(m_tmEndDate < m_tmStartDate)
m_tmEndDate = m_tmStartDate;
UpdateData(FALSE);
*pResult = 0;
}
void CDateDialog::OnCloseupEndDate(NMHDR* pNMHDR, LRESULT* pResult)
{
UpdateData();
if(m_tmStartDate > m_tmEndDate)
m_tmStartDate = m_tmEndDate;
UpdateData(FALSE);
*pResult = 0;
}
答案 0 :(得分:0)
很久以前我已经观察过CDateTimeCtrl的错误。如果与CTime变量一起使用,该类的GetTime方法将崩溃。我记得当时我在互联网上找到了与此问题相关的参考资料。我现在找不到它。建议的解决方案是使用COleDateTime。
似乎DDE / DDX交换存在类似的问题,并且最新的MFC中尚未修复它。
当时对我来说最简单的解决方法是使用COleDateTime变量。
DDX_DateTimeCtrl适用于CTime和COleDateTime,因此您的解决方案是使用COleDateTime作为m_tmEndDate
和m_tmStartDate
变量。