Wednesday, January 26, 2011

CDateTimeCtrl Class

CDateTimeCtrl Class  :
Encapsulates the functionality of a date and time picker control.

class CDateTimeCtrl : public CWnd

The date and time picker control (DTP control) provides a simple interface to exchange date and time information with a user. This interface contains fields, each of which displays a part of the date and time information stored in the control. The user can change the information stored in the control by changing the content of the string in a given field. The user can move from field to field using the mouse or the keyboard.

You can customize the date and time picker control by applying a variety of styles to the object when you create it.

Header: afxdtctl.h

Base Class Members:

CObject Members
CCmdTarget Members
CWnd Members

Construction
CDateTimeCtrl

Constructs a CDateTimeCtrl object.

Create  : Creates the date and time picker control and attaches it to the CDateTimeCtrl object.

Attributes
GetMonthCalColor :  Retrieves the color for a given portion of the month calendar within the date and time picker control.

GetMonthCalCtrl :  Retrieves the date and time picker's child month calendar control.

GetMonthCalFont: Retrieves the font currently used by the date and time picker control's child month calendar control.

GetRange   :  Retrieves the current minimum and maximum allowable system times for a date and time picker control.

SetFormat :  Sets the display of a date and time picker control based on a given format string.

SetMonthCalColor :  Sets the color for a given portion of the month calendar within a date and time picker control.

SetMonthCalFont : Sets the font to be used by the date and time picker control's child month calendar control.

SetRange : Sets the minimum and maximum allowable system times for a date and time picker control.

Operations

GetTime : Retrieves the currently selected time from a date and time picker control and places it in a specified SYSTEMTIME structure.

SetTime : Sets the time in a date and time picker control.


CDateTimeCtrl::SetRange

Sets the minimum and maximum allowable system times for a date and time picker control.

Signatures:
BOOL SetRange( const COleDateTime* pMinRange,   const COleDateTime* pMaxRange );
BOOL SetRange( const CTime* pMinRange,  const CTime* pMaxRange );

Parameters

pMinRange :   A pointer to a COleDateTime object or a CTime object containing the earliest time allowed in the CDateTimeCtrl object.

pMaxRange : A pointer to a COleDateTime object or a CTime object containing the latest time allowed in the CDateTimeCtrl object.

Return Value
Nonzero if successful; otherwise 0.


CDateTimeCtrl::GetRange

Retrieves the current minimum and maximum allowable system times for a date and time picker control.

DWORD GetRange(COleDateTime* pMinRange, COleDateTime* pMaxRange ) const;
DWORD GetRange(  CTime* pMinRange,   CTime* pMaxRange ) const;

Parameters:
pMinRange : A pointer to a COleDateTime object or a CTime object containing the earliest time allowed in the CDateTimeCtrl object.

pMaxRange : A pointer to a COleDateTime object or a CTime object containing the latest time allowed in the CDateTimeCtrl object.

Return Value
A DWORD value containing flags that indicate which ranges are set.
 If  return value & GDTR_MAX == 0

then the second parameter is valid. Similarly, if
return value & GDTR_MIN == 0, then the first parameter is valid.

EXAMPLE
// This function will set several ranges in the control, then  call the ShowRange() function to show the set ranges to the  user.

void CDatesDlg::OnTestBTN()
{
// Gain a pointer to the control  CMonthCalCtrl* pCtrl = (CMonthCalCtrl*)  GetDlgItem //IDC_MONTHCALENDAR1);

CDateTimeCtrl* pCtrl = (CDateTimeCtrl*) GetDlgItem(IDC_DATETIMEPICKER1);
ASSERT(pCtrl != NULL);

// Set minimum of January 1st, 1995 with no maximum.
COleDateTime dtMin;
COleDateTime dtMax;

dtMin = COleDateTime(1995, 1, 1, 0, 0, 0);
dtMax.SetStatus(COleDateTime::null);
pCtrl->SetRange(&dtMin, &dtMax);
ShowRange(pCtrl);

// Set no minimum and maximum of September 30th, 1997.
dtMin.SetStatus(COleDateTime::null);
dtMax = COleDateTime(1997, 9, 30, 0, 0, 0);
pCtrl->SetRange(&dtMin, &dtMax);
ShowRange(pCtrl);

// Set minimum of April 15, 1992 and maximum of June 5, 2002.
dtMin = COleDateTime(1992, 4, 15, 0, 0, 0);
dtMax = COleDateTime(2002, 6, 5, 0, 0, 0);
pCtrl->SetRange(&dtMin, &dtMax);

ShowRange(pCtrl);
}

void CDatesDlg::ShowRange(CDateTimeCtrl* pCtrl)
{
   ASSERT(pCtrl != NULL);
   CString strMessage;
   COleDateTime dtMinimum;
   COleDateTime dtMaximum;

// Get the range.
WORD dwResult = pCtrl->GetRange(&dtMinimum, &dtMaximum);

// If a minimum was specified, format it. Otherwise, indicate that there is no lower bound.
if (dwResult & GDTR_MIN)
strMessage += dtMinimum.Format(_T("Minimum range is %x %X.\r\n"));

else
strMessage += _T("No minimum range.\r\n");

// Treat maximum similarly.
if (dwResult & GDTR_MAX)
strMessage += dtMaximum.Format(_T("Maximum range is %x %X.\r\n"));

else
strMessage += _T("No maximum range.\r\n");

// Show the user.
AfxMessageBox(strMessage);
}

Also read :
http://journeyallover.blogspot.com/2010/08/date-and-time-in-c.html

No comments:

Post a Comment

Health Benefits of Cashews

  Benefits of Cashews. Healthy food is an integral part of healthy body. Regular exercises such as Yoga and healthy diet is important to...