Hi,
I am using Dev-C++ with g++ compiler.
I recently made a DLL in c++ that shows a window using Windows API. I discovered that the text shown inside are large and bold, which aren't looking very good. I want to change this, so I used the following code (similar):

#include <windows.h>

//... sonething else ...

HFONT hf = CreateFont(nHeight,
    nWidth,
    nEscapement,
    nOrientation,
    fnWeight,
    fdwItalic,
    fdwUnderline,
    fdwStrikeOut,
    fdwCharSet,
    fdwOutputPrecision,
    fdwClipPrecision,
    fdwQuality,
    fdwPitchAndFamily,
    lpszFace
);

SendDlgItemMessage(myHwnd,
    IDC_TEST,
    WM_SETFONT,
    (WPARAM) hf,
    (LPARAM) true
);
//... other stuff ...

I got this error:

[Linker error] undefined reference to `CreateFontA@56'

That seems not the problem of my source, but a problem around wingdi.h and libgdi32.a. I've searched through G**gle but I can't find one suitable solution.

May I use this code but be able to link it? Or is there other solutions to change the font? Hopefully I wouldn't have to get a new library.

Could anyone help, I would be thankful.

Thanks,
alvinwong

Dani AI

Generated

Good catch by : that error is purely a link step issue, not your code. On Windows the GDI font APIs live in Gdi32, and CreateFont is documented to require linking against Gdi32 (MinGW’s import library is libgdi32.a). The dialog/control messaging you use to apply the font comes from User32 and requires User32 as well. Including wingdi.h does not pull in those import libraries; you must add them to the link inputs for your DLL or EXE. (learn.microsoft.com)

Two practical tips once you are linking correctly:

  • To match the OS look, do not hardcode a face/size. Either grab the stock UI font or, better, ask Windows for the current message/dialog font via SystemParametersInfo(SPI_GETNONCLIENTMETRICS) and pass that LOGFONT to CreateFontIndirect. Microsoft explicitly notes that DEFAULT_GUI_FONT from GetStockObject is not recommended and points you to SPI_GETNONCLIENTMETRICS. (learn.microsoft.com)
  • Manage lifetime: keep the HFONT alive as long as any control is using it and delete it when the window/dialog is destroyed (DeleteObject). If you free it too early, controls will fall back to a default or render incorrectly. (learn.microsoft.com)

If you ever wire this up from code rather than the resource editor, remember that applying a font is per-control: send the message to each control you want to restyle (the function you used is from User32, which is why that library is needed). (learn.microsoft.com)

Recommended Answers

All 3 Replies

I did a quick google search and found the answer on the GameDev.Net forums. You need to link with the following:
-lwinmm
-lgdi32

Wow! I don't understand. Why I need to use extra library? I thought I have already had libgdi32.a and wingdi32.h?

I only have these two files that contains "gdi". Where am I be able to get it work? I am not familiar to the linker...

Oh, well, I finally managed to do it.

I am creating a DLL, so that some windows library aren't included automatically.

To tell others how to do this, if you are using Dev-C++, click "Project"->"Project Options"->"Parameters" tab->"Add Library or Object"->select the library file (located at <devcpp's dir>\lib, usually similar to the header file: eg. wingdi.h->libgdi32.a)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.