http://bbs.bccn.net/thread-291042-1-1.html
#include <tchar.h>
#include <malloc.h>
#include <windows.h>
WINBASEAPI HWND WINAPI GetConsoleWindow();
//
// 把 double 型转换成 FIXED 型数据
FIXED FixedFromDouble(double d)
{
long l;
l = (long)(d * 65536L);
return *(FIXED*)&l;
}
// 初始化单位矩阵
BOOL MatrixIdentity(MAT2* pMatrix, double dbZoom)
{
if(pMatrix)
{
pMatrix->eM11 = FixedFromDouble(dbZoom);
pMatrix->eM12 = FixedFromDouble(0);
pMatrix->eM21 = FixedFromDouble(0);
pMatrix->eM22 = FixedFromDouble(dbZoom);
return TRUE;
}
return FALSE;
}
// 初始化字体
HFONT InitFont()
{
LOGFONT lf = { -20, 16, 0, 0, 1, 0, 0, 0,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH,
_T("宋体") };
return CreateFontIndirect(&lf);
}
// 打印字符
BOOL PrintCharacter(HDC hDC, UINT ch)
{
if(hDC)
{
GLYPHMETRICS gm;
MAT2 matrix;
DWORD dwSize;
// 放大2倍矩阵
if(!MatrixIdentity(&matrix, 2.0f)) return FALSE;
// 测试需要申请的空间
dwSize = GetGlyphOutline(hDC, ch, GGO_BITMAP, &gm, 0, NULL, &matrix);
if(dwSize > 0 && dwSize < 0xFFFF)
{
LPBYTE lpBuffer = (LPBYTE)malloc(dwSize);
if(lpBuffer)
{
// 获得字形
if(GetGlyphOutline(hDC, ch, GGO_BITMAP, &gm, dwSize, lpBuffer, &matrix))
{
int i, j, k;
int nByteCount;
// 每行字节数
nByteCount = ((gm.gmBlackBoxX + 31) >> 5) << 2;
// 遍历行
for(i = 0; i < gm.gmBlackBoxY; i++)
{
// 遍历列
for(j = 0; j < nByteCount; j++)
{
BYTE code = lpBuffer[i * nByteCount + j];
// 遍历位
for(k = 0; k < 8; k++)
{
if(code & (0x80 >> k)) _tprintf(_T("+"));
else _tprintf(_T(" "));
}
}
_tprintf(_T("\r\n"));
}
}
}
free(lpBuffer);
return TRUE;
}
}
return FALSE;
}
int PrintString(HDC hDC, LPCTSTR pString)
{
if(pString)
{
TCHAR* p = pString;
while(*p)
{
if(!PrintCharacter(hDC, *p)) break;
_tprintf(_T("\r\n"));
p += 1;
}
return p - pString;
}
return 0;
}
int main()
{
// 获得当前控制台设备句柄
HWND hWnd = GetConsoleWindow();
HDC hDC = GetDC(hWnd);
HFONT hFont = InitFont();
HFONT hOldFont = SelectObject(hDC, hFont);
int cnt = PrintString(hDC, _T("FlyingBlue园林"));
SelectObject(hDC, hOldFont);
DeleteObject(hFont);
ReleaseDC(hWnd, hDC);
return 0;
}
字模
最新推荐文章于 2024-01-21 11:18:45 发布