共用方式為


如何處理DTN_FORMATQUERY通知

本主題示範如何處理日期和時間選擇器 (DTP) 控制項所傳送的格式查詢通知。

您需要知道的事項

技術

必要條件

  • C/C++
  • Windows 使用者介面程序設計

指示

DTP 控制件會傳送 DTN_FORMATQUERY 通知碼,以要求控制件內回呼欄位大小上限的相關信息。 您的應用程式必須處理此訊息,以確保所有欄位都正確顯示。

下列 C++ 程式代碼範例是應用程式定義的函式,可計算指定回呼欄位最寬字串的寬度,以處理 DTN_FORMATQUERY 通知程式碼。

安全性警告: 使用 lstrcmp 不正確可能會危害應用程式的安全性。 例如,在下列程式代碼範例中呼叫 lstrcmp 之前,您應該確定兩個字串都是以 Null 終止。 您應該先檢閱 安全性考慮:Microsoft Windows 控件 再繼續。

//  DoFormatQuery processes DTN_FORMATQUERY messages to ensure that the
//  DTP control displays callback fields properly.
//

void WINAPI DoFormatQuery(
 HWND hwndDP, 
 LPNMDATETIMEFORMATQUERY lpDTFQuery)
{
    HDC hdc;
    HFONT hFont, hOrigFont;

    //  Prepare the device context for GetTextExtentPoint32 call.
    hdc = GetDC(hwndDP);

    hFont = (HFONT) SendMessage(hwndDP, WM_GETFONT, 0L, 0L); 

    if(!hFont)
        hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);

    hOrigFont = (HFONT) SelectObject(hdc, hFont);

    // Check to see if this is the callback segment desired. If so,
    // use the longest text segment to determine the maximum 
    // width of the callback field, and then place the information into 
    // the NMDATETIMEFORMATQUERY structure.
    if(!lstrcmp(L"XX",lpDTFQuery->pszFormat))
        GetTextExtentPoint32 (hdc,
                          L"366",  // widest date string
                          3,
                          &lpDTFQuery->szMax);

    // Reset the font in the device context; then release the context.
    SelectObject(hdc,hOrigFont);
    ReleaseDC(hwndDP, hdc);
}

使用日期和時間選擇器控制件

日期和時間選擇器控件參考

日期和時間選擇器