📄 showhz.cpp
字号:
// showHz.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "showHz.h"
#include <windows.h>
#include <stdio.h>
#include "resource.h"
#include "Commdlg.h"
#define MAX_LOADSTRING 100
#define HZ_NUM 10000
#define HZ_WITH 240
#define HZ_WIDTH 96
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
BYTE Hz_Code[HZ_WITH * HZ_WITH]={0};
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
/*-----------------------------------------
CHOSFONT.C -- ChooseFont Demo
(c) Charles Petzold, 1998
-----------------------------------------*/
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void GetBmp(HDC hdc ,int px,int py);
bool WriteFile(int);
BYTE hz[HZ_WIDTH * HZ_WIDTH];
COLORREF BkColor;
int hz_wide=HZ_WIDTH;
BYTE type=0;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("CHOSFONT") ;
HWND hwnd ;
MSG msg ;
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) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("字库生成"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static CHOOSEFONT cf;
static int cyChar ,hz_code=0;
static LOGFONT lf ;
static bool bFontSet = false;
static bool bBuild = false;
static bool bDraw = false;
static bool bSave = false;
HDC hdc ;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;
static TCHAR tText[] =TEXT ("240点字体选黑体,大小为180\0 ");
switch (message)
{
case WM_CREATE:
// Get text height
cyChar = HIWORD (GetDialogBaseUnits ()) ;
// Initialize the LOGFONT structure
GetObject (GetStockObject (SYSTEM_FONT), sizeof (lf), &lf) ;
// Inialize the CHOOSEFONT structure
cf.lStructSize = sizeof (CHOOSEFONT) ;
cf.hwndOwner = hwnd ;
cf.hDC = NULL ;
cf.lpLogFont = &lf ;
cf.iPointSize = 0 ;
cf.Flags = CF_INITTOLOGFONTSTRUCT |
CF_SCREENFONTS | CF_EFFECTS ;
cf.rgbColors = 0 ;
cf.lCustData = 0 ;
cf.lpfnHook = NULL ;
cf.lpTemplateName = NULL ;
cf.hInstance = NULL ;
cf.lpszStyle = NULL ;
cf.nFontType = 0 ;
cf.nSizeMin = 0 ;
cf.nSizeMax = 0 ;
return 0 ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDM_FONT:
if (ChooseFont(&cf))
{
bFontSet=true;
hz_wide=cf.lpLogFont->lfHeight;
if(hz_wide<0)hz_wide=-hz_wide;
bDraw = true;
InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
case ID_UP:
if( bFontSet)
{
if(hz_code>0)hz_code--;
bDraw = true;
}
else
{
ChooseFont(&cf);
hz_wide=cf.lpLogFont->lfHeight;
if(hz_wide<0)hz_wide=-hz_wide;
bFontSet=true;
}
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case ID_DOWN:
if( bFontSet)
{
if(hz_code<lstrlen(szText))hz_code++;
bDraw = true;
}
else
{
ChooseFont(&cf);
hz_wide=cf.lpLogFont->lfHeight;
if(hz_wide<0)hz_wide=-hz_wide;
bFontSet=true;
}
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case ID_SAVE:
bSave=true;
bDraw = true;
InvalidateRect (hwnd, NULL, true) ;
return 0 ;
case ID_BUILD:
if( bFontSet)
{
hz_code=0;
bBuild=true;
bDraw = true;
bSave=true;
}
else
{
ChooseFont(&cf);
hz_wide=cf.lpLogFont->lfHeight;
if(hz_wide<0)hz_wide=-hz_wide;
bFontSet=true;
}
InvalidateRect (hwnd, NULL, TRUE) ;
return 0;
case ID_CONTINUE:
if (1)
{
bBuild=true;
bSave=true;
InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
case ID_PAUSE:
if (1)
{
bBuild=false;
InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, About);
break;
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
// Display sample text using selected font
if(bDraw)
{
//Rectangle(hdc,0,0,100,100);
//TextOut (hdc, 0, 0, tText, 16) ;
SelectObject (hdc, CreateFontIndirect (&lf)) ;
GetTextMetrics (hdc, &tm) ;
SetTextColor (hdc, cf.rgbColors) ;
TextOut (hdc, 0, 0, &szText[hz_code], 1) ;
bDraw = false;
BkColor = GetBkColor(hdc);
GetBmp(hdc, 0, 0);
if(bSave)WriteFile(hz_code);
bSave=false;
DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
}
EndPaint (hwnd, &ps) ;
if(bBuild)
{
if(hz_code<lstrlen(szText))
{
hz_code++;
bDraw = true;
bSave = true;
InvalidateRect (hwnd, NULL, TRUE) ;
}
else
{
bBuild = false;
}
}
return 0 ;
case WM_LBUTTONDOWN:
if(hz_code<lstrlen(szText))
hz_code++;
else hz_code=0;
InvalidateRect (hwnd, NULL, TRUE);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static BYTE chose=0;
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK )
{
type=chose;
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDC_TYPE1)
{
chose=1;
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDC_TYPE2)
{
chose=2;
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDC_TYPE3)
{
chose=3;
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDC_TYPE0)
{
chose=0;
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void GetBmp(HDC hdc ,int px,int py)
{
int x,y;
COLORREF HzColor;
for(x=0;x<hz_wide;x++)
{
for(y=0;y<hz_wide;y++)
{
HzColor=GetPixel(hdc, px+x, py+y );
if(HzColor != BkColor)
hz[ y * HZ_WIDTH + x]=77;
else hz[ y * HZ_WIDTH + x]=46;
}
}
}
bool WriteFile(int count)
{
FILE *fp;
FILE *data;
int i,j;
BYTE *temp=hz;
BYTE tOut;
int wm,qm,hzc;
hzc=count;;
wm=hzc%94;
qm=hzc/94;
qm=qm+0xa1;
wm=wm+0xa1;
hzc=qm*256+wm;
if((fp=fopen("hzc.txt","a"))==NULL)
{
return false;
}
fprintf(fp,"/*\"%c%c\" height = %d*/\n",qm,wm,hz_wide);
for(i=0;i<hz_wide;i++)
{
fwrite(temp,hz_wide,1,fp);
temp+=HZ_WIDTH;
fprintf(fp,"\n");
}
fprintf(fp,"\n");
fclose(fp);
temp=hz;
if((data=fopen("hzc.h","a"))==NULL)
{
return false;
}
fprintf(data,"\n\t/* 0x%x \"%c%c\" */\n",hzc,qm,wm);
for(i=0;i<hz_wide;i++)
{
fprintf(data,"\t");
for(j=0;j<hz_wide;j+=8)
{
tOut=0;
if(type==0)
{
if(temp[j] == 77)tOut=128;
if(temp[j+1] == 77)tOut+=64;
if(temp[j+2] == 77)tOut+=32;
if(temp[j+3] == 77)tOut+=16;
if(temp[j+4] == 77)tOut+=8;
if(temp[j+5] == 77)tOut+=4;
if(temp[j+6] == 77)tOut+=2;
if(temp[j+7] == 77)tOut+=1;
}
else if(type==1)
{
if(temp[j] == 77)tOut=1;
if(temp[j+1] == 77)tOut+=2;
if(temp[j+2] == 77)tOut+=4;
if(temp[j+3] == 77)tOut+=8;
if(temp[j+4] == 77)tOut+=16;
if(temp[j+5] == 77)tOut+=32;
if(temp[j+6] == 77)tOut+=64;
if(temp[j+7] == 77)tOut+=128;
}
else if(type==2)
{
if(temp[j*HZ_WIDTH] == 77)tOut=128;
if(temp[(1+j)*HZ_WIDTH] == 77)tOut+=64;
if(temp[(2+j)*HZ_WIDTH] == 77)tOut+=32;
if(temp[(3+j)*HZ_WIDTH] == 77)tOut+=16;
if(temp[(4+j)*HZ_WIDTH] == 77)tOut+=8;
if(temp[(5+j)*HZ_WIDTH] == 77)tOut+=4;
if(temp[(6+j)*HZ_WIDTH] == 77)tOut+=2;
if(temp[(7+j)*HZ_WIDTH] == 77)tOut+=1;
}
else if(type==3)
{
if(temp[j*HZ_WIDTH] == 77)tOut=1;
if(temp[(1+j)*HZ_WIDTH] == 77)tOut+=2;
if(temp[(2+j)*HZ_WIDTH] == 77)tOut+=4;
if(temp[(3+j)*HZ_WIDTH] == 77)tOut+=8;
if(temp[(4+j)*HZ_WIDTH] == 77)tOut+=16;
if(temp[(5+j)*HZ_WIDTH] == 77)tOut+=32;
if(temp[(6+j)*HZ_WIDTH] == 77)tOut+=64;
if(temp[(7+j)*HZ_WIDTH] == 77)tOut+=128;
}
if(tOut<16)fprintf(data,"0x0%x,",tOut);
else fprintf(data,"0x%x,",tOut);
}
if(type<2)temp+=HZ_WIDTH;
else temp++;
fprintf(data,"\n");
}
fclose(data);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -