📄 bitfont.c
字号:
}
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = szBuf;
ofn.nMaxFile = MAX_PATH;
ofn.nMaxFileTitle = MAX_PATH;
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szName;
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)) {
HANDLE f;
LONG ret;
LONG skip, width, bytewidth, height;
LONG pagenum, pages;
LONG size, size1, size2;
unsigned char *font;
/* 转换字库名称 */
while (*chg)
chg++;
while (*chg != TEXT('\\') && chg != szFonName)
chg--;
if (*chg == TEXT('\\'))
chg++;
/* 根据字库名称读取字库信息 */
size1 = GetPrivateProfileInt(chg, TEXT("Size"), 0, szFonName);
/* 检查一下返回值 */
if (size1 == 0)
return FALSE;
width = GetPrivateProfileInt(chg, TEXT("Width"), 0, szFonName);
bytewidth = width / Byte;
if (width % Byte)
bytewidth++;
height = GetPrivateProfileInt(chg, TEXT("Height"), 0, szFonName);
pagenum = GetPrivateProfileInt(chg, TEXT("PageNum"), 0, szFonName);
pages = GetPrivateProfileInt(chg, TEXT("Pages"), 0, szFonName); //???
/* 这里需要改进,还是…… */
if (height * bytewidth * pagenum * pages > size1) {
MessageBox(hwnd, TEXT("字体配置文件大小设置不对!"), NULL, MB_ICONSTOP);
return FALSE;
}
f = CreateFile(szName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (f == INVALID_HANDLE_VALUE) {
MessageBox(hwnd, TEXT("内部错误!"), NULL, MB_ICONSTOP);
CloseHandle(f);
return FALSE;
}
size2 = GetFileSize(f, NULL);
if (size1 > size2) {
MessageBox(hwnd, TEXT("字体文件大小不对!"), NULL, MB_ICONSTOP);
CloseHandle(f);
return FALSE;
}
skip = GetPrivateProfileInt(chg, TEXT("Skip"), 0, szFonName);
size = size1 - skip;
font = malloc(size);
if (font == NULL) {
MessageBox(hwnd, TEXT("动态分配内存错误!"), 0, MB_ICONSTOP);
CloseHandle(f);
return FALSE;
}
ret = 0;
SetFilePointer(f, skip, &ret, FILE_BEGIN);
ret = 0;
ReadFile(f, font, size, &ret, NULL);
CloseHandle(f);
if (size != ret) {
MessageBox(hwnd, TEXT("读取字体文件错误!"), 0, MB_ICONSTOP);
return FALSE;
}
/* 读取成功,设置所有信息参数 */
if (BitFont)
free(BitFont);
BitFont = font;
lstrcpy(szFileName, chg);
Bit = GetPrivateProfileInt(szFileName, TEXT("Bit"), 0, szFonName);
Print = GetPrivateProfileInt(szFileName, TEXT("Print"), 0, szFonName);
ByteWidth = bytewidth;
Height = height;
LineNum = GetPrivateProfileInt(szFileName, TEXT("LineNum"), 0, szFonName);
PageNum = pagenum;
Pages = pages;
Count = PageNum * Pages;
cWidth = width + Spacing;
cHeight = Height + Spacing;
return TRUE;
}
return FALSE;
}
/*------------------------------------------------------------------------
Procedure: WndProc ID:1
Purpose: 主处理函数
Input: Win32 默认
Output: Win32 默认
Errors:
------------------------------------------------------------------------*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rc1, rc2;
int Lines, cX, cY;
LONG uPage;
ULONG ulScrollLines;
SCROLLINFO si;
static int iPage = 1;
static int iDeltaPerLine, iAccumDelta;
switch (msg) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_OPEN:
if (OpenBitFontFile(hwnd)) {
/* 打开字库成功后,设置相应程序内部信息 */
iPage = 1;
si.cbSize = sizeof(si);
si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
si.nMin = 1;
si.nPage = 1;
si.nMax = Pages;
si.nPos = iPage;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
Lines = PageNum / LineNum;
if (PageNum % LineNum)
Lines++;
cX = Spacing + LineNum * cWidth;
if (Pages > 1)
cX += GetSystemMetrics(SM_CXVSCROLL);
cY = Spacing + Lines * cHeight;
SetRect(&rc1, 0, 0, cX, cY);
AdjustWindowRectEx(&rc1, GetWindowStyle(hwnd), GetMenu(hwnd) != NULL, GetWindowExStyle(hwnd));
GetWindowRect(hwnd, &rc2);
MoveWindow(hwnd, rc2.left, rc2.top, rc1.right - rc1.left, rc1.bottom - rc1.top, TRUE);
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case IDM_ABOUT:
MessageBox(hwnd, TEXT("点阵字库查看器\n\nLarry Li[Wuhan], 2003.1.10\n"), TEXT("关于..."), MB_ICONINFORMATION);
return 0;
case IDM_EXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
return 0;
}
return 0;
case WM_CREATE:
/* 初始化 */
si.cbSize = sizeof(si);
si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
si.nMin = 1;
si.nPage = 1;
si.nMax = Pages;
si.nPos = iPage;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
SendMessage(hwnd, WM_SETTINGCHANGE, 0, 0);
return 0;
case WM_SETTINGCHANGE:
/* 设置您的滚轮鼠标 */
#define WHEEL_DELTA 120
ulScrollLines = GetSystemMetrics(SM_MOUSEWHEELPRESENT);
if (ulScrollLines)
iDeltaPerLine = WHEEL_DELTA / ulScrollLines;
else
iDeltaPerLine = 0 ;
return 0;
case WM_MOUSEWHEEL:
/* 开始滚动吧! */
if (iDeltaPerLine == 0)
break ;
iAccumDelta += (short) HIWORD (wParam); // 120 or -120
while (iAccumDelta >= iDeltaPerLine) {
SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
iAccumDelta -= iDeltaPerLine;
}
while (iAccumDelta <= -iDeltaPerLine) {
SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
iAccumDelta += iDeltaPerLine;
}
return 0;
case WM_KEYDOWN:
/* 键盘控制接口 */
switch(wParam){
#define Key2Msg(k, b) case VK_##k: SendMessage(hwnd, WM_VSCROLL, SB_##b, 0); break
Key2Msg(HOME, TOP);
Key2Msg(END, BOTTOM);
Key2Msg(PRIOR, PAGEUP);
Key2Msg(NEXT, PAGEDOWN);
Key2Msg(UP, LINEUP);
Key2Msg(DOWN, LINEDOWN);
#undef Key2Msg
}
/* 这是很早以前写的东西,被我用在不少地方;宏用的好时就真的是很好用! */
break;
case WM_VSCROLL:
/* 处理滚动条的操作 */
switch (LOWORD(wParam)) {
case SB_TOP: uPage = 1; break;
case SB_BOTTOM: uPage = Pages; break;
case SB_LINEUP: uPage = iPage - 1; break;
case SB_LINEDOWN: uPage = iPage + 1; break;
case SB_PAGEUP: uPage = iPage - 10; break;
case SB_PAGEDOWN: uPage = iPage + 10; break;
case SB_THUMBPOSITION: uPage = HIWORD(wParam); break;
default: return 0;
}
uPage = max(1, min(uPage, Pages));
if (uPage != iPage) {
/* 没有改变时就不需要操作 */
#if 0
si.fMask = SIF_POS;
si.nPos = uPage;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
#else
SetScrollPos(hwnd, SB_VERT, uPage, TRUE);
#endif
InvalidateRect(hwnd, NULL, TRUE);
iPage = uPage;
}
return 0;
case WM_PAINT:
/* 标准的 Win32 绘图实例 */
hdc = BeginPaint(hwnd, &ps);
if (BitFont)
DrawFont(hdc, BitFont, iPage - 1);
else {
RECT rc;
GetClientRect(hwnd, &rc);
DrawText(hdc, TEXT("Larry Li[Wuhan], 2003.1.25"), -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
/* 没什么好写的了 */
FreeFont(BitFont);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -