| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3856 人关注过本帖
标题:关于 <生日快乐!> 帖的一些改进
取消只看楼主 加入收藏
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
结帖率:100%
收藏
 问题点数:0 回复次数:4 
关于 <生日快乐!> 帖的一些改进
程序代码:
#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;
}

效果:

++++++++++++++++++++++++++
    ++++             ++++++
    ++++                +++
    ++++                 +++
    ++++                  ++
    ++++                   ++
    ++++
    ++++
    ++++            ++
    ++++            ++
    ++++            ++
    ++++           +++
    ++++++++++++++++++
    ++++          ++++
    ++++            ++
    ++++            ++
    ++++            ++
    ++++
    ++++
    ++++
    ++++
    ++++
    ++++
    ++++
    ++++
    ++++
++++++++++++

         +++
++++++++++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
++++++++++++++++++++

+++++++++++     +++++++++++
   ++++++           +++
    +++++           ++
     ++++          +++
     +++++         ++
      ++++        +++
      +++++       ++
       ++++      +++
       +++++     ++
        +++++   +++
         ++++   ++
         +++++ ++
          +++++++
          ++++++
           +++++
           ++++
            +++
            ++
           +++
           ++
          +++
   +++    ++
  +++++  ++
   ++++++

        +++++
       +++++++
       +++++++
        +++++




         +++
++++++++++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
++++++++++++++++++++

      ++
++++++++     ++++++++
    ++++   ++      ++++
    ++++ ++         ++++
    +++++            ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
++++++++++++     ++++++++++++

        +++++++++     ++++
     ++++      ++++++++++++
    +++          +++    +++
   ++++          ++++
  ++++            ++++
  ++++            ++++
  ++++            ++++
  ++++            ++++
   ++++           +++
    ++++         +++
     ++++      ++++
    +++ +++++++++
  ++++
  ++++
  +++++++++++
    ++++++++++++++++++
   +++     +++++++++++++
 ++++               +++++
++++                 ++++
++++                 ++++
++++                 ++++
 ++++               ++++
   +++++          ++++
      +++++++++++++

+++++++++++++++++++++
    ++++          ++++++
    ++++            ++++++
    ++++             +++++
    ++++              +++++
    ++++              +++++
    ++++              +++++
    ++++              +++++
    ++++              ++++
    ++++             +++++
    ++++            ++++
    ++++          ++++
    ++++++++++++++++
    ++++           +++++
    ++++             +++++
    ++++               ++++
    ++++               +++++
    ++++                ++++
    ++++                +++++
    ++++                +++++
    ++++                +++++
    ++++                +++++
    ++++               +++++
    ++++               +++++
    ++++             +++++
    ++++           ++++++
+++++++++++++++++++++

         +++
++++++++++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
        ++++
++++++++++++++++++++

      ++               ++
++++++++         ++++++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++             ++++
    ++++            +++++
     ++++          + ++++
      ++++       ++  ++++++++
        +++++++++    ++

        +++++++++
     ++++       ++++
    +++           +++
  ++++             +++
  +++              ++++
 +++                +++
++++                ++++
++++                ++++
++++++++++++++++++++++++
++++
++++
++++
 ++++
 ++++                ++
  ++++               +
   ++++            ++
     ++++        +++
        +++++++++

+                                            ++
+++++++++++++++++++++++++++++++++++++++++++++++++
+++                                          ++++
+++                                          +++
+++                                          +++
+++                            +++           +++
+++                          ++++++          +++
+++        ++++++++++++++++++      +         +++
+++                                          +++
+++                                          +++
+++                                          +++
+++                                   +      +++
+++                                 ++++     +++
+++  +++++++++++++++++++++++++++++++++++++   +++
+++    +        +++       +++                +++
+++             +++       +++                +++
+++             +++       +++                +++
+++             +++       +++                +++
+++            ++++       +++                +++
+++            +++        +++                +++
+++            +++        +++          ++    +++
+++            +++        +++          ++    +++
+++           +++         +++          ++    +++
+++           +++         +++          ++    +++
+++          +++          ++++         ++    +++
+++         +++           ++++         +++   +++
+++        +++            ++++++      ++++++ +++
+++       ++               +++++++++++++++   +++
+++     +++                                  +++
+++    ++                                    +++
+++   +                                      +++
+++                                          +++
++++++++++++++++++++++++++++++++++++++++++++++++
+++                                          +++
+++                                          +++
+++


             ++++                    ++++
             +++++                   +++++
             ++++                    +++
             ++++                    +++
             ++++                    +++
             ++++                    +++
             ++++                    +++
             ++++     +              +++           ++
             ++++    +++             +++         +++++
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
            +++++                  +++++++
            +++++                  +++++++
           ++++++                 +++++++++
           ++++++                 +++++++ +
           +++++++               ++++++++ ++
          +++++++++++            +++ ++++ ++
         ++++++++  ++++         +++  ++++  ++
         +++ ++++   ++++       +++   ++++  +++
        ++++ ++++    ++++     ++++   ++++   +++
       ++++  ++++    ++++     +++    ++++    +++
       +++   ++++     ++     +++     ++++    ++++
      +++    ++++           +++      ++++     ++++
     +++     ++++          ++        ++++      ++++
    +++      ++++         ++         ++++       +++++
   +++       ++++        ++          ++++        ++++++
  ++         ++++      +++           ++++         +++++++
 ++          ++++     ++             ++++          +++++++
++           ++++    +               ++++           ++
             ++++  ++                ++++
             ++++                    ++++
             ++++                    ++++
             ++++                    ++++
             ++++                    ++++
             ++++                    ++++
             ++++                    ++++
             +++                     ++

Press any key to continue
搜索更多相关主题的帖子: 生日 快乐 改进 
2009-11-04 20:17
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
注意编译设置把_MBCS改成 _UNICODE,UNICODE

否则汉字将不能显示

或者在最前面添加以下代码:

#define _UNICODE
#define UNICODE

[ 本帖最后由 flyue 于 2009-11-4 20:24 编辑 ]

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2009-11-04 20:23
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
再补充一个代码:
程序代码:
#define _UNICODE
#define UNICODE

#include <tchar.h>
#include <malloc.h>
#include <locale.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 = { -12, 6, 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, 1.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);
    TCHAR strInput[256] = { 0 };

    setlocale(LC_CTYPE, "chs");
    _tprintf(_T("Input a character: "));
    _tscanf(_T("%s"), strInput);
    PrintString(hDC, strInput);
    
    SelectObject(hDC, hOldFont);
    DeleteObject(hFont);
    ReleaseDC(hWnd, hDC);

    return 0;
}

效果

程序代码:
Input a character: 你好,世界!
      █    █
      █  █
    █    ██████
  ██  █          █
█  ██      █
    █    █  █  █
    █    █  █  █
    █  █    █    █
    ██      █    █
    █        █
    █    ███

    █    █████
    █            █
█████      █
  █    █    █
  █    █    █
  █    ███████
█    █      █
██  █      █
    ██      █
  █    █    █
█          ██

  █
  █
█

        █      █
  █    █      █
  █    █      █
███████████
  █    █      █
  █    █      █
  █    █      █
  █    █████
  █    █      █
  █
  ██████████

    ████████
    █    █      █
    ████████
    █    █      █
    ████████
      █      █
    █  █    ██
██    █    █  ██
        █    █
      █      █
  ██        █

█
█
█
█
█
█

█

Press any key to continue

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2009-11-05 13:05
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
纯 C 书写, 并不涉及任何 C++ 语法

支持Win32的 C 语言编译器都能编译

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2009-11-06 19:29
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
你有没有注意我前面发的帖子?

代码最前面添加:
#define _UNICODE
#define UNICODE

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2009-11-07 14:08
快速回复:关于 <生日快乐!> 帖的一些改进
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020786 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved