Search code examples
visual-c++fontsvisual-studio-2019gdi+common-controls

How to change the font style of the text of a common control like button, label, etc. in c++ using GDI+?


I am trying to change the font style of a button using GDI+ but I don't know how to do it.

My button -

HWND btn = CreateWindow(L"BUTTON", L"My button", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 50, 100, 300, hWnd, NULL, NULL, NULL);

I have initialized GDI+ -

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

And also Shutdown it -

GdiplusShutdown(gdiplusToken);

Any help please


Solution

  • Common controls don't use GDI+. You need use LOGFONT.

    HWND TextBox;
    TextBox = CreateWindowW(WC_EDIT, L"", WS_BORDER | WS_VISIBLE | WS_CHILD, 330, 130, 300, 100, hWnd, NULL, NULL, NULL);
    
    LOGFONT logfont; 
    ZeroMemory(&logfont, sizeof(LOGFONT));
    
    logfont.lfCharSet = DEFAULT_CHARSET; //Font
    logfont.lfHeight = 20; //Font Height
    logfont.lfWidth = 23; //Font Width
    logfont.lfWeight = 300; //Font Weight
    
    //If want
    logfont.lfItalic = true; // Italic (Boolean)
    logfont.lfUnderline = true; // Underline (Boolean)
    
    
    HFONT hFont = CreateFontIndirect(&logfont);   
    SendMessage(TextBox, WM_SETFONT, (WPARAM)hFont, TRUE);