首页 > 解决方案 > 我无法更改文本的字体

问题描述

我想更改用户在文本字段中输入的文本的字体。这是我的代码的路径:

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

有什么方法可以解决这个问题?

标签: cwinapifonts

解决方案


这是 C++ 和 C 之间的区别,在 C 中它需要是:

SendMessage(Edit, WM_SETFONT, (WPARAM)hfont, TRUE);

即使在呈现 winapi 代码时,大多数非 Microsoft 示例也可能假定使用 C++ 编译。


推荐阅读