本主题演示如何处理日期和时间选取器 (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);
}
相关主题