创建一个类CMFCPropertyGridDateTimeProperty,继承自CMFCPropertyGridProperty
/.h
// CMFCPropertyGridDateTimeProperty object
//QQ:20794027
//Name:zhaogaojian
![](https://i-blog.csdnimg.cn/blog_migrate/4ccc1d7c802f106c25ab5915a51c98e1.jpeg)
class CMFCPropertyGridDateTimeProperty : public CMFCPropertyGridProperty
{
DECLARE_DYNAMIC(CMFCPropertyGridDateTimeProperty)
// Construction
public:
CMFCPropertyGridDateTimeProperty(const CString& strName, const CString& dtDateTime, DWORD_PTR dwData = 0, LPCTSTR lpszDescr = NULL,BOOL isDate=TRUE);
virtual ~CMFCPropertyGridDateTimeProperty();
// Overrides
public:
virtual void OnClickButton(CPoint point);
// Attributes
protected:
BOOL m_bIsDate;
CString m_strDateTime;
};
// MyPropertyGridProperty.cpp : 实现文件
//
#include "stdafx.h"
#include "LCMeterSetCOM.h"
#include "MyPropertyGridProperty.h"
#include "MyDateDialog.h"
/.cpp
// CMFCPropertyGridDateTimeProperty object
#define AFX_PROP_HAS_LIST 0x0001
#define AFX_PROP_HAS_BUTTON 0x0002
#define AFX_PROP_HAS_SPIN 0x0004
IMPLEMENT_DYNAMIC(CMFCPropertyGridDateTimeProperty, CMFCPropertyGridProperty)
CMFCPropertyGridDateTimeProperty::CMFCPropertyGridDateTimeProperty(const CString& strName, const CString& strDateTime, DWORD_PTR dwData, LPCTSTR lpszDescr,BOOL isDate) : CMFCPropertyGridProperty(strName, COleVariant(strDateTime), lpszDescr, dwData)
{
m_dwFlags = AFX_PROP_HAS_BUTTON;
AllowEdit(false);
m_bIsDate=isDate;
}
CMFCPropertyGridDateTimeProperty::~CMFCPropertyGridDateTimeProperty()
{
}
void CMFCPropertyGridDateTimeProperty::OnClickButton(CPoint /*point*/)
{
ASSERT_VALID(this);
ASSERT_VALID(m_pWndList);
ASSERT_VALID(m_pWndInPlace);
ASSERT(::IsWindow(m_pWndInPlace->GetSafeHwnd()));
m_bButtonIsDown = TRUE;
Redraw();
//CString
CString strDate;
COleDateTime m_dtDateTime;
m_dtDateTime = m_varValue.date;
BOOL bUpdate = FALSE;
CDateTimeCtrl m_dtCtrl;
CMyDateDialog dlg;
if (dlg.DoModal() == IDOK)
{
bUpdate = TRUE;
if(m_bIsDate)
strDate.Format("%04d-%02d-%02d",dlg.m_dtDatePicker.GetYear(),dlg.m_dtDatePicker.GetMonth(),dlg.m_dtDatePicker.GetDay());
else
strDate.Format("%02d:%02d:%02d",dlg.m_dtTimePicker.GetHour(),dlg.m_dtTimePicker.GetMinute(),dlg.m_dtTimePicker.GetSecond());
//strPath = dlg.GetPathName();
}
if (bUpdate)
{
if (m_pWndInPlace != NULL)
{
m_pWndInPlace->SetWindowText(strDate);
}
m_varValue = (LPCTSTR) strDate;
}
m_bButtonIsDown = FALSE;
Redraw();
if (m_pWndInPlace != NULL)
{
m_pWndInPlace->SetFocus();
}
else
{
m_pWndList->SetFocus();
}
}
CMyDateDialog 是一个继承自dialog的类,里边有两个日期控件,变量名分别为m_dtDatePicker,m_dtTimePicker
使用方法:
...
apGroup1->AddSubItem(new CMFCPropertyGridDateTimeProperty(_T("日期"), strDate, 71 ,"请选择日期",TRUE));
...
LRESULT CPropertiesViewBar::OnPropertyChanged (WPARAM wParam,LPARAM lParam)
{
CMFCPropertyGridProperty* pProp = (CMFCPropertyGridProperty*) lParam;
BOOL bResetMDIChild = FALSE;
CString str;
int iYear=0,iMonth=0,iDay=0,iHour=0,iMinute=0,iSecond=0;
int iData=(int) pProp->GetData ();
switch (iData)
{
当更新数据时,再将字符串转成COleDateTime;