网页功能: 加入收藏 设为首页 网站搜索  
制做自己的点阵字库的方法和程序
发表日期:2007-01-16作者:[转贴] 出处:  

fontmaker.h

/////////////////////////////////////////////////////////////////////////////
// ELEMENT GDI/DX Game Engine [Version 2002/3/1]
/////////////////////////////////////////////////////////////////////////////
// Original Author : 邱海峰[Southdy]
// OICQ : 359766
// EMAIL: topmud@263.net
/////////////////////////////////////////////////////////////////////////////
// DESCRIPTION:
// OTHER: 邱海峰创建于2002/3/1
/////////////////////////////////////////////////////////////////////////////
#pragma once
/////////////////////////////////////////////////////////////////////////////
// 包含文件
/////////////////////////////////////////////////////////////////////////////
#include "ekwindow.h"// 窗口程序框架接口
/////////////////////////////////////////////////////////////////////////////

struct BufferDesc
{
    int w,h,pitch;
    void* data;
    BufferDesc(int width,int height):w(width),h(height),pitch((width*2+7)&~7),data(new unsigned char[pitch*height]){}
    ~BufferDesc(){delete [] data;}
};

/////////////////////////////////////////////////////////////////////////////
class MyApp : public EK_Application
{
/////////////////////////////////////////////////////////////////////
    HFONT m_Font;
    HBITMAP m_FontBMP;
    unsigned short* m_FontMap;
    HDC m_FontDC;
    BufferDesc* m_surface;
/////////////////////////////////////////////////////////////////////
    EK_TMessage<MyApp> MsgDestroy;
    EK_TMessage<MyApp> MsgCommand;
/////////////////////////////////////////////////////////////////////
    LRESULT OnDestroy(WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnCommand(WPARAM wParam, LPARAM lParam, BOOL& bHandled);
/////////////////////////////////////////////////////////////////////
    void AfterCreate();
    void Idle();
    void BeforeExit();
/////////////////////////////////////////////////////////////////////
    void InitFont();
    void DestroyFont();
    void DrawChar(unsigned int c);
/////////////////////////////////////////////////////////////////////
public:
/////////////////////////////////////////////////////////////////////
    MyApp():EK_Application(),m_surface(0){}
    MyApp(const HINSTANCE hInstance):EK_Application(hInstance),m_surface(0){}
    ~MyApp(){}
/////////////////////////////////////////////////////////////////////
};
/////////////////////////////////////////////////////////////////////////////

fontmaker.cpp

/////////////////////////////////////////////////////////////////////////////
// ELEMENT GDI/DX Game Engine [Version 2002/3/1]
/////////////////////////////////////////////////////////////////////////////
// Original Author : 邱海峰[Southdy]
// OICQ : 359766
// EMAIL: topmud@263.net
/////////////////////////////////////////////////////////////////////////////
// DESCRIPTION:
// OTHER: 邱海峰创建于2002/3/1
/////////////////////////////////////////////////////////////////////////////
#pragma message("点阵汉字制作简单演示")
/////////////////////////////////////////////////////////////////////////////
// 包含文件
/////////////////////////////////////////////////////////////////////////////

#include "fontmaker.h"
#include <fstream.h>

#define FONTPOINT 12

/////////////////////////////////////////////////////////////////////////////
// FUNCTION:
// PURPOSE: 窗口程序入口
// ENTRY:
// EXITS:
//  SUCCESS :
//  FAILURE :
// OTHER:
/////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MyApp mywin(hInstance);
    if(!mywin.InitMain(NULL,"ELEMENT","云海孤峰", WS_VISIBLE|WS_SYSMENU|WS_MINIMIZEBOX|WS_OVERLAPPED,0,0,640,480,NULL,"EK_MENU"))
        return 0;
    mywin.Excute();
    return 0;
}

/////////////////////////////////////////////////////////////////////////////
// FUNCTION:
// PURPOSE: 主窗口WM_DESTROY消息处理函数
// ENTRY:
// EXITS:
//  SUCCESS :
//  FAILURE :
// OTHER:
/////////////////////////////////////////////////////////////////////////////
LRESULT MyApp::OnDestroy(WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    PostQuitMessage(0);
    bHandled = 1;
    return 0;
}

/////////////////////////////////////////////////////////////////////////////
// FUNCTION:
// PURPOSE: 主窗口WM_COMMAND消息处理函数
// ENTRY:
// EXITS:
//  SUCCESS :
//  FAILURE :
// OTHER:
/////////////////////////////////////////////////////////////////////////////
LRESULT MyApp::OnCommand(WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    switch(LOWORD(wParam))
    {
    case 10:
        break;
    case 11:
        DestroyWindow(GetWnd());
        break;
    }

    bHandled = 0;
    return 0;
}

/////////////////////////////////////////////////////////////////////////////
// FUNCTION: void MyApp::AfterCreate()
// PURPOSE: 运行前期准备函数
// ENTRY:
// EXITS:
//  SUCCESS :
//  FAILURE :
// OTHER:
/////////////////////////////////////////////////////////////////////////////
void MyApp::AfterCreate()
{
    MsgDestroy.SetMsgHandler(this,&MyApp::OnDestroy);
    MsgCommand.SetMsgHandler(this,&MyApp::OnCommand);
    RegMsgHandler(WM_DESTROY,&MsgDestroy);
    RegMsgHandler(WM_COMMAND,&MsgCommand);
    m_surface=new BufferDesc(640,480);
    InitFont();
    DrawChar('家');
}

/////////////////////////////////////////////////////////////////////////////
// FUNCTION: void MyApp::Idle()
// PURPOSE: 循环工作函数
// ENTRY:
// EXITS:
//  SUCCESS :
//  FAILURE :
// OTHER:
/////////////////////////////////////////////////////////////////////////////
static int GDIbmpinfo16[13]={sizeof(BITMAPINFOHEADER),0,0,0x100001,BI_BITFIELDS,0,0,0,0,0,0xf800,0x7e0,0x1f};
/////////////////////////////////////////////////////////////////////////////
void MyApp::Idle()
{
    HDC myhdc=GetDC(GetWnd());
    if(!myhdc)
        return;
    GDIbmpinfo16[1]=m_surface->pitch>>1,GDIbmpinfo16[2]=-m_surface->h;
    SetDIBitsToDevice(myhdc,0,0,m_surface->w,m_surface->h,0,0,0,m_surface->h,m_surface->data,(BITMAPINFO*)&(GDIbmpinfo16[0]),0);
    ReleaseDC(GetWnd(),myhdc);
}

/////////////////////////////////////////////////////////////////////////////
// FUNCTION: void MyApp::BeforeExit()
// PURPOSE: 退出前期准备函数
// ENTRY:
// EXITS:
//  SUCCESS :
//  FAILURE :
// OTHER:
/////////////////////////////////////////////////////////////////////////////
void MyApp::BeforeExit()
{
    DestroyFont();
    delete m_surface;
}

/////////////////////////////////////////////////////////////////////////////
void MyApp::InitFont()
{
    GDIbmpinfo16[1]=FONTPOINT,GDIbmpinfo16[2]=-FONTPOINT;
    HDC myhdc=GetDC(GetWnd());
    if(!myhdc)
        return;
    m_FontBMP = CreateDIBSection(myhdc,(BITMAPINFO*)&(GDIbmpinfo16[0]),DIB_RGB_COLORS,(void**)&m_FontMap,0,0);
    ReleaseDC(GetWnd(),myhdc);
    for(int i=0;i<FONTPOINT*FONTPOINT;i++)
        m_FontMap=0x1f;
    m_FontDC=CreateCompatibleDC(NULL);
    SelectObject(m_FontDC,m_FontBMP);
    m_Font=CreateFont( FONTPOINT,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_QUALITY,"宋体");
    SelectObject(m_FontDC,m_Font);
}

/////////////////////////////////////////////////////////////////////////////
void MyApp::DestroyFont()
{
    DeleteDC(m_FontDC);
    DeleteObject(m_FontBMP);
    DeleteObject(m_Font);
}

/////////////////////////////////////////////////////////////////////////////
static RECT rect={0,0,FONTPOINT,FONTPOINT};
///////////////////////////////////////////////////////////////////////////////
void MyApp::DrawChar(unsigned int c)
{
    char str[3]={(c&0xff00)>>8,c&0xff,0};
    if(c<0x80)
    {
        str[0]=str[1];
        str[1]=0;
    }

    DrawText(m_FontDC,str,1,&rect,DT_CENTER);
    int i=0,j=0;
    ofstream myoutput("test.txt");
    if(!myoutput.is_open())
        return;

    for(i=0;i<FONTPOINT;i++)
    {
        for(j=0;j<FONTPOINT;j++)
            if(m_FontMap[i*FONTPOINT+j]==0)
                myoutput<<"*";
            else myoutput<<" ";
        myoutput<<endl;
    }
    myoutput.close();
}
/////////////////////////////////////////////////////////////////////////////

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 制做自己的点阵字库的方法和程序
本类热点文章
  如何用D3D/OpenGL在制作2D游戏中高效地..
  自己绘制True type font字体
  使用CPU时间戳进行高精度计时
  阵汉字显示
  程序员的超强外挂——Visual Assist .N..
  用VC++实现console程序显示彩色文本 wx..
  制做自己的点阵字库的方法和程序
  “变速齿轮”再研究
  一个简单的线程管理机制
  VC编程中常用快捷键
  Delpih中的Windows API编程初步
  BIG5到GB的转换技术
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00378