字模

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值