c++ - 如何在运行时将编辑样式更改为 ES_NUMBER?

标签 c++ windows winapi

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

最佳答案

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

关于c++ - 如何在运行时将编辑样式更改为 ES_NUMBER?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181388/

相关文章:

windows - 获取给定 Windows 消息标识符的数值

winapi - 删除临时文件时Win32文件权限共享冲突

c - 进程是从其父进程继承其环境变量还是从 Windows 获取它们?

c++ - 从迭代器中的函数指针调用函数

c++ - 如何测试 std::random_device 的随机性?

Windows mongodb 服务器 bind_ip 配置

java - 通过 CreateProcess() 启动的 JVM 丢失类路径库

c++ - 模拟按住一个键

c++ - 为运算符方法编写实现

c++ - 在 C++ 中求值是什么意思?