Change font size of TextOut()

Hi there, do anyone know how to change the font size of TextOut()? :)
Use CreateFont() or CreateFontIndirect(), and then select the newly-created font with SelectObject() into the device context before calling TextOut().
...and finally, don't forget to SelectObject() the old font back into the device context, and DeleteObject() your new FONT out of existence so you don't have a memory leak.
Thanks, I managed to create and use the new font, but how do I select the old font?
Fonts are GDI objects (as are brushes, pens, regions, etc.) and need to be selected into the Device Context with SelectObject(), as you've successfully done already, apparently. The return value from SelectObject is the object which was removed from the Device Context to make room for whatever you just selected into it. The typical drill is to save that object into a temporary (stack) variable, then when you are finished drawing with your modified device context, re-select the old object back in.

Here is my colorful version of a Hello, World! program that creates a bright red background and writes "Hello, World!" with a very large white font. Check out the fnWndProc_OnPaint() handler and you'll see what I've done. All you GDI experts out there please examine what I've done and let both Kuzco and me know if I did anything wrong!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//HelloWorld.h

typedef struct    WindowsEventArguments
{
 HWND             hWnd;
 WPARAM           wParam;
 LPARAM           lParam;
 HINSTANCE        hIns;
}WndEventArgs,    *lpWndEventArgs;


struct EVENTHANDLER
{
 unsigned int     Code;
 long             (*fnPtr)(lpWndEventArgs);
};



//Main Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//Main.cpp
#include <windows.h>
#include "HelloWorld.h"
EVENTHANDLER  EventHandler[2];


long fnWndProc_OnPaint(lpWndEventArgs Wea)
{
 HFONT hFont,hTmp;
 PAINTSTRUCT ps;
 HBRUSH hBrush;
 RECT rc;
 HDC hDC;

 hDC=BeginPaint(Wea->hWnd,&ps);
 GetClientRect(Wea->hWnd,&rc);
 hBrush=CreateSolidBrush(RGB(255,0,0));
 FillRect(hDC,&rc,hBrush);
 DeleteObject(hBrush);
 SetTextColor(hDC,RGB(255,255,255));
 hFont=CreateFont(80,0,0,0,FW_BOLD,0,0,0,0,0,0,2,0,"SYSTEM_FIXED_FONT");
 hTmp=(HFONT)SelectObject(hDC,hFont);
 SetBkMode(hDC,TRANSPARENT);
 DrawText(hDC,"Hello, World!",-1,&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
 DeleteObject(SelectObject(hDC,hTmp));
 EndPaint(Wea->hWnd,&ps);

 return 0;
}


long fnWndProc_OnClose(lpWndEventArgs Wea)
{
 DestroyWindow(Wea->hWnd);
 PostQuitMessage(0);

 return 0;
}


void AttachEventHandlers(void)      //This procedure maps windows messages to the
{                                   //procedure which handles them.
 EventHandler[0].Code=WM_PAINT,     EventHandler[0].fnPtr=fnWndProc_OnPaint;
 EventHandler[1].Code=WM_CLOSE,     EventHandler[1].fnPtr=fnWndProc_OnClose;
}


long __stdcall fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
 WndEventArgs Wea;                  //This procedure loops through the EVENTHANDER array
                                    //of structs to try to make a match with the msg parameter
 for(unsigned int i=0; i<2; i++)    //of the WndProc.  If a match is made the event handling
 {                                  //procedure is called through a function pointer -
     if(EventHandler[i].Code==msg)  //(EventHandler[i].fnPtr).  If no match is found the
     {                              //msg is passed onto DefWindowProc().
        Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
        return (*EventHandler[i].fnPtr)(&Wea);
     }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int __stdcall WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 char szClassName[]="Form5";
 WNDCLASSEX wc;
 MSG messages;
 HWND hWnd;

 AttachEventHandlers();
 wc.lpszClassName=szClassName;                            wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);                           wc.style=CS_HREDRAW|CS_VREDRAW;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);                 wc.hInstance=hIns;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);              wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH),    wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                                    wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,100,550,500,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
       TranslateMessage(&messages);
       DispatchMessage(&messages);
 }

 return messages.wParam;
}
Last edited on
Topic archived. No new replies allowed.