263 lines
7.7 KiB
C
263 lines
7.7 KiB
C
#include <stdio.h>
|
|
|
|
#include "c_WinApp.h"
|
|
#include "FontsExp.h"
|
|
#include "FontStr.h"
|
|
#include "c_WinUI_Toast.h"
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
typedef struct {
|
|
int Count;
|
|
HWND hWindow;
|
|
}ENUMSTRUCT;
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
static TEXTMETRIC TextMetrics;
|
|
static char aTextFace[80];
|
|
|
|
static HFONT TheFont;
|
|
static LPLOGFONT TheLogFont;
|
|
|
|
static HWND hFontList;
|
|
static HWND hAlphaEdit;
|
|
static HWND hTrueType;
|
|
static HWND hNumFonts;
|
|
static HWND hFontName;
|
|
static HWND ButtonWindows[3];
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
#define WINDOW_CLASS_NAME "FontsExp.Main"
|
|
|
|
|
|
static
|
|
int CALLBACK DescribeFontCallback(LPENUMLOGFONT lpnlf, LPNEWTEXTMETRIC lpntm, int FontType, ENUMSTRUCT FAR * lpData);
|
|
|
|
|
|
static void HandleMetrics(TEXTMETRIC TextMetrics) {
|
|
if ((TextMetrics.tmPitchAndFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE) {
|
|
SendMessage(ButtonWindows[0], BM_SETCHECK, 1, 0);
|
|
}else {
|
|
SendMessage(ButtonWindows[0], BM_SETCHECK, 0, 0);
|
|
}
|
|
|
|
if ((TextMetrics.tmWeight > 600)) {
|
|
SendMessage(ButtonWindows[1], BM_SETCHECK, 1, 0);
|
|
}else {
|
|
SendMessage(ButtonWindows[1], BM_SETCHECK, 0, 0);
|
|
}
|
|
|
|
if ((TextMetrics.tmItalic)) {
|
|
SendMessage(ButtonWindows[2], BM_SETCHECK, 1, 0);
|
|
}else {
|
|
SendMessage(ButtonWindows[2], BM_SETCHECK, 0, 0);
|
|
}
|
|
|
|
}
|
|
|
|
static void ShowTheFont(HWND hwnd) {
|
|
// FONTENUMPROC lpfnEnumProc;
|
|
ENUMSTRUCT EnumStruct;
|
|
char lpszBuffer[150];
|
|
HFONT SaveIt;
|
|
char* Alpha="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
HDC DC = GetDC(hwnd);
|
|
|
|
LRESULT Index = SendMessage(hFontList, LB_GETCURSEL, 0, 0);
|
|
SendMessage(hFontList, LB_GETTEXT, (WPARAM)Index, (LPARAM)(lpszBuffer));
|
|
// lpfnEnumProc = (FONTENUMPROC)MakeProcInstance((FARPROC)DescribeFontCallback, WinApp.hInstance);
|
|
EnumStruct.Count = 0;
|
|
EnumStruct.hWindow = hwnd;
|
|
// EnumFontFamilies(DC, lpszBuffer, lpfnEnumProc, (LONG)&EnumStruct);
|
|
EnumFontFamilies(DC, lpszBuffer, (FONTENUMPROC)DescribeFontCallback, (LPARAM)&EnumStruct);
|
|
if (TheFont) {
|
|
SaveIt = SelectFont(DC, TheFont);
|
|
}
|
|
GetTextMetrics(DC, &TextMetrics);
|
|
HandleMetrics(TextMetrics);
|
|
GetTextFace(DC, 150, aTextFace);
|
|
if (TheFont) {
|
|
SelectFont(DC, SaveIt);
|
|
}
|
|
ReleaseDC(hwnd, DC);
|
|
SendMessage(hFontName, WM_SETTEXT, 0, (LPARAM)&lpszBuffer);
|
|
SendMessage(hFontName, WM_SETFONT, (WPARAM)TheFont, 0);
|
|
|
|
SendMessage(hAlphaEdit, WM_SETTEXT, 0, (LPARAM)Alpha);
|
|
SendMessage(hAlphaEdit, WM_SETFONT, (WPARAM)TheFont, 0);
|
|
|
|
c_WinUI_Toast_Show(WinApp.hInstance, TEXT("Ñ¡ÖÐ×ÖÌå"), lpszBuffer, 2000);
|
|
}
|
|
|
|
static
|
|
BOOL CALLBACK FontCallback(const LOGFONT * lpelf, const TEXTMETRIC* lpntm, DWORD FontType, ENUMSTRUCT FAR * lpData) {
|
|
SendMessage(lpData->hWindow, LB_ADDSTRING, 0, (LPARAM)lpelf->lfFaceName);
|
|
lpData->Count++;
|
|
return 1;
|
|
}
|
|
|
|
static
|
|
int CALLBACK DescribeFontCallback(LPENUMLOGFONT lpnlf, LPNEWTEXTMETRIC lpntm, int FontType, ENUMSTRUCT FAR * lpData) {
|
|
SendMessage(lpData->hWindow, WM_NEWFONT, 0, (LPARAM)&lpnlf->elfLogFont);
|
|
return 1;
|
|
}
|
|
|
|
c_WinGUI_EventHandlerDef(WM_DESTROY, Main_Destroy) {
|
|
if (TheFont) {
|
|
DeleteObject(TheFont);
|
|
}
|
|
PostQuitMessage(0);
|
|
}
|
|
|
|
c_WinGUI_EventHandlerDef(WM_CREATE, Main_Create) {
|
|
static char* Titles[] = {"TrueType", "Heavy", "Italic"};
|
|
hFontList = CreateWindow("listbox", NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD,
|
|
9, 30, 201, 180,
|
|
hwnd, (HMENU)(ID_LISTBOX), WinApp.hInstance, NULL);
|
|
hNumFonts = CreateWindow("static", NULL,
|
|
WS_CHILD | WS_VISIBLE | WS_BORDER,
|
|
10, 10, 200, 20,
|
|
hwnd, (HMENU)(-1), WinApp.hInstance, NULL);
|
|
hFontName = CreateWindow("edit", NULL,
|
|
WS_CHILD | ES_LEFT | WS_VISIBLE | WS_BORDER | ES_READONLY,
|
|
260, 10, 850, 70,
|
|
hwnd, (HMENU)(-1), WinApp.hInstance, NULL);
|
|
hAlphaEdit = CreateWindow("edit", NULL,
|
|
WS_CHILD | ES_LEFT | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_MULTILINE | WS_HSCROLL ,
|
|
260, 90, 850, 70,
|
|
hwnd, (HMENU)(-1), WinApp.hInstance, NULL);
|
|
for (int i=0; i<3; i++) {
|
|
ButtonWindows[i] = CreateWindow("button", Titles[i],
|
|
WS_CHILD|WS_VISIBLE|BS_CHECKBOX,
|
|
260 + (i*125), 180, 100, 35,
|
|
hwnd, (HMENU)(-1), WinApp.hInstance, NULL);
|
|
}
|
|
TheFont = 0;
|
|
PostMessage(hwnd, WM_STARTFONTS, 0, 0);
|
|
return TRUE;
|
|
}
|
|
|
|
c_WinGUI_EventHandlerDef(WM_COMMAND, Main_Command) {
|
|
char s[500];
|
|
switch (id) {
|
|
case ID_LISTBOX: {
|
|
if (codeNotify==LBN_SELCHANGE) {
|
|
ShowTheFont(hwnd);
|
|
}
|
|
break;
|
|
}
|
|
case CM_INFO: {
|
|
GetFontString(s, sizeof(s), TextMetrics, aTextFace);
|
|
MessageBox(hwnd, s, "Info", MB_OK);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
c_WinGUI_EventHandlerDef(WM_USERDEFINED, Main_StartFonts) {
|
|
char s[100];
|
|
HDC DC = GetDC(hwnd);
|
|
// FONTENUMPROC lpfnEnumProc = (FONTENUMPROC) MakeProcInstance((FARPROC)FontCallback, WinApp.hInstance);
|
|
ENUMSTRUCT EnumStruct;
|
|
EnumStruct.Count = 0;
|
|
EnumStruct.hWindow = hFontList;
|
|
// EnumFontFamilies(DC, NULL, lpfnEnumProc, (LONG)&EnumStruct);
|
|
// FreeProcInstance(lpfnEnumProc);
|
|
EnumFontFamilies(DC, NULL, (FONTENUMPROC)FontCallback, (LPARAM)&EnumStruct);
|
|
ReleaseDC(hwnd, DC);
|
|
|
|
snprintf(s, sizeof(s), "There are %d fonts available", EnumStruct.Count);
|
|
SetWindowText(hNumFonts, s);
|
|
SetFocus(hFontList);
|
|
SendMessage(hFontList, LB_SETCURSEL, 0, 0);
|
|
ShowTheFont(hwnd);
|
|
return 1;
|
|
}
|
|
|
|
c_WinGUI_EventHandlerDef(WM_USERDEFINED, Main_NewFont) {
|
|
if (TheFont) {
|
|
DeleteObject(TheFont);
|
|
}
|
|
TheLogFont = (LPLOGFONT) lParam;
|
|
TheFont = CreateFontIndirect(TheLogFont);
|
|
return 1;
|
|
}
|
|
|
|
c_WinGUI_EventMapBegin(WndProc)
|
|
c_WinGUI_EventMap(WM_DESTROY, Main_Destroy);
|
|
c_WinGUI_EventMap(WM_CREATE, Main_Create);
|
|
c_WinGUI_EventMap(WM_COMMAND, Main_Command);
|
|
c_WinGUI_UDEventMap(WM_STARTFONTS, Main_StartFonts);
|
|
c_WinGUI_UDEventMap(WM_NEWFONT, Main_NewFont);
|
|
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 = (HBRUSH)(COLOR_WINDOW + 1);
|
|
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;
|
|
}
|
|
|