(WIN32 API) Edit Control Style not applying to RichEdit in CreateWindowEx - c++

I used the following code and it worked fine, allowed the user to only enter in numbers. I wanted to increase the functionality by using RichEdit so I added that.
I went from using:
wchar_t sampletext[] = L"foobar";
HWND inputText = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", sampletext,
ES_NUMBER | WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_MULTILINE,
10, 10, 500, 75, hWnd, NULL, *hInst, NULL);
to:
LoadLibrary(L"riched32.dll");
wchar_t sampletext[] = L"foobar";
HWND inputText = CreateWindowEx(WS_EX_CLIENTEDGE, L"RichEdit", sampletext,
ES_NUMBER | WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_MULTILINE,
10, 10, 500, 75, hWnd, NULL, *hInst, NULL);
Changing it allowed me to use CTRL+Z and CTRL+A and all but now for some reason I could enter characters that weren't numbers.
How do I fix this so Rich Edit only accepts number in this text field?
or
Alternatively, how would I create my own custom filter that would only accept numbers into the text field?
EDIT:
Here's an image of me typing

The "RichEdit" control class has its own set of styles.
But ES_NUMBER is one of them, according to MSDN. However, note the comment by ElmueSoft.
To filter input to only digits without help from the control, you can subclass it. You'd need to handle quite a few messages though. WM_CHAR is the most obvious, but WM_PASTE and WM_SETTEXT can have non-numeric text passed in.
Good information on subclassing:
http://blogs.msdn.com/b/oldnewthing/archive/2009/05/07/9592397.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx

Related

buttons are all highlighted

I have two buttons. Each button is highlighted instead of just the first one. How do I make the first button to be only the one highlighted?
BTW This is the code for the two buttons:
openFileButton = CreateWindowA("button", "Open File and start hashing", WS_VISIBLE | WS_CHILD | SS_CENTER, 220, 210, 200, 50, hWnd,
(HMENU)BUTTON_OPEN_FILE, NULL, NULL);
SendMessage(openFileButton, WM_SETFONT, my_font, true);
hashFileButton = CreateWindowA("button", "Get the File hash", WS_VISIBLE | WS_CHILD | SS_CENTER, 220, 270, 200, 50, hWnd,
(HMENU)BUTTON_HASH_FILE, NULL, NULL);
SendMessage(hashFileButton, WM_SETFONT, my_font, true);
Tried using SetFocus but got no luck.
First, stop calling the A versions of WinAPI functions. Those went obsolete back in the '90s. Use the W versions instead, with L prefixes on your string literals and wchar_t as your character type.
Second, you are passing the wrong style flags for buttons. Specifically, you are passing SS_CENTER. This is a style for static controls (hence the SS_ prefix), not for buttons. SS_CENTER is #defined in the Windows header files as the value 0x1, which is equivalent to the button style, BS_DEFPUSHBUTTON. (Note that #define macros are not type-safe, so the compiler doesn't know and can't warn you that you're specifying a static-control style when creating a button control.)
The presence of this erroneous BS_DEFPUSHBUTTON style is why your buttons are both appearing highlighted, as if they are the default button in a dialog box. For standard buttons, you want BS_PUSHBUTTON, which is #defined as the value 0x0, and thus equivalent to not passing any style flags to your CreateWindow function. But for readability and self-documenting code, I would strongly recommend explicitly passing BS_PUSHBUTTON.
So, your corrected code becomes:
openFileButton = CreateWindowW(L"button",
L"Open File and Start Hashing",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
220, 210, 200, 50,
hWnd,
(HMENU)BUTTON_OPEN_FILE,
NULL,
NULL);
SendMessage(openFileButton, WM_SETFONT, my_font, true);
hashFileButton = CreateWindowW(L"button",
L"Get the File Hash",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
220, 270, 200, 50,
hWnd,
(HMENU)BUTTON_HASH_FILE,
NULL,
NULL);
SendMessage(hashFileButton, WM_SETFONT, my_font, true);
You might actually want to make one (and only one) of your buttons into the "default" button using the BS_DEFPUSHBUTTON style. The behavior is easily implemented by calling the IsDialogMessage function within your main message loop (behavior which you would get for "free" by creating a dialog box, with the added boon of the Resource Editor to lay out the controls and set styles).

Use combobox into Listview Items in C++ win32 api

How can I set items of a listview as Combobox in C++ win32 api?
my listview
hWndListView = CreateWindowEx(
WS_EX_CLIENTEDGE,
WC_LISTVIEW,
L"",
WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
10,
100,
430,
400,
hWndx,
(HMENU)IDC_LIST,
NULL,
NULL);
my combobox
HWND hComboBox2 = CreateWindow(
WC_COMBOBOX,
CMBX_CLASS_NAME,
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
10,
50,
300,
300,
hWndListView,
NULL,
NULL,
NULL);
Is it possible?
thank you!
It's not possible with standard ListView common control. You can implement your own control that does what you want by subclassing standard one, and letting it do the painting, and all other functionality. Your customization then will include responding to clicks, hittesting to figure out where click happened (with LVM_HITTEST), creating and displaying combobox, and updating item/subitem with new selection from combobox.

How to get text from a textbox in the Windows API

I've been scratching my head over this for a week now. I'm using the Windows API and I made a textbox with
editBox = CreateWindowEx(WS_EX_PALETTEWINDOW, TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | ES_MULTILINE | ES_LEFT | ES_AUTOVSCROLL, 175, 110, 140, 150, hwnd, (HMENU) ID_TEXT, NULL, NULL);
and a button
button = CreateWindowEx(WS_EX_PALETTEWINDOW, "BUTTON", "Ok", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 175, 260, 140, 20, hwnd, (HMENU) ID_BUTTON, NULL, NULL);
and I want to be able to click the button and it store what was typed into editBox in a primitive string. Examples are preffered but links and documentation are also very much appreciated! :)
You can use the GetWindowText API function to get the text of an edit control.
For a window with title this function retrieves the title. For an edit control it retrieves the edit control's text. Just ignore any documentation saying that you're limited to 64K or so, if you encounter it (it was once that way).
To detect the button click, process the WM_COMMAND window message in your window procedure; it's sent to the parent window of the button. There is a more sophisticated approach based on reflecting the message back to button, which can then process it itself, and that approach is used in most higher level frameworks. But at the API level, just check WM_COMMAND in the parent window's window procedure.

Richedit Msftedit strange border

I get really strange behavior when I use more than one RichEdit control:
LoadLibrary("Msftedit.dll");
RichEdit = CreateWindow("RICHEDIT50W", "", ES_READONLY | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, 5, 370, 600, 300, hwnd, NULL, NULL, NULL);
RichEdit2 = CreateWindow("RICHEDIT50W", "", /*0x550081C4*/ 0x50810804, 610, 65, 600, 300, hwnd, NULL, NULL, NULL);
The first EditControl works as it should, however the second one has some strange border around it.
First I tried to use the same styles as for the first richedit, then I found out the style using Spy++.
Even if I have the same styles, I get different results, probably the same as if I used WS_EX_CLIENTEDGE extended style for the second richedit.
I even tried to free the library and load it again, but with the same result.
Thank You all in advance!
So I figured it out. I have no Idea why, but the second RichEdit is somehow getting the WS_EX_CLIENTEDGE style after is created, so I simply remove the style like so:
LONG lExStyle = GetWindowLong(RichEdit2, GWL_EXSTYLE);
lExStyle &= ~WS_EX_CLIENTEDGE;
SetWindowLong(RichEdit2, GWL_EXSTYLE, lExStyle);
Now the RichEdit looks the same as the first one. But I have really no idea, where the style comes from.

How to show caret in a disabled MFC CEdit control?

If an edit control is disabled, the caret disappears. that case, if the edit is displaying a long text, then it is not possible to see the rest of the words.With the caret i, we can do that.
I think you must use an enabled readonly edit control instead of a disabled one.
Check 'readonly' in the edit box property window or use ES_READONLY when calling Create.
Also, don't forget to check Auto HScroll (ES_AUTOHSCROLL) ( and AutoVScroll if multiline).
CEdit* pEdit = new CEdit;
pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER
| ES_AUTOHSCROLL | ES_READONLY, (10, 10, 100, 100), this, 1);