/************************************************************************/ /* fonttest.cpp - the simple font testing app. Creates many fonts with */ /* distinct properties available from CreateFontIndirect() */ /* (c) 2016 leha-bot. License: BSD. */ /************************************************************************/ #define _CRT_SECURE_NO_WARNINGS #include #include #include #include LRESULT CALLBACK FontWndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp); const TCHAR g_wndClassName[] = TEXT("FontTestWndClass"); ATOM RegisterWndClass(HINSTANCE hInst) { WNDCLASSEX wc = { sizeof(wc), /*cbSize*/ CS_VREDRAW | CS_HREDRAW, /*style*/ FontWndProc, /*lpfnWndProc*/ 0, 0, /*cbClsExtra, cbWndExtra*/ hInst, /*hInstance*/ LoadIcon(NULL, IDI_WINLOGO), LoadCursor(NULL, IDC_ARROW), (HBRUSH) (COLOR_WINDOW), NULL, /*lpszMenuName*/ g_wndClassName, }; return RegisterClassEx(&wc); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HWND hwnd; MSG msg; BOOL retcode; /* init toolbar stuff from comctl32.dll */ // InitCommonControls(); /* Register our window's class */ if (!RegisterWndClass(hInstance)) { return GetLastError(); } hwnd = CreateWindow(g_wndClassName, TEXT("Font Test Application"), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_VISIBLE, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, 0); ShowWindow(hwnd, SW_SHOWDEFAULT); while ( (retcode = GetMessage(&msg, hwnd, 0, 0)) != 0) { if (retcode == -1) { return GetLastError(); } else { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; } #if 0 // GUI-only structs. struct FontLabelInfo { int x, y; char *text; size_t len; LOGFONT font; HFONT hf; }; const size_t MAX_LABELS = 12; struct FontWndContext { FontLabelInfo labels[MAX_LABELS]; struct View { HWND hRebar; } ui; }; #endif struct CONST_TSTRING { TCHAR *str; size_t len; }; #define COUNTOF(arr) sizeof(arr) / sizeof(arr[0]) #define MAKE_TSTRING(quote) {TEXT(quote), COUNTOF(quote)} HFONT FAR PASCAL MyCreateFont(void) { CHOOSEFONT cf; LOGFONT lf; HFONT hfont; // Initialize members of the CHOOSEFONT structure. cf.lStructSize = sizeof(CHOOSEFONT); cf.hwndOwner = (HWND)NULL; cf.hDC = (HDC)NULL; cf.lpLogFont = &lf; cf.iPointSize = 0; cf.Flags = CF_SCREENFONTS; cf.rgbColors = RGB(0, 0, 0); cf.lCustData = 0L; cf.lpfnHook = (LPCFHOOKPROC)NULL; cf.lpTemplateName = (LPTSTR)NULL; cf.hInstance = (HINSTANCE)NULL; cf.lpszStyle = (LPTSTR)NULL; cf.nFontType = SCREEN_FONTTYPE; cf.nSizeMin = 0; cf.nSizeMax = 0; // Display the CHOOSEFONT common-dialog box. ChooseFont(&cf); // Create a logical font based on the user's // selection and return a handle identifying // that font. hfont = CreateFontIndirect(cf.lpLogFont); return (hfont); } LRESULT CALLBACK FontWndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { #if 0 HWND hRebar; REBARBANDINFO rbinfo = { sizeof(rbinfo), RBBIM_STYLE | RBBIM_TEXT, RBBS_FIXEDSIZE | RBBS_USECHEVRON, 0, 0, TEXT("Font Properties:") }; #endif const size_t MAX_WEIGHTS = 10; static const CONST_TSTRING fontLabels[MAX_WEIGHTS] = { MAKE_TSTRING("Tahoma Font Weight Don't Care (FW_DONTCARE, 0) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Thin (FW_THIN, 100) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Extra Light (FW_EXTRALIGHT 200) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Light (FW_LIGHT 300) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Normal (FW_NORMAL 400) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Medium (FW_MEDIUM 500) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Semibold (FW_SEMIBOLD 600) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Bold (FW_BOLD 700) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Extrabold (FW_EXTRABOLD 800) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), MAKE_TSTRING("Tahoma Font Weight Black (FW_BLACK 900) - Äóðîâ, Ñóåòà, ÐåàêòÎÑ"), }; static struct FontWndContext { HFONT hTahomas[MAX_WEIGHTS]; } wndctx; HDC hdc; PAINTSTRUCT ps; LOGFONT lf = { 0 }, lf2 = { 0 }; SIZE linesz; int off = 0; switch (msg) { case WM_CREATE: #if 0 hRebar = CreateWindow(REBARCLASSNAME, TEXT("Font Properties"), WS_CHILD | WS_VISIBLE | CCS_TOP, 0, 0, CW_USEDEFAULT, 40, hWnd, 0, GetModuleHandle(0), 0); SendMessage(hRebar, RB_INSERTBAND, -1, (LPARAM) &rbinfo); #endif _tcsncpy(lf.lfFaceName, TEXT("Tahoma Bold"), COUNTOF(lf.lfFaceName)); lf.lfCharSet = RUSSIAN_CHARSET; lf.lfHeight = 12; //wndctx.hTahomas[0] = MyCreateFont(); // lf.lfPitchAndFamily = DEFAULT_PITCH; for (size_t i = 0; i < MAX_WEIGHTS; i++) { lf.lfWeight = i * 100; wndctx.hTahomas[i] = /*CreateFont(lf.lfHeight, lf.lfWeight, lf.lfEscapement, lf.lfOrientation, lf.lfWeight, lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet, lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality, lf.lfPitchAndFamily, lf.lfFaceName);*/ CreateFontIndirect(&lf); GetObject(wndctx.hTahomas[i], sizeof(lf2), &lf2); } return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); SetBkMode(hdc, TRANSPARENT); SetGraphicsMode(hdc, GM_ADVANCED); for (size_t i = 0; i < MAX_WEIGHTS; i++) { SelectFont(hdc, wndctx.hTahomas[i]); GetObject(wndctx.hTahomas[i], sizeof(lf2), &lf2); GetTextExtentPoint32(hdc, fontLabels[i].str, fontLabels[i].len, &linesz); TextOut(hdc, 0, off, fontLabels[i].str, fontLabels[i].len - 1); { TCHAR many[100]; GetTextFace(hdc, 100, many); TextOut(hdc, linesz.cx, off, many, _tcslen(many)); } off += linesz.cy; } EndPaint(hWnd, &ps); return 0; case WM_DESTROY: for (size_t i = 0; i < MAX_WEIGHTS; i++) { DeleteFont(wndctx.hTahomas[i]); } return 0; default: return DefWindowProc(hWnd, msg, wp, lp); } }