|
|
可以显示出汉字,但是颜色无论怎么设置也还是黑色,请高手指教,贴出相关代码如下:
HDC hMemDC = NULL;
HDC hDC = NULL;
bool ColorSelected = false;
bool Drawed = true;
float glR, glG, glB;
void CreateBitmap(BITMAP *bmp, LONG width, LONG height)
{
FillMemory(bmp, sizeof(*bmp), 0);
bmp->bmWidth = width;
bmp->bmHeight = height;
bmp->bmWidthBytes = ((width + 7) / 8 + 1) & (~1);
bmp->bmPlanes = 1;
bmp->bmBitsPixel = 1;
bmp->bmBits = GlobalAlloc(GMEM_FIXED, bmp->bmWidthBytes*height);
}
unsigned char *GetBitmapBits(HDC hDC, HBITMAP hBmp, int* size)
{
BITMAP bi;
unsigned char *bits;
struct {
BITMAPINFOHEADER bih;
RGBQUAD col[2];
}bic;
BITMAPINFO *binf = (BITMAPINFO *)&bic;
GetObject(hBmp, sizeof(bi), &bi);
*size = bi.bmHeight*(((bi.bmWidth + 31) & (~31)) / 8);
bits = (unsigned char *)GlobalAlloc(GPTR, *size);
binf->bmiHeader.biSize = sizeof(binf->bmiHeader);
binf->bmiHeader.biWidth = bi.bmWidth;
binf->bmiHeader.biHeight = bi.bmHeight;
binf->bmiHeader.biPlanes = 1;
binf->bmiHeader.biBitCount = 1;
binf->bmiHeader.biCompression = BI_RGB;
binf->bmiHeader.biSizeImage = *size;
binf->bmiHeader.biXPelsPerMeter = 1;
binf->bmiHeader.biYPelsPerMeter = 1;
binf->bmiHeader.biClrUsed = 0;
binf->bmiHeader.biClrImportant = 0;
GetDIBits(hDC, hBmp, 0, bi.bmHeight, bits, binf, DIB_RGB_COLORS);
return bits;
}
unsigned char *CreateFontBitmap(HDC hDC, HFONT hFont, char *str, PSIZEL size)
{
BITMAP bmp;
HBITMAP hbmp;
unsigned char *pbm;
int len = lstrlen(str);
HFONT hFontOld = (HFONT)SelectObject(hDC, hFont);
GetTextExtentPoint32(hDC, str, len, size);
CreateBitmap(&bmp, size->cx, size->cy);
hbmp = CreateBitmapIndirect(&bmp);
GlobalFree(bmp.bmBits);
DeleteObject(&bmp);
if(hbmp && hMemDC)
{
HBITMAP hPrevBmp = (HBITMAP)SelectObject(hMemDC, hbmp);
HFONT hPrevFont;
int size0;
BITMAP bi;
SetBkColor(hMemDC, RGB(0,0,0));
SetTextColor(hMemDC, RGB(255,255,255));
SetBkMode(hMemDC, OPAQUE);
hPrevFont = (HFONT)SelectObject(hMemDC, hFont);
TextOut(hMemDC, 0, 0, str, len);
GetObject(hbmp, sizeof(bi), &bi);
pbm = GetBitmapBits(hMemDC, hbmp, &size0);
size->cx = ((bi.bmWidth+31)&(~31));
size->cy = bi.bmHeight;
GlobalFree(bi.bmBits);
DeleteObject(&bi);
DeleteObject(hPrevFont);
DeleteObject(hPrevBmp);
DeleteObject(hbmp);
}
SelectObject(hDC, hFontOld);
return pbm;
}
int DrawStringFont(int x,int y,char *str)
{
LOGFONT lf;
HFONT hFont;
unsigned char *lpBitmap;
SIZE size;
if(!hDC)
{
hDC = wglGetCurrentDC();
hMemDC = CreateCompatibleDC(hDC);
}
FillMemory(&lf, sizeof(lf), 0);
lf.lfHeight = 10;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfWeight = FW_BOLD;
lf.lfOutPrecision = OUT_STROKE_PRECIS;
lf.lfClipPrecision = CLIP_CHARACTER_PRECIS;
lstrcpy(lf.lfFaceName, "MS Sans Serif");
hFont = CreateFontIndirect(&lf);
GlobalFree(&lf);
lpBitmap = CreateFontBitmap(hDC, hFont, str, &size);
DeleteObject(hFont);
glColor3f(0,0,0);
glRasterPos2f((float)x + 1, (float)y + 1 + ADJUST_HEIGHT);
glBitmap(size.cx, size.cy, 0.0, 0.0, 0.0, 0.0, lpBitmap);
if (ColorSelected)
{
ColorSelected = false;
glColor3f(glR, glG, glB);
}
else
{
glColor3f(1.0, 1.0, 1.0);
}
glRasterPos2f((float)x, (float)y + ADJUST_HEIGHT);
glBitmap(size.cx, size.cy, 0.0, 0.0, 0.0, 0.0, lpBitmap);
GlobalFree(lpBitmap);
Drawed = true;
return x + size.cx;
}
|
|