如何在运行时将编辑风格改为ES_NUMBER?

How to change the edit style to ES_NUMBER at run time?

创建编辑控件时,我没有添加 ES_NUMBER。后来基于布尔标志,我想更改样式并使其成为 ES_NUMBER 并将其恢复为布尔标志的另一个值。

documentation 有答案。部分摘录:

To create an edit control using the CreateWindow or CreateWindowEx function, specify the EDIT class, appropriate window style constants, and a combination of the following edit control styles. After the control has been created, these styles cannot be modified, except as noted.

因此,我们可能会也可能不会在创建控件后更改样式。让我们看看:

ES_NUMBER

Allows only digits to be entered into the edit control. Note that, even with this set, it is still possible to paste non-digits into the edit control.

To change this style after the control has been created, use SetWindowLong.

To translate text that was entered into the edit control to an integer value, use the GetDlgItemInt function. To set the text of the edit control to the string representation of a specified integer, use the SetDlgItemInt function.

要添加样式,请执行以下操作:

LONG style = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, style | ES_NUMBER);

或删除它:

LONG style = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, style & ~ES_NUMBER);