Is there a way to get the handle of the entry field in a date time picker (DTP)?

眉间皱痕 提交于 2019-12-24 08:46:40

问题


The DATETIMEPICKERINFOstructure obtained by sending the DTM_GETDATETIMEPICKERINFOmessage has a field hwndEdit which might be what I'm looking for. However, I'm getting always NULL for it so I'm wondering what's its actual meaning. If not, is there a way to get the handle of the entry field?


回答1:


hwndEdit only seems to be valid when the control has the DTS_APPCANPARSE style and you click the date text with the mouse (I tested this with OutputDebugString and a timer). The edit control is created and destroyed dynamically. The hwndUD handle is only valid if DTS_UPDOWN is set and the hwndDropDown is only valid while the dropdown is visible.

It is not called out in the documentation but DTM_GETDATETIMEPICKERINFO is marked Vista+ and this often means the feature is only implemented in ComCtl32 v6 so you also have to make sure you have a manifest that requests this version.

To change the color you can try DTM_SETMCCOLOR but only MCSC_BACKGROUND is documented to work when Visual Styles are active.




回答2:


I'm afraid there is no way to get what you wanted. I just created a simple Win32 application just to test the possibility. If I use the DTM_GETDATETIMEPICKERINFO, hwndDropDown, hwndEdit and hwndUD give me NULL. If I try to enum child window, well before I do so I check it with Spy++, no luck, there is no child window associated with it.

Finally, I tried GetFocus() and WindowFromPoint(), both give me the HWND of the DateTimePicker itself only.

Here is my testing code:

#pragma comment(lib, "comctl32.lib")
#include <windows.h>
#include <tchar.h>
#include <commctrl.h>

enum MYID {
    MYID_FIRST = WM_APP,
    MYID_DTP
};

LPCTSTR const g_MyWndClass = _T("DTPTest");
LPCTSTR const g_MyWndTitle = _T("DTPTest");

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void OnWindowCreate(HWND);
void OnTimer(HWND);

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int nCmdShow)
{
    INITCOMMONCONTROLSEX icex{};
    icex.dwSize = sizeof(icex);
    icex.dwICC = ICC_DATE_CLASSES;
    InitCommonControlsEx(&icex);
    WNDCLASSEX wcex{};
    wcex.cbSize = sizeof(wcex);
    wcex.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.lpfnWndProc = WndProc;
    wcex.lpszClassName = g_MyWndClass;
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClassEx(&wcex);
    HWND hwnd = CreateWindowEx(0,
        g_MyWndClass, g_MyWndTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, 600, 400,
        nullptr, nullptr, nullptr, nullptr);
    if (!hwnd) { return 99; }
    SetTimer(hwnd, 0, 100, nullptr);
    ShowWindow(hwnd, nCmdShow);
    MSG msg{};
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return static_cast<int>(msg.wParam);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
    switch (msg) {
    case WM_CREATE:
        OnWindowCreate(hwnd);
        break;
    case WM_TIMER:
        OnTimer(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, w, l);
    }
    return 0;
}

void OnWindowCreate(HWND hwnd)
{
    HWND hwndDTP = CreateWindowEx(0, DATETIMEPICK_CLASS, nullptr,
        WS_CHILD | WS_VISIBLE | DTS_SHOWNONE,
        20, 50, 220, 20,
        hwnd, reinterpret_cast<HMENU>(MYID_DTP), nullptr, nullptr);
    DATETIMEPICKERINFO info{};
    info.cbSize = sizeof(DATETIMEPICKERINFO);
    SendMessage(hwndDTP, DTM_GETDATETIMEPICKERINFO, 0,
        reinterpret_cast<LPARAM>(&info));
    if (!info.hwndDropDown && !info.hwndEdit && !info.hwndUD)
    {
        MessageBox(hwnd, _T("No luck with DTM_GETDATETIMEPICKERINFO"),
            nullptr, MB_ICONERROR);
    }
}

void OnTimer(HWND hwnd)
{
    POINT pt{};
    GetCursorPos(&pt);
    HWND hwndPoint = WindowFromPoint(pt);
    HWND hwndFocus = GetFocus();
    TCHAR buf[99]{};
    wsprintf(buf, _T("Pointing at %p, focusing %p"),
        hwndPoint, hwndFocus);
    SetWindowText(hwnd, buf);
}


来源:https://stackoverflow.com/questions/43576035/is-there-a-way-to-get-the-handle-of-the-entry-field-in-a-date-time-picker-dtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!