이 항목에서는 DTP(날짜 및 시간 선택기) 컨트롤에서 보낸 형식 알림을 처리하는 방법을 보여 줍니다.
알아야 할 사항
기술
필수 구성 요소
- C/C++
- Windows 사용자 인터페이스 프로그래밍
지시
DTP 컨트롤은 컨트롤의 콜백 필드에 표시될 텍스트를 요청하기 위해 DTN_FORMAT 알림을 보냅니다. 애플리케이션이 기본적으로 지원하지 않는 정보를 표시하도록 DTP 컨트롤을 사용하도록 설정하려면 이 알림을 처리해야 합니다.
다음 C++ 코드 예제는 콜백 필드에 텍스트 문자열을 제공하여 DTN_FORMAT 알림 코드를 처리하는 애플리케이션 정의 함수(DoFormat)입니다. 애플리케이션은 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 0에서 366까지의 현재 연도를 나타내는 INT 값을 반환합니다. 이 함수는 처리하는 동안 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 윤년인 경우 TRUE인 BOOL 값을 반환하고, 그렇지 않으면 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);
}
관련 항목