This topic is now archived and is closed to further replies.


Putting text in a window

Recommended Posts

I know how do display text using DrawText() and how to make fonts and such, but I want to know if there is a way to draw text to an hdc in memory then display that to the window later, say for double buffering in a game. THX, JAP

Share this post

Link to post
Share on other sites
Just make a memory dc large enough to hold the text, and use DrawText or ExTextOut to draw to the memory dc, then later on, to display that text, Blit the memory dc to the backbuffer before you flip it. On a side note, I use ExTextOut because it is a little faster, but it also doesn''t do wordwrap and some other things DrawText does.

Share this post

Link to post
Share on other sites
How do I make this memory dc large enough for text, would this work,

HDC hdcMem = CreateCompatibleDC(hdc);

or is there another way to make it, because nothing displays when I use the memory dc method. This is the current function I have,

void drawtext( HDC hdc, RECT* prc )
HDC hdcMem = CreateCompatibleDC(hdc);
FillRect(hdcMem, prc, (HBRUSH)GetStockObject(WHITE_BRUSH));
HDC hdc2;
long lfHeight;

hdc2 = GetDC(NULL);
lfHeight = -MulDiv(100, GetDeviceCaps(hdc2, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc2);

hf = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");

char szSize[] = "See Christmas";
HFONT hfOld = (HFONT)SelectObject(hdcMem, hf);

SetBkColor(hdcMem, RGB(255,255,255));
SetTextColor(hdcMem, RGB(255,0,0));
SetBkMode(hdcMem, TRANSPARENT);

DrawText(hdcMem, szSize, -1, prc, DT_SINGLELINE);

BitBlt(hdc, 0, 0, 100, 100, hdcMem, 0, 0, SRCCOPY);

SelectObject(hdcMem, hfOld);



Share this post

Link to post
Share on other sites