Partilhar via


Como processar a notificação de DTN_FORMAT

Este tópico demonstra como processar uma notificação de formato enviada pelo controle DTP (seletor de data e hora).

O que precisa de saber

Tecnologias

Pré-requisitos

  • C/C++
  • Programação da interface do usuário do Windows

Instruções

Um controlo DTP envia a notificação DTN_FORMAT para solicitar o texto que será exibido num campo de retorno de chamada do controlo. Seu aplicativo deve manipular essa notificação para permitir que o controle DTP exiba informações que ele não suporta nativamente.

O exemplo de código C++ a seguir é uma função definida pelo aplicativo (DoFormat) que processa DTN_FORMAT códigos de notificação fornecendo uma cadeia de caracteres de texto para um campo de retorno de chamada. A aplicação chama a função definida pela aplicação GetDayNum para solicitar o número do dia a ser usado na cadeia de caracteres de retorno de chamada.

//  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.
 }
}

A função definida pelo aplicativo GetDayNum

A função de exemplo definida pelo aplicativo DoFormat chama a seguinte função GetDayNum definida pelo aplicativo para solicitar o número do dia baseado na data atual. GetDayNum retorna um valor de INT que representa o dia atual do ano, de 0 a 366. Esta função chama outra função definida pelo aplicativo, IsLeapYr, durante o processamento.

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

A função definida pelo aplicativo IsLeapYr

A função definida pelo aplicativo GetDayNum chama a função IsLeapYr para determinar se o ano atual é um ano bissexto. IsLeapYr retorna um valor de BOOL que é VERDADEIRO se for um ano bissexto e FALSO de outra forma.

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

Usando controles de seletor de data e hora

Controle de Referência de Seletor de Data e Hora

Seletor de Data e Hora