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
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;
}
}
}
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.
There is no unsigned float in c++ so not possible!
You will probably have to validate explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With