c - 我无法更改文本的字体
问题描述
我想更改用户在文本字段中输入的文本的字体。这是我的代码的路径:
HWND Edit = CreateWindowEx(...);
HFONT hfont = CreateFont(12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "MS Sans Serif");
SendMessage(Edit, WM_SETFONT, WPARAM(hfont), TRUE);
但我得到了一些奇怪的错误。奇怪,因为互联网上的每个人都使用这种方法,显然每个人都很好。这是来自 gcc 的错误消息:
C:\Users\Admin\Desktop\winapi>gcc main.c
main.c: In function 'WinMain':
main.c:86:32: error: expected expression before 'WPARAM'
SendMessage(Edit, WM_SETFONT, WPARAM(hfont), TRUE);
^~~~~~
In file included from c:\mingw\include\windef.h:42:0,
from c:\mingw\include\windows.h:42,
from main.c:1:
main.c:86:2: error: too few arguments to function 'SendMessageA'
SendMessage(Edit, WM_SETFONT, WPARAM(hfont), TRUE);
^
In file included from c:\mingw\include\windows.h:48:0,
from main.c:1:
c:\mingw\include\winuser.h:4157:27: note: declared here
WINUSERAPI LRESULT WINAPI SendMessageA (HWND, UINT, WPARAM, LPARAM);
^~~~~~~~~~~~
有什么方法可以解决这个问题?
解决方案
这是 C++ 和 C 之间的区别,在 C 中它需要是:
SendMessage(Edit, WM_SETFONT, (WPARAM)hfont, TRUE);
即使在呈现 winapi 代码时,大多数非 Microsoft 示例也可能假定使用 C++ 编译。
推荐阅读
- python - 如何在 python/Padas 中获取二维 df 中每一列中连续正数的计数
- serverless-framework - Mac 上的 Serverless DynamoDB Local 中出现 JNI 错误
- python - 例外:未找到 settings.yml
- c# - 本地键盘挂钩终止目标进程
- javascript - 有没有办法用替换功能替换多个字符?
- pyspark - databricks 分区日期按周
- jquery - ASP.NET Core MVC 中的动态控件创建问题
- github-actions - Chocolatey 无法在 GitHub Actions 中安装 visualstudio2017-workload-vctools
- python - 在 python 中加速 pd.read_excel
- oracle - Oracle DB:使用联合时的数据类型转换问题