ReactOS  0.4.14-dev-50-g13bb5e2
enumfonts.cpp
Go to the documentation of this file.
1 
2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 4 - Listing 4.2 (Font Enumeration Demo)
5 //
6 // Created by Damon Chandler <dmc27@ee.cornell.edu>
7 // Updates can be downloaded at: <www.coriolis.com>
8 //
9 // Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
10 // if you have any questions about this code.
11 // ------------------------------------------------------------------
12 
13 
14 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
15 #include <windows.h>
16 #include <cassert>
17 
18 
19 #ifndef SNDMSG
20 #define SNDMSG ::SendMessage
21 #endif /* ifndef SNDMSG */
22 
23 
24 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 
26 
28 const int ID_LISTBOX = 101;
29 
31 const char* WndClassName = "GMainWnd";
33  LPARAM lParam);
34 
35 
37  int nCmdShow)
38 {
39  hInst = hInstance;
40 
41  WNDCLASS wc;
42  memset(&wc, 0, sizeof(WNDCLASS));
43 
47  wc.hInstance = hInstance;
48  wc.hCursor = NULL; //LoadCursor(NULL, IDC_ARROW);
49  wc.hbrBackground = reinterpret_cast<HBRUSH>(
50  COLOR_BTNFACE + 1
51  );
52 
53  if (RegisterClass(&wc))
54  {
55  HWND hWnd =
57  WndClassName, TEXT("Font Enumeration Demo"),
60  CW_USEDEFAULT, CW_USEDEFAULT, 295, 285,
61  NULL, NULL, hInst, NULL
62  );
63 
64  if (hWnd)
65  {
66  ShowWindow(hWnd, nCmdShow);
68 
69  MSG msg;
70  while (GetMessage(&msg, NULL, 0, 0))
71  {
74  }
75  }
76  }
77  return 0;
78 }
79 //-------------------------------------------------------------------------
80 
81 
83  NEWTEXTMETRICEX* lpntme, int FontType, LPARAM lParam)
84 {
85  if (FontType == TRUETYPE_FONTTYPE)
86  {
87  // if the typeface name is not already in the list
88  LPARAM name = reinterpret_cast<LPARAM>(lpelfe->elfFullName);
90  {
91  // add each font to the list box
93 
94  // fix the size of the font
95  lpelfe->elfLogFont.lfHeight = -20;
96  lpelfe->elfLogFont.lfWidth = 0;
97 
98  // create a new font and store its handle
99  // as the data of the newly added item
102  reinterpret_cast<LPARAM>(hFont));
103  }
104  }
105  return TRUE;
106 }
107 //-------------------------------------------------------------------------
108 
109 
111 {
112  // grab a handle to the screen's DC
113  HDC hScreenDC = GetDC(NULL);
114  try
115  {
116  //
117  // NOTE: Windows 95, 98 and Me requires that the lpLogfont
118  // (second) parameter of the EnumFontFamiliesEx function is
119  // non-NULL. This parameter can be NULL on Windows NT/2000.
120  //
121  LOGFONT lf = {0};
123 
124  // enumerate the current screen fonts
126  hScreenDC, &lf,
128  0, 0
129  );
130  }
131  catch (...)
132  {
133  // release the screen's DC
134  ReleaseDC(NULL, hScreenDC);
135  }
136  // release the screen's DC
137  ReleaseDC(NULL, hScreenDC);
138 }
139 //-------------------------------------------------------------------------
140 
141 
143  LPARAM lParam)
144 {
145  switch (msg)
146  {
147  case WM_CREATE:
148  {
149  hListBox =
151  WS_EX_CLIENTEDGE, TEXT("LISTBOX"), TEXT(""),
154  20, 10, 250, 250,
155  hWnd, reinterpret_cast<HMENU>(ID_LISTBOX),
156  hInst, NULL
157  );
158  if (hListBox)
159  {
160  AddScreenFonts();
161  }
162  }
163  case WM_MEASUREITEM:
164  {
165  // grab a pointer to the MEASUREITEMSTRUCT structure
166  LPMEASUREITEMSTRUCT lpmis =
167  reinterpret_cast<LPMEASUREITEMSTRUCT>(lParam);
168 
169  // test the identifier of the control that
170  // the message is meant for
171  if ((int)lpmis->CtlID == ID_LISTBOX)
172  {
173  // adjust the height
174  lpmis->itemHeight = 25;
175 
176  return TRUE;
177  }
178  break;
179  }
180  case WM_DRAWITEM:
181  {
182  // grab a pointer to the DRAWITEMSTRUCT structure
183  LPDRAWITEMSTRUCT lpdis =
184  reinterpret_cast<LPDRAWITEMSTRUCT>(lParam);
185 
186  // test the identifier of the control that
187  // the message is meant for
188  if ((int)lpdis->CtlID == ID_LISTBOX)
189  {
190  COLORREF OldColor = GetTextColor(lpdis->hDC);
191  int stock = WHITE_BRUSH;
192 
193  // if the item is currently selected
194  if (lpdis->itemState & ODS_SELECTED)
195  {
196  // set the text color to white and
197  // the background color to black
198  COLORREF clrWhite = PALETTERGB(255, 255, 255);
199  OldColor = SetTextColor(lpdis->hDC, clrWhite);
200  stock = BLACK_BRUSH;
201  }
202  FillRect(
203  lpdis->hDC, &lpdis->rcItem,
204  static_cast<HBRUSH>(GetStockObject(stock))
205  );
206 
207  if ((int)lpdis->itemID != -1)
208  {
209  // extract the item's text
210  char text[MAX_PATH];
211  SNDMSG(hListBox, LB_GETTEXT, lpdis->itemID,
212  reinterpret_cast<LPARAM>(text));
213 
214  // extract the corresponding font handle
215  DWORD value =
216  SNDMSG(hListBox, LB_GETITEMDATA, lpdis->itemID, 0);
217  HFONT hFont = reinterpret_cast<HFONT>(value);
218 
219  // select the corresponding font
220  // into the device context
221  HFONT HOldFont = static_cast<HFONT>(
222  SelectObject(lpdis->hDC, hFont)
223  );
224 
225  // render the text transparently
226  SetBkMode(lpdis->hDC, TRANSPARENT);
227 
228  // render the text
229  RECT RText = lpdis->rcItem;
230  InflateRect(&RText, -2, -2);
231  DrawText(lpdis->hDC, text, strlen(text), &RText,
233 
234  // restore the previous font
235  SelectObject(lpdis->hDC, HOldFont);
236  }
237 
238  // render the focus rectangle
239  if (lpdis->itemState & ODS_FOCUS)
240  {
241  DrawFocusRect(lpdis->hDC, &lpdis->rcItem);
242  }
243 
244  // restore the previous color/mode
245  SetTextColor(lpdis->hDC, OldColor);
246  SetBkMode(lpdis->hDC, OPAQUE);
247 
248  return TRUE;
249  }
250  break;
251  }
252  case WM_DESTROY:
253  {
254  // delete each created font object
255  int count = SNDMSG(hListBox, LB_GETCOUNT, 0, 0);
256  for (int index = 0; index < count; ++index)
257  {
259  DeleteObject(reinterpret_cast<HFONT>(value));
260  }
261 
262  PostQuitMessage(0);
263  break;
264  }
265  }
266  return DefWindowProc(hWnd, msg, wParam, lParam);
267 }
268 //-------------------------------------------------------------------------
269 
270 
271 
HGDIOBJ WINAPI GetStockObject(_In_ int)
#define DispatchMessage
Definition: winuser.h:5666
#define SNDMSG
Definition: enumfonts.cpp:20
BOOL WINAPI TranslateMessage(_In_ const MSG *)
BOOL WINAPI InflateRect(_Inout_ LPRECT, _In_ int, _In_ int)
#define TRUE
Definition: types.h:120
#define LB_FINDSTRINGEXACT
Definition: winuser.h:2017
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow)
Definition: enumfonts.cpp:36
#define CreateWindow
Definition: winuser.h:5655
#define LB_ADDSTRING
Definition: winuser.h:2013
HDC WINAPI GetDC(_In_opt_ HWND)
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
const WCHAR * text
Definition: package.c:1827
#define LB_GETITEMDATA
Definition: winuser.h:2023
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define LBS_HASSTRINGS
Definition: pedump.c:684
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
BOOL WINAPI DrawFocusRect(_In_ HDC, _In_ LPCRECT)
static HDC
Definition: imagelist.c:92
#define CALLBACK
Definition: compat.h:27
BOOL WINAPI UpdateWindow(_In_ HWND)
HWND hWnd
Definition: settings.c:17
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
#define ODS_FOCUS
Definition: winuser.h:2524
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define CS_HREDRAW
Definition: winuser.h:648
#define WHITE_BRUSH
Definition: wingdi.h:901
BYTE lfCharSet
Definition: dimm.idl:50
UINT_PTR WPARAM
Definition: windef.h:207
HBRUSH hbrBackground
Definition: winuser.h:3144
#define WS_CHILD
Definition: pedump.c:617
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define WS_CLIPCHILDREN
Definition: pedump.c:619
#define DEFAULT_CHARSET
Definition: wingdi.h:383
BYTE elfFullName[LF_FULLFACESIZE]
Definition: wingdi.h:2692
WPARAM wParam
Definition: combotst.c:138
#define LBS_STANDARD
Definition: winuser.h:321
HFONT hFont
Definition: main.c:53
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define DrawText
Definition: winuser.h:5672
#define LBS_OWNERDRAWFIXED
Definition: pedump.c:682
int WINAPI SetBkMode(_In_ HDC, _In_ int)
Definition: dc.c:1032
HINSTANCE hInstance
Definition: charmap.c:20
#define DT_LEFT
Definition: winuser.h:534
#define LB_ERR
Definition: winuser.h:2407
LONG lfHeight
Definition: dimm.idl:42
const int ID_LISTBOX
Definition: enumfonts.cpp:28
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
HCURSOR hCursor
Definition: winuser.h:3143
#define CreateWindowEx
Definition: winuser.h:5656
#define TRANSPARENT
Definition: wingdi.h:949
#define CW_USEDEFAULT
Definition: winuser.h:225
COLORREF WINAPI GetTextColor(_In_ HDC)
Definition: text.c:831
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
GLuint index
Definition: glext.h:6031
#define DT_VCENTER
Definition: winuser.h:543
#define LB_GETCOUNT
Definition: winuser.h:2020
#define DefWindowProc
Definition: ros2win.h:31
#define WM_DESTROY
Definition: winuser.h:1591
#define BLACK_BRUSH
Definition: wingdi.h:895
#define WS_CAPTION
Definition: pedump.c:624
#define CS_VREDRAW
Definition: winuser.h:653
#define WS_OVERLAPPED
Definition: pedump.c:615
DWORD COLORREF
Definition: windef.h:285
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define WM_MEASUREITEM
Definition: winuser.h:1628
LONG lfWidth
Definition: dimm.idl:43
#define LB_SETITEMDATA
Definition: winuser.h:2047
#define index(s, c)
Definition: various.h:29
#define OPAQUE
Definition: wingdi.h:948
#define LB_GETTEXT
Definition: winuser.h:2031
#define COLOR_BTNFACE
Definition: winuser.h:918
LPCSTR lpszClassName
Definition: winuser.h:3146
UINT style
Definition: winuser.h:3137
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: enumfonts.cpp:142
COLORREF WINAPI SetTextColor(_In_ HDC, _In_ COLORREF)
Definition: text.c:888
#define DT_SINGLELINE
Definition: winuser.h:540
#define TEXT(s)
Definition: k32.h:26
#define WS_EX_CLIENTEDGE
Definition: winuser.h:384
HINSTANCE hInstance
Definition: winuser.h:3141
#define PALETTERGB(r, g, b)
Definition: wingdi.h:2937
#define CreateFontIndirect
Definition: wingdi.h:4439
unsigned int UINT
Definition: ndis.h:50
#define EnumFontFamiliesEx
Definition: wingdi.h:4446
#define GetMessage
Definition: winuser.h:5691
int CALLBACK MyEnumFontFamExProc(ENUMLOGFONTEX *lpelfe, NEWTEXTMETRICEX *lpntme, int FontType, LPARAM lParam)
Definition: enumfonts.cpp:82
WNDPROC lpfnWndProc
Definition: winuser.h:3138
#define WM_DRAWITEM
Definition: winuser.h:1627
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define msg(x)
Definition: auth_time.c:54
FONTENUMPROCA FONTENUMPROC
Definition: wingdi.h:2897
Definition: name.c:36
#define ODS_SELECTED
Definition: winuser.h:2520
#define WM_CREATE
Definition: winuser.h:1590
void AddScreenFonts()
Definition: enumfonts.cpp:110
unsigned int ULONG
Definition: retypes.h:1
#define RegisterClass
Definition: winuser.h:5737
LONG_PTR LRESULT
Definition: windef.h:209
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
#define WS_VISIBLE
Definition: pedump.c:620
#define WS_SYSMENU
Definition: pedump.c:629
#define memset(x, y, z)
Definition: compat.h:39
HWND hListBox
Definition: enumfonts.cpp:27
LPARAM lParam
Definition: combotst.c:139
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
HINSTANCE hInst
Definition: enumfonts.cpp:30
#define APIENTRY
Definition: api.h:79
LOGFONTA elfLogFont
Definition: wingdi.h:2691
const char * WndClassName
Definition: enumfonts.cpp:31
#define TRUETYPE_FONTTYPE
Definition: wingdi.h:1108