Search code examples
winapiwin32guiwin32-process

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


I have tried tooltip feature in win32 code, when 'OK' clicked the message displays only when we hover on particular box. But the feature Iam expecting is auto display of message at particular box when 'OK' is clicked. Is it possible to add such feature of auto popup? I need some balloon type of error popup. My exact scenario is to error popup when Zero is given as input in a dialog, when 'OK' is clicked.

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

Solution

  • The following is an example of balloon tip for display information when input value is not valid.

    #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;
    

    Refer to Edit Control, EDITBALLOONTIP structure, Edit_ShowBalloonTip macro

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

    It will like this:

    enter image description here