Files
2026-07-28 14:17:49 +08:00

321 lines
8.8 KiB
C

#include <stdio.h>
#include "SimpFont.h"
#include "c_WinApp.h"
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#define WINDOW_CLASS_NAME "SimpFont.Main"
static LOGFONT LogFont;
static HFONT TheFont;
static char aFaceName[80];
static TEXTMETRIC TextMets;
static char* FontChoice[]={
"Times New Roman",
"Arial",
"Symbol",
"StockFont"
};
typedef enum {
Roman, Swiss, Symbol, StockFont
}TChoice;
static TChoice Choice;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
static HFONT GetFont(int Escapement, char* Name) {
memset(&LogFont, 0, sizeof(LOGFONT));
LogFont.lfHeight = 37;
LogFont.lfWeight = 400;
LogFont.lfEscapement = Escapement;
LogFont.lfItalic = 1;
LogFont.lfUnderline = 1;
LogFont.lfOutPrecision = OUT_STROKE_PRECIS;
LogFont.lfClipPrecision = CLIP_STROKE_PRECIS;
LogFont.lfQuality = DEFAULT_QUALITY;
strcpy(LogFont.lfFaceName, Name);
if (TheFont) {
DeleteFont(TheFont);
}
TheFont = CreateFontIndirect(&LogFont);
return TheFont;
}
static char* GetType(char* s) {
strcpy(s, "Font Type:");
if ((TextMets.tmPitchAndFamily & TMPF_FIXED_PITCH)==0) {
strcat(s, "Default<>");
}
if ((TextMets.tmPitchAndFamily & TMPF_FIXED_PITCH)==TMPF_FIXED_PITCH) {
strcat(s, "Fixed<>");
}
if ((TextMets.tmPitchAndFamily & TMPF_VECTOR)==TMPF_VECTOR) {
strcat(s, "Vector<>");
}
if ((TextMets.tmPitchAndFamily & TMPF_TRUETYPE)==TMPF_TRUETYPE) {
strcat(s, "TrueType<>");
}
if ((TextMets.tmPitchAndFamily & TMPF_DEVICE)==TMPF_DEVICE) {
strcat(s, "Device<>");
}
if (strlen(s) > 99) {
s[strlen(s) - 3 ] = '\0';
}
return s;
}
static char* GetFamily(char* s) {
int R = TextMets.tmPitchAndFamily & 0xF0;
strcpy(s, "Family:");
if (R==FF_DONTCARE) strcat(s, "Don't Care or don't know");
if (R==FF_ROMAN) strcat(s, "Roman");
if (R==FF_SWISS) strcat(s, "Swiss");
if (R==FF_MODERN) strcat(s, "Modern");
if (R==FF_SCRIPT) strcat(s, "Script");
if (R==FF_DECORATIVE) strcat(s, "Decorative");
return s;
}
static char* GetCharSet(char* s) {
strcpy(s, "Char Set:");
if (TextMets.tmCharSet==ANSI_CHARSET) {
strcat(s, "Ansi");
}
if (TextMets.tmCharSet==DEFAULT_CHARSET) {
strcat(s, "Default");
}
if (TextMets.tmCharSet==SYMBOL_CHARSET) {
strcat(s, "Symbol");
}
if (TextMets.tmCharSet==OEM_CHARSET) {
strcat(s, "OEM");
}
return s;
}
static char* GetFontString(char* s, TEXTMETRIC TextMetric, char* FaceName) {
char szType[99];
char szFamily[99];
char szCharSet[99];
TextMets = TextMetric;
GetType(szType);
GetFamily(szFamily);
GetCharSet(szCharSet);
sprintf(s, "Font:%s\n"
"Height: %d\n"
"Ascent: %d\n"
"Descent: %d\n"
"AveCharW: %d\n"
"MaxCharW: %d\n"
"Weight: %d\n"
"Italic: %hd\n"
"Underlined: %d\n"
"%s\n"
"%s\n"
"%s", FaceName,
TextMets.tmHeight,
TextMets.tmAscent,
TextMets.tmDescent,
TextMets.tmAveCharWidth,
TextMets.tmMaxCharWidth,
TextMets.tmWeight,
TextMets.tmItalic,
TextMets.tmUnderlined,
szType,
szFamily,
szCharSet
);
return s;
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
c_WinGUI_EventHandlerDef(WM_DESTROY, Main_Destroy) {
if (TheFont) {
DeleteFont(TheFont);
}
PostQuitMessage(0);
}
c_WinGUI_EventHandlerDef(WM_CREATE, Main_Create) {
GetFont(0, "New Times Roman");
return TRUE;
}
c_WinGUI_EventHandlerDef(WM_COMMAND, Main_Command) {
char s[500];
switch (id) {
case CM_INFO: {
GetFontString(s, TextMets, aFaceName);
MessageBox(hwnd, s, "Font Info", MB_OK);
break;
}
case CM_ROMAN: {
Choice = Roman;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_SWISS: {
Choice = Swiss;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_SYMBOL: {
Choice = Symbol;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_ANSI_FIXED_FONT: {
if (TheFont) DeleteFont(TheFont);
TheFont = GetStockFont(ANSI_FIXED_FONT);
Choice = StockFont;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_ANSI_VAR_FONT: {
if (TheFont) DeleteFont(TheFont);
TheFont = GetStockFont(ANSI_VAR_FONT);
Choice = StockFont;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_DEVICE_DEFAULT_FONT: {
if (TheFont) DeleteFont(TheFont);
TheFont = GetStockFont(DEVICE_DEFAULT_FONT);
Choice = StockFont;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_OEM_FIXED_FONT: {
if (TheFont) DeleteFont(TheFont);
TheFont = GetStockFont(OEM_FIXED_FONT);
Choice = StockFont;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_SYSTEM_FONT: {
if (TheFont) DeleteFont(TheFont);
TheFont = GetStockFont(SYSTEM_FONT);
Choice = StockFont;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case CM_SYSTEM_FIXED_FONT: {
if (TheFont) DeleteFont(TheFont);
TheFont = GetStockFont(SYSTEM_FIXED_FONT);
Choice = StockFont;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
}
}
c_WinGUI_EventHandlerDef(WM_PAINT, Main_Paint) {
PAINTSTRUCT PaintStruct;
HFONT OldFont;
HDC PaintDC = BeginPaint(hwnd, &PaintStruct);
if (Choice==StockFont) {
OldFont = SelectFont(PaintDC, TheFont);
GetTextFace(PaintDC, sizeof(aFaceName), aFaceName);
GetTextMetrics(PaintDC, &TextMets);
SetTextColor(PaintDC, RGB(rand() % 255, rand()%255, rand()%255));
TextOut(PaintDC, 10, 10, aFaceName, strlen(aFaceName));
TextOut(PaintDC, 10, 30, "Stock Fonts", 11);
TextOut(PaintDC, 10, 50, "Ten Letters", 11);
SelectFont(PaintDC, OldFont);
}else {
for (int i=0; i<=3; i++) {
TheFont = GetFont(900 * i, FontChoice[Choice]);
OldFont = SelectFont(PaintDC, TheFont);
SetTextColor(PaintDC, RGB(rand() % 255, rand()%255, rand()%255));
TextOut(PaintDC, 200, 200, "Ahoy!", 5);
GetTextFace(PaintDC, sizeof(aFaceName), aFaceName);
GetTextMetrics(PaintDC, &TextMets);
SelectFont(PaintDC, OldFont);
}
TextOut(PaintDC, 10, 10, aFaceName, strlen(aFaceName));
}
EndPaint(hwnd, &PaintStruct);
}
c_WinGUI_EventMapBegin(WndProc)
c_WinGUI_EventMap(WM_CREATE, Main_Create);
c_WinGUI_EventMap(WM_DESTROY, Main_Destroy);
c_WinGUI_EventMap(WM_COMMAND, Main_Command);
c_WinGUI_EventMap(WM_PAINT, Main_Paint);
c_WinGUI_EventMapEnd()
static
BOOL Register(HINSTANCE hInstance) {
WNDCLASS WndClass;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = GetStockBrush(WHITE_BRUSH);
WndClass.lpszMenuName = "Menu";
WndClass.lpszClassName = WINDOW_CLASS_NAME;
return RegisterClass(&WndClass);
}
static
HWND Create(HINSTANCE hInstance, int nCmdShow) {
HWND hwnd = CreateWindow(WINDOW_CLASS_NAME, WINDOW_CLASS_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL
);
if (hwnd == NULL) {
return NULL;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return hwnd;
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
c_WinGUI_Main(){
MSG msg;
if (!hPrevInstance) {
if (!Register(hInstance)) {
return FALSE;
}
}
WinApp.hInstance = hInstance;
HWND MainHwnd = Create(hInstance, nShowCmd);
if (!MainHwnd) {
return FALSE;
}
WinApp.hwnd = MainHwnd;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}