我使用MFC AppWizard創建了一個SDI應用‘test’,未勾選ActiveX控件,其它為默認設置。然後為CTestView類添加成員變量CString m_strName,創建對話框類CTestDialog,並使用向導為其添加成員變量CTime m_dataTime(自動添加DDX函數)。
之後,在CTestView::OnDraw(CDC* pDC)中創建CTestDialog對象dlg,使用DoModal生成窗口後,對m_strName重新賦值。編譯通過,但運行出錯。
在Debug的時候發現,調用了DoModal之後, 對話框出現之後,點擊OK按鈕後,原本保存m_strName地址的那部分內存(4字節)內容會被修改,導致找不到m_strName的值(新地址越界了,原地址指向的內容沒變)。
而在我單獨使用EditBox控件的時候則不會出現這樣的問題,所以我想知道這中間發生了什麼,有什麼區別以及該如何正確使用DataTimePicker控件。
// testView.h : interface of the CTestView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_TESTVIEW_H__85D7C03F_39A6_4535_80A5_5E109333D12B__INCLUDED_)
#define AFX_TESTVIEW_H__85D7C03F_39A6_4535_80A5_5E109333D12B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CTestView : public CView
{
protected: // create from serialization only
CTestView();
DECLARE_DYNCREATE(CTestView)
// Attributes
public:
CTestDoc* GetDocument();
CString m_strName;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CTestView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CTestView)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in testView.cpp
inline CTestDoc* CTestView::GetDocument()
{ return (CTestDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_TESTVIEW_H__85D7C03F_39A6_4535_80A5_5E109333D12B__INCLUDED_)
// testView.cpp : implementation of the CTestView class
//
#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "testView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction
CTestView::CTestView()
{
// TODO: add construction code here
m_strName="0000";
}
CTestView::~CTestView()
{
}
BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTestView drawing
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CTestDialog dlg;
dlg.DoModal();
m_strName="11111";
}
/////////////////////////////////////////////////////////////////////////////
// CTestView printing
BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid();
}
void CTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers
#if !defined(AFX_DIALOGS_H__32ACF32B_B56E_4CFB_A5AE_E6F2F343DF92__INCLUDED_)
#define AFX_DIALOGS_H__32ACF32B_B56E_4CFB_A5AE_E6F2F343DF92__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Dialogs.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CTestDialog dialog
class CTestDialog : public CDialog
{
// Construction
public:
CTestDialog(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CTestDialog)
enum { IDD = IDD_DIALOG1 };
CTime m_dateTime;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CTestDialog)
// NOTE: the ClassWizard will add member functions here
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_DIALOGS_H__32ACF32B_B56E_4CFB_A5AE_E6F2F343DF92__INCLUDED_)
// Dialogs.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "Dialogs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestDialog dialog
CTestDialog::CTestDialog(CWnd* pParent /*=NULL*/)
: CDialog(CTestDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDialog)
m_dateTime = 0;
//}}AFX_DATA_INIT
}
void CTestDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDialog)
DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_dateTime);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDialog, CDialog)
//{{AFX_MSG_MAP(CTestDialog)
// NOTE: the ClassWizard will add message map macros here
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDialog message handlers
按照你說的寫了下, 沒問題啊, 上傳個按照你說明的寫的demo你試試
把圖片下載下來改成zip格式就可以了