📄 fontlist.c
字号:
//======================================================================
// FontList - Lists the available fonts in the system
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include "FontList.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("FontList");
HINSTANCE hInst; // Program instance handle
FONTFAMSTRUCT ffs[FAMILYMAX];
INT sFamilyCnt = 0;
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_PAINT, DoPaintMain,
WM_DESTROY, DoDestroyMain,
};
//======================================================================
//
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;
// Initialize application.
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 1;
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine,
int nCmdShow) {
HWND hWnd;
// Save program instance handle in global variable
hInst = hInstance;
// Create main window
hWnd = CreateWindow (szAppName, // Window class
TEXT("Font Listing"),// Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Font callback functions
//
//----------------------------------------------------------------------
// FontFamilyCallback - Callback function that enumerates the font
// families
//
int CALLBACK FontFamilyCallback (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
int rc = 1;
// Stop enumeration if array filled.
if (sFamilyCnt >= FAMILYMAX)
return 0;
// Copy face name of font.
lstrcpy (ffs[sFamilyCnt++].szFontFamily, lplf->lfFaceName);
return rc;
}
//----------------------------------------------------------------------
// EnumSingleFontFamily - Callback function that enumerates fonts
//
int CALLBACK EnumSingleFontFamily (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
PFONTFAMSTRUCT pffs;
pffs = (PFONTFAMSTRUCT) lParam;
pffs->nNumFonts++; // Increment count of fonts in family
return 1;
}
//----------------------------------------------------------------------
// PaintSingleFontFamily - Callback function that draws a font
//
int CALLBACK PaintSingleFontFamily (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
PPAINTFONTINFO ppfi;
TCHAR szOut[256];
INT nFontHeight, nPointSize;
HFONT hFont, hOldFont;
ppfi = (PPAINTFONTINFO) lParam; // Translate lParam into struct
// pointer.
// Create the font from the LOGFONT structure passed.
hFont = CreateFontIndirect (lplf);
// Select the font into the device context.
hOldFont = SelectObject (ppfi->hdc, hFont);
// Compute font size.
nPointSize = (lplf->lfHeight * 72) /
GetDeviceCaps(ppfi->hdc,LOGPIXELSY);
// Format string and paint on display.
wsprintf (szOut, TEXT("%s Point:%d"), lplf->lfFaceName,
nPointSize);
ExtTextOut (ppfi->hdc, 25, ppfi->yCurrent, 0, NULL,
szOut, lstrlen (szOut), NULL);
// Compute the height of the default font.
nFontHeight = lpntm->tmHeight + lpntm->tmExternalLeading;
// Update new draw point.
ppfi->yCurrent += nFontHeight;
// Deselect font and delete.
SelectObject (ppfi->hdc, hOldFont);
DeleteObject (hFont);
return 1;
}
//======================================================================
// Message handling procedures for MainWindow
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndCB;
HDC hdc;
INT i, rc;
// Create a command bar.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
//Enumerate the available fonts
hdc = GetDC (hWnd);
rc = EnumFontFamilies ((HDC)hdc, (LPTSTR)NULL,
FontFamilyCallback, 0);
for (i = 0; i < sFamilyCnt; i++) {
ffs[i].nNumFonts = 0;
rc = EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily,
EnumSingleFontFamily,
(LPARAM)(PFONTFAMSTRUCT)&ffs[i]);
}
ReleaseDC (hWnd, hdc);
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
TEXTMETRIC tm;
INT nFontHeight, i;
TCHAR szOut[256];
PAINTFONTINFO pfi;
// Adjust the size of the client rect to take into account
// the command bar height.
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
hdc = BeginPaint (hWnd, &ps);
// Get the height of the default font.
GetTextMetrics (hdc, &tm);
nFontHeight = tm.tmHeight + tm.tmExternalLeading;
// Initialize struct that is passed to enumerate function.
pfi.yCurrent = rect.top;
pfi.hdc = hdc;
for (i = 0; i < sFamilyCnt; i++) {
// Format output string and paint font family name.
wsprintf (szOut, TEXT ("Family: %s "),
ffs[i].szFontFamily);
ExtTextOut (hdc, 5, pfi.yCurrent, 0, NULL,
szOut, lstrlen (szOut), NULL);
pfi.yCurrent += nFontHeight;
// Enumerate each family to draw a sample of that font.
EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily,
PaintSingleFontFamily,
(LPARAM)&pfi);
}
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -