Make a new class as below
Hope it helps you
.h
#pragma once
void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, COleDateTime& value);
// CDateTimeEx
class CDateTimeEx : public CDateTimeCtrl
{
DECLARE_DYNAMIC(CDateTimeEx)
public:
CDateTimeEx();
virtual ~CDateTimeEx();
protected:
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnPopupDelete();
afx_msg void OnDtnDatetimechange(NMHDR *pNMHDR, LRESULT *pResult);
DECLARE_MESSAGE_MAP()
private:
void ShowMenu(void);
public:
unsigned int m_nIdCtrl;
CWnd* m_pWnd;
};
.cpp
// DateTimeEx.cpp : implementation file
//
#include "stdafx.h"
#include "DateTimeEx.h"
void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, COleDateTime& value)
{
CDateTimeEx dt;
CString strTemp;
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
CDateTimeCtrl* pWnd = (CDateTimeCtrl*) CWnd::FromHandle(hWndCtrl);
ENSURE(pWnd);
if (pDX->m_bSaveAndValidate)
{
int nLen = ::GetWindowTextLength(hWndCtrl);
::GetWindowText(hWndCtrl, strTemp.GetBufferSetLength(nLen), nLen+1);
strTemp.ReleaseBuffer();
if (strTemp.IsEmpty() || strTemp == _T(" "))
{
value.SetStatus(COleDateTime::null);
}
else
{
if (!value.ParseDateTime(strTemp)) // throws exception
{
value.SetStatus(COleDateTime::null);
// Can't convert string to datetime
AfxMessageBox(AFX_IDP_PARSE_DATETIME);
pDX->Fail(); // throws exception
}
}
}
else
{
if (value.GetStatus() == COleDateTime::valid)
{
pWnd->SetFormat(NULL);
pWnd->SetTime(value);
strTemp = value.Format();
}
else
{
// Sets the time to the calendar
pWnd->SetTime(COleDateTime::GetCurrentTime());
// Clears the DateTime control
pWnd->SetFormat(" ");
strTemp = _T("");
AfxSetWindowText(hWndCtrl, strTemp);
}
}
}
// CDateTimeEx
IMPLEMENT_DYNAMIC(CDateTimeEx, CDateTimeCtrl)
CDateTimeEx::CDateTimeEx()
: m_nIdCtrl(0)
, m_pWnd(NULL)
{
}
CDateTimeEx::~CDateTimeEx()
{
}
BEGIN_MESSAGE_MAP(CDateTimeEx, CDateTimeCtrl)
ON_COMMAND(IDM_POPUP_DELETE, &CDateTimeEx::OnPopupDelete)
ON_NOTIFY_REFLECT(DTN_DATETIMECHANGE, &CDateTimeEx::OnDtnDatetimechange)
END_MESSAGE_MAP()
// CDateTimeEx message handlers
BOOL CDateTimeEx::PreTranslateMessage(MSG* pMsg)
{
BOOL bCtrlFound = FALSE;
// Retreiving Control ID
m_pWnd = FromHandle(pMsg->hwnd);
if (m_pWnd != NULL)
{
m_nIdCtrl = m_pWnd->GetDlgCtrlID();
bCtrlFound = TRUE;
}
switch (pMsg->message)
{
case WM_MOUSEMOVE:
break;
case WM_CHAR:
break;
case WM_LBUTTONDBLCLK:
break;
case WM_RBUTTONUP:
break;
case WM_RBUTTONDOWN:
ShowMenu();
break;
case WM_LBUTTONDOWN:
break;
case WM_KEYDOWN:
// No ESCAPE or RETURN Key
if( pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE )
{
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return FALSE; // DO NOT process further
}
break;
// NO ALT+ key
case WM_SYSCOMMAND:
return TRUE;
break;
}
return CDateTimeCtrl::PreTranslateMessage(pMsg);
}
// Shows the delete popup menu item
void CDateTimeEx::ShowMenu(void)
{
int n = 0;
// Popup menu
CMenu popUpMenu;
popUpMenu.CreatePopupMenu();
popUpMenu.InsertMenu(n, MF_BYPOSITION, IDM_POPUP_DELETE, _T("Supprimer")); n++;
CPoint p;
GetCursorPos(&p);
popUpMenu.TrackPopupMenu(TPM_CENTERALIGN | TPM_LEFTBUTTON,
p.x, p.y, (CWnd *) this, NULL);
}
// Clears window content
void CDateTimeEx::OnPopupDelete()
{
SetFormat(" ");
m_pWnd->SetWindowTextA(_T(""));
}
void CDateTimeEx::OnDtnDatetimechange(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
// Sets format to default sytem date format
SetFormat(NULL);
*pResult = 0;
>> On Monday, March 24, 2008 1:16 PM Sheng Jiang[MVP] wrote:
>> use DTS_SHOWNONE
>>
>> --
>> Sheng Jiang
>> Microsoft MVP in VC++
>>> On Tuesday, March 25, 2008 7:44 AM rockdale wrote:
>>> Hi, all:
>>>
>>> I have a datetime fiels visit_date on my dialog which currently using
>>> CDateTimeCtrl for use to pick the date that they visit, but the visit
>>> date should allow null which the CdateTimeCtrl does not support, what
>>> is the best way to implement this?
>>>
>>> I am thinking using a masked CEdit ctrl for user to enter date /delete
>>> date, then put a button besidethe textbox so user can click this
>>> button to shoiw the calendar if they want choose date from the
>>> CDateTimeCtrl?
>>>
>>> Or are any better implementations out there already, please point me
>>> to the hyperlinks.
>>>
>>> thanks
>>> -rockdale
>>>> On Tuesday, March 25, 2008 7:44 AM rockdale wrote:
>>>> The first time I can use SetFormat("") to hide Today's date when I
>>>> initDialog, Then the user can
>>>> pick a date from the CDateTimeCtrl, but what if they try to delete/
>>>> empty the date they picked before? This part I do not know how. add a
>>>> button say "clear"
>>>> also, How could I know the DateTime is picked by the user or is the
>>>> underline datetime for SetFormat(""), or should I set a special date
>>>> so that I know the user did not pick a date?
>>>>
>>>> thanks again
>>>>
>>>> On Mar 24, 1:16=A0pm, "Sheng Jiang[MVP]"
>>>> <sheng_ji...@hotmail.com.discuss> wrote:
>>>> e
>>>>> On Wednesday, March 26, 2008 11:47 AM Sheng Jiang[MVP] wrote:
>>>>> I am not suggesting changing the formating. I am suggesting the DTS_SHOWNONE
>>>>> style.
>>>>> Use SetTime/GetTime to get/set the time.A null time has a COleDateTime::null
>>>>> status.
>>>>>
>>>>> --
>>>>> Sheng Jiang
>>>>> Microsoft MVP in VC++
>>>>> "rockdale" <
rockdal...@gmail.com> wrote in message
>>>>> news:f187b421-065f-4332...@m3g2000hsc.googlegroups.com...
>>>>> The first time I can use SetFormat("") to hide Today's date when I
>>>>> initDialog, Then the user can
>>>>> pick a date from the CDateTimeCtrl, but what if they try to delete/
>>>>> empty the date they picked before? This part I do not know how. add a
>>>>> button say "clear"
>>>>> also, How could I know the DateTime is picked by the user or is the
>>>>> underline datetime for SetFormat(""), or should I set a special date
>>>>> so that I know the user did not pick a date?
>>>>>
>>>>> thanks again
>>>>>
>>>>> On Mar 24, 1:16 pm, "Sheng Jiang[MVP]"
>>>>> <sheng_ji...@hotmail.com.discuss> wrote:
>>>>> message
>>>>>> On Wednesday, March 26, 2008 12:51 PM Doug Harrison [MVP] wrote:
>>>>>> On Wed, 26 Mar 2008 09:47:38 -0600, "Sheng Jiang[MVP]"
>>>>>> <sheng...@hotmail.com.discuss> wrote:
>>>>>>
>>>>>>
>>>>>> Last time I looked, DTS_SHOWNONE added a checkbox inside the control, and
>>>>>> when unchecked, simply grayed the text of the control. I never could get
>>>>>> the control to display nothing at all using DTS_SHOWNONE. The only way I
>>>>>> found was the SetFormat method I posted earlier.
>>>>>>
>>>>>> --
>>>>>> Doug Harrison
>>>>>> Visual C++ MVP
>>>>>>> On Wednesday, March 26, 2008 2:07 PM Sheng Jiang[MVP] wrote:
>>>>>>> The op wants to
>>>>>>> 1 pick the date that they visit,
>>>>>>> 2 allow null which he thinks the CDateTimeCtrl does not support
>>>>>>> I just point out CDateTimeCtrl does support null values.
>>>>>>>
>>>>>>> I agree changing the formating will look nicer, but the feedback from data
>>>>>>> entry people is that masked editbox makes input faster.
>>>>>>>
>>>>>>> --
>>>>>>> Sheng Jiang
>>>>>>> Microsoft MVP in VC++
>>>>>>> "Doug Harrison [MVP]" <
d...@mvps.org> wrote in message
>>>>>>> news:20vku39l4nu2vd42u...@4ax.com...
>>>>>>> DTS_SHOWNONE
>>>>>>> COleDateTime::null