我无法更改文字的字体

时间:2019-04-05 19:02:10

标签: c winapi fonts

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

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

但是我收到一些奇怪的错误。奇怪,因为Internet上的每个人都使用此方法,而且显然每个人都很好。这是来自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);
                           ^~~~~~~~~~~~

有哪些方法可以解决此问题?

1 个答案:

答案 0 :(得分:1)

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

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

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