?? font.cpp
字號:
/**
* @file GLFont.cpp
*
* 項目描述: 3DS文件載入
* 文件描述: 字體類
* 適用平臺: Windows98/2000/NT/XP
*
* 作者: WWBOSS
* 電子郵件: wwboss123@gmail.com
* 創建日期: 2006-09-13
* 修改日期: 2006-12-02
*
*/
//========================================================
#include "Font.h"
GLFont::GLFont()
{
}
/** 析構函數 */
GLFont::~GLFont()
{
if(m_hFont)
DeleteObject(m_hFont); /**< 刪除字體句柄 */
}
/** 初始化字體 */
bool GLFont::InitFont()
{
/**< 創建字體 */
m_hFont = CreateFont(-16, /**< 字體高度 */
0, /**< 字體寬度 */
0, /**< 字體的旋轉角度 Angle Of Escapement */
0, /**< 字體底線的旋轉角度Orientation Angle */
FW_BOLD, /**< 字體的重量 */
FALSE, /**< 是否使用斜體 */
FALSE, /**< 是否使用下劃線 */
FALSE, /**< 是否使用刪除線 */
GB2312_CHARSET, /**< 設置字符集 */
OUT_TT_PRECIS, /**< 輸出精度 */
CLIP_DEFAULT_PRECIS, /**< 裁剪精度 */
ANTIALIASED_QUALITY, /**< 輸出質量 */
FF_DONTCARE|DEFAULT_PITCH, /**< Family And Pitch */
"宋體"); /**< 字體名稱 */
if(!m_hFont)
return false; /**< 創建字體失敗則返回 */
/** 初始化成功則返回true */
return true;
}
/** 在指定位置輸出字符串 */
void GLFont::PrintText(char *string, float x, float y)
{
HBITMAP hBitmap,hOldBmp; /**< 定義兩個位圖句柄 */
BITMAP bm; /**< 位圖結構變量 */
SIZE size; /**< 位圖尺寸 */
GLboolean lp,tp;
HDC hDC = ::CreateCompatibleDC(0); /**< 暫存設備場景 */
glGetBooleanv(GL_LIGHTING,&lp); /**< 查看場景中是否有光照 */
glGetBooleanv(GL_TEXTURE_2D,&tp);/**< 查看場景中是否啟用紋理 */
/** 保存和設置一些屬性 */
glLoadIdentity();
glPushMatrix();
glTranslatef(0,0,-10.0f);
glDisable(GL_LIGHTING); /**< 關閉光照 */
glDisable(GL_TEXTURE_2D); /**< 關閉紋理 */
glDisable(GL_DEPTH_TEST); /**< 關閉深度測試 */
SelectObject(hDC, m_hFont); /**< 選擇字體 */
::GetTextExtentPoint32(hDC, string, strlen(string), &size);/**< 獲取字符位圖大小 */
hBitmap = CreateBitmap(size.cx, size.cy,1, 1, NULL); /**< 創建與hDC相關單色位圖 */
hOldBmp = (HBITMAP)SelectObject(hDC,hBitmap); /**< 選擇位圖 */
SetBkColor(hDC, RGB(0, 0, 0)); /**< 背景色為黑色 */
SetTextColor(hDC, RGB(255, 255, 255)); /**< 字體顏色白色 */
SetBkMode(hDC, OPAQUE); /**< 用當前的背景顏色填充背景 */
TextOut(hDC, 0, 0, string, strlen(string)); /**< 輸出文字到暫存hDC */
/** 獲得相關位圖數據結構 */
GetObject(hBitmap, sizeof(bm), &bm);
size.cx = (bm.bmWidth + 31) & (~31); /**< 邊緣對齊 */
size.cy = bm.bmHeight;
int bufsize = size.cx * size.cy/8; /**< 圖形數據長度 */
/** 定義單色位圖結構 */
struct {
BITMAPINFOHEADER bih;
RGBQUAD col[2];
}bic;
/** 獲取單色位圖結構信息 */
BITMAPINFO *binf = (BITMAPINFO *)&bic;
binf->bmiHeader.biSize = sizeof(binf->bmiHeader); /**< 修改結構信息 */
binf->bmiHeader.biWidth = bm.bmWidth;
binf->bmiHeader.biHeight = bm.bmHeight;
binf->bmiHeader.biPlanes = 1;
binf->bmiHeader.biBitCount = 1; /**< 單色 */
binf->bmiHeader.biCompression = BI_RGB; /**< 顏色方式 */
binf->bmiHeader.biSizeImage = bufsize;
binf->bmiHeader.biXPelsPerMeter = 1;
binf->bmiHeader.biYPelsPerMeter = 1;
binf->bmiHeader.biClrUsed = 0;
binf->bmiHeader.biClrImportant = 0;
/** 定義圖形數據塊 */
UCHAR* pBmpBits = new UCHAR[bufsize];
memset(pBmpBits, 0, sizeof(UCHAR)*bufsize);
/** 將設備無關數據保存在pBmpBits指向的數據塊中 */
::GetDIBits(hDC, hBitmap, 0, bm.bmHeight, pBmpBits, binf,DIB_RGB_COLORS);
/** 顯示字符串 */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /**< 指定像素存儲模式 */
glRasterPos2f(x,y); /**< 定位 */
glBitmap(size.cx, size.cy, 0.0, 0.0, 0.0, 0.0, pBmpBits); /**< 位圖顯示 */
delete pBmpBits; /**< 刪除指針 */
SelectObject(hDC, hOldBmp); /**< 恢復原來位圖信息 */
DeleteObject(hBitmap);
::DeleteDC(hDC);
/** 恢復一些屬性 */
if(lp)
glEnable(GL_LIGHTING); /**< 啟用光照 */
if(tp)
glEnable(GL_TEXTURE_2D); /**< 啟用紋理 */
glEnable(GL_DEPTH_TEST); /**< 啟用深度測試 */
glPopMatrix();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -