在 win32 代码中输入 'zero' 时提示自动消息

Prompt auto message when 'zero' as input in win32 code

我尝试过 win32 代码中的工具提示功能,当 'OK' 单击时,消息仅在我们将鼠标悬停在特定框上时显示。但是我期望的功能是单击 'OK' 时在特定框中自动显示消息。是否可以添加自动弹出的功能?我需要一些气球类型的错误弹出窗口。 我的确切情况是在单击 'OK' 时在对话框中输入零时出现错误弹出窗口。

HWND gzui_controls::create_tool_tip_balloon(HWND hdlg, int tool_id, PTSTR text) const { if (!tool_id || !hdlg || !text) { return FALSE; }

HWND hwndTool = GetDlgItem(hdlg, tool_id);
if (WM_LBUTTONUP)
{
HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
        WS_POPUP | SWP_NOMOVE | TTS_NOPREFIX | TTS_BALLOON | BS_PUSHBUTTON,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        hdlg, NULL,
        getModuleInstance(), NULL);

    if (!hwndTool || !hwndTip)
    {
        return (HWND)NULL;
    }

    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hdlg;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;
    toolInfo.lpszText = text;
    SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
    return hwndTip;
}

以下是输入值无效时显示信息的气球提示示例。

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

// ...

   editHwnd = CreateWindow(L"EDIT", 
       NULL, 
       WS_VISIBLE | WS_CHILD | ES_MULTILINE | WS_BORDER,
       120,
       10,
       100,
       100,
       hWnd,
       NULL,
       hInst,
       NULL);

// ...

case WM_COMMAND:
    {
        if (HIWORD(wParam) == EN_CHANGE)
        {
            // TODO: Add logic of detecting input value here
            if (editHwnd == (HWND)lParam) // && inputVal == 0
            {
                balloonTip.cbStruct = sizeof(EDITBALLOONTIP);
                balloonTip.pszText = L"Zero is given as input";
                balloonTip.pszTitle = L"Tips";
                balloonTip.ttiIcon = TTI_INFO;
                Edit_ShowBalloonTip(editHwnd, &balloonTip);
            }
        }
    }
    break;

参考Edit Control, EDITBALLOONTIP structure, Edit_ShowBalloonTip macro

Note To use Edit_ShowBalloonTip macro, you must provide a manifest specifying Comclt32.dll version 6.0.

会喜欢这样: