DTN_FORMAT Bildirimini İşleme

Bu konuda, tarih ve saat seçici (DTP) denetimi tarafından gönderilen bir biçim bildiriminin nasıl işlendiği gösterilmektedir.

Bilmeniz gerekenler

Teknolojileri

Önkoşullar

  • C/C++
  • Windows Kullanıcı Arayüzü Programlama

Talimat

DTP denetimi, denetimin geri çağırma alanında görüntülenecek metni istemek için DTN_FORMAT bildirimini gönderir. Uygulamanızın DTP denetiminin yerel olarak desteklemediği bilgileri görüntülemesini sağlamak için bu bildirimi işlemesi gerekir.

Aşağıdaki C++ kod örneği, geri çağırma alanı için bir metin dizesi sağlayarak DTN_FORMAT bildirim kodlarını işleyen uygulama tanımlı bir işlevdir (DoFormat). Uygulama, geri çağırma dizesinde kullanılacak gün numarasını istemek için GetDayNum uygulama tanımlı işlevi çağırır.

//  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 uygulama tanımlı işlevi

Uygulama tanımlı örnek işlev DoFormat, geçerli tarihe göre gün numarasını istemek için aşağıdaki GetDayNum uygulama tanımlı işlevini çağırır. GetDayNum, 0 ile 366 arası yılın geçerli gününü temsil eden bir INT değeri döndürür. Bu işlev, işleme sırasında IsLeapYr başka bir uygulama tanımlı işlevi çağırır.

//  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 uygulama tarafından tanımlanan işlev

GetDayNum uygulama tanımlı işlevi, geçerli yılın artık yıl olup olmadığını belirlemek için IsLeapYr işlevini çağırır. IsLeapYr, eğer artık yılsa TRUE olan bir BOOL değeri döndürür ve aksi takdirde FALSE döndürür.

//  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);
}        

Tarih ve Saat Seçici Denetimlerini Kullanma

Tarih ve Saat Seçici Kontrol Referansı

tarih ve saat seçici