如何處理DTN_FORMAT通知
本主題示範如何處理日期和時間選擇器 (DTP) 控制項所傳送的格式通知。
您需要知道的事項
技術
必要條件
- C/C++
- Windows 使用者介面程序設計
指示
DTP 控制件會 傳送DTN_FORMAT 通知,以要求將在控件的回呼欄位中顯示的文字。 您的應用程式必須處理此通知,才能讓 DTP 控制件顯示它原本不支援的資訊。
下列 C++ 程式代碼範例是應用程式定義的函式 (DoFormat),其會藉由提供回呼字段的文字字串來處理 DTN_FORMAT 通知碼。 應用程式會呼叫 GetDayNum 應用程式定義函式,要求回呼字串中使用的日期號碼。
// DoFormat processes DTN_FORMAT to provide the text for a callback
// field in a DTP control. In this example, the callback field
// contains a value for the day of year. The function calls the
// application-defined function GetDayNum (below) to retrieve
// the correct value. StringCchPrintf truncates the formatted string if
// the entire string will not fit into the destination buffer.
void WINAPI DoFormat(HWND hwndDP, LPNMDATETIMEFORMAT lpDTFormat)
{
HRESULT hr = StringCchPrintf(lpDTFormat->szDisplay,
sizeof(lpDTFormat->szDisplay)/sizeof(lpDTFormat->szDisplay[0]),
L"%d",GetDayNum(&lpDTFormat->st));
if(SUCCEEDED(hr))
{
// Insert code here to handle the case when the function succeeds.
}
else
{
// Insert code here to handle the case when the function fails or the string
// is truncated.
}
}
GetDayNum 應用程式定義的函式
應用程式定義的範例函 式 DoFormat 會呼叫下列 GetDayNum 應用程式定義函式,以根據目前的日期要求日期號碼。 GetDayNum 會傳 回 INT 值,代表年份的目前日期,從 0 到 366。 此函式會在處理期間呼叫另一個應用程式定義的函 式 IsLeapYr。
// GetDayNum is an application-defined function that retrieves the
// correct day of year value based on the contents of a given
// SYSTEMTIME structure. This function calls the IsLeapYr function to
// check if the current year is a leap year. The function returns an
// integer value that represents the day of year.
int WINAPI GetDayNum(SYSTEMTIME *st)
{
int iNormYearAccum[ ] = {31,59,90,120,151,181,
212,243,273,304,334,365};
int iLeapYearAccum[ ] = {31,60,91,121,152,182,
213,244,274,305,335,366};
int iDayNum;
if(IsLeapYr(st->wYear))
iDayNum=iLeapYearAccum[st->wMonth-2]+st->wDay;
else
iDayNum=iNormYearAccum[st->wMonth-2]+st->wDay;
return (iDayNum);
}
IsLeapYr 應用程式定義函式
應用程式定義的 函式 GetDayNum 會呼叫 IsLeapYr 函 式,以判斷目前年份是否為閏年。 IsLeapYr 會傳回 BOOL 值,如果它是閏年,則傳回 TRUE,否則傳回 FALSE。
// IsLeapYr determines if a given year value is a leap year. The
// function returns TRUE if the current year is a leap year, and
// FALSE otherwise.
BOOL WINAPI IsLeapYr(int iYear)
{
BOOL fLeapYr = FALSE;
// If the year is evenly divisible by 4 and not by 100, but is
// divisible by 400, it is a leap year.
if ((!(iYear % 4)) // each even fourth year
&& ((iYear % 100) // not each even 100 year
|| (!(iYear % 400)))) // but each even 400 year
fLeapYr = TRUE;
return (fLeapYr);
}
相關主題