Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 API : how to make Edit Text to accept unsigned float only in c++?

I'm trying to learn win32 api :)

I have some edit text placed in DialogBox and I want it to accept only float numbers that are greater than 0

I was able to make that edit text to accept Integers only by using the style "ES_NUMBER" in resource file but I can't find any way how to make it accept positive float values please I need your help thanks

like image 916
user2398647 Avatar asked May 19 '13 10:05

user2398647


3 Answers

In addition to handling the EN_CHANGE notification you also have the option of subclassing the window. This will allow you to restrict which keystrokes are valid and only allow numbers, a dot, etc. The example below shows how to create an edit control, subclass it and filter the input so that only specific characters are allowed. It does not handle operations such as pasting from the clipboard so you will want to expand it to meet your specific requirements.

The benefits of this approach are that you do not need to add any additional code to the parent window to filter the edit control. This allows you to use it throughout your application without having to duplicate a lot of code. Another benefit is it eliminates possible flicker that occurs from updating the contents of the control to remove unwanted characters.

static WNDPROC OriginalEditCtrlProc = NULL;

LRESULT CALLBACK MyWindowProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam)
{
    if(uMsg == WM_CHAR)
    {
        // Make sure we only allow specific characters
        if(!    ((wParam >= '0' && wParam <= '9')
                || wParam == '.'
                || wParam == VK_RETURN
                || wParam == VK_DELETE
                || wParam == VK_BACK))
        {
            return 0;
        }
    }

    return CallWindowProc(OriginalEditCtrlProc, hwnd, uMsg, wParam, lParam);
}

void CreateCustomEdit(HINSTANCE hInstance, HWND hParent, UINT id)
{
    HWND hwnd;

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        _T("EDIT"),
        _T(""),
        WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
        0, 0, 200, 40,
        hParent,
        reinterpret_cast<HMENU>(id),
        hInstance,
        NULL);
    if(hwnd != NULL)
    {
        // Subclass the window so we can filter keystrokes
        WNDPROC oldProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(
            hwnd,
            GWLP_WNDPROC,
            reinterpret_cast<LONG_PTR>(MyWindowProc)));
        if(OriginalEditCtrlProc == NULL)
        {
            OriginalEditCtrlProc = oldProc;
        }
    }
}
like image 80
Captain Obvlious Avatar answered Nov 08 '22 06:11

Captain Obvlious


Use EN_UPDATE notifications and if the user types a minus sign, simply delete it. The net effect will be exactly what you want: a control that accepts only positive floats. Don't use EN_CHANGE because that is sent after the control has been redrawn, and changing the input then will require another redraw, which will give the impression of the control flickering.

like image 35
Carey Gregory Avatar answered Nov 08 '22 06:11

Carey Gregory


There is no unsigned float in c++ so not possible!

You will probably have to validate explicitly.

like image 31
Bathsheba Avatar answered Nov 08 '22 05:11

Bathsheba