Click to See Complete Forum and Search --> : Urgent: I need help with text on bitmaps


Tom Bollich
March 29th, 1999, 10:06 AM
I need to take a CBitmap, write text on it,

and then get it back into a CBitmap, what

is the best way to do this? Right now I'm

doing:

CBitmap bitmap;

bitmap.LoadBitmap(IDB_BITMAP1);

//and then I create a CDC object:

CDC dcObj;

dcObj.CreateCompatibleDC(NULL);

dcObj.SelectObject(bitmap);

dcObj.TextOut(0,0, "Hi");

// now how exactly do I get it back out?

// I tried this:

CBitmap bitmap2;

bitmap2.CreateCompatibleBitmap(&dcObj, 10, 10);

// and all I get is a black background with

// a grey square in the middle which I think is 10x10

I also tried using the CDC.SelectObject to try to

get the bitmap out and I used GetCurrentBitmap, both

gave me back a grey bitmap. So could someone please

tell me how to do this, or maybe show me where I am

going wrong in my code? Oh and I am pretty new to

C++ and MFC so please speak slowly and with a lot of

big hand movements ;)

Thanks

Jerry Coffin
March 29th, 1999, 12:09 PM
When you select your bitmap into the DC, save the DC that it started out with.

CBitmap old_ddb = dcobj.SelectObject(bitmap);

then do your drawing. When you're done, select the original bitmap back into the DC:

dcobj.SelectObject(old_ddb);

and simply use your bitmap. You don't have to do anything to get it back out of the DC -- when you select a bitmap

into a DC, you're basically just telling the DC "here's a block of memory you can draw into". You don't have to get

the bitmap back out, because it was never _in_ to start with...

Here's a bit of tested code. This part is in an OnInitialUpdate:

CClientDC cdc(this);

dc.CreateCompatibleDC(&cdc);

bm.CreateCompatibleBitmap(&cdc, 100, 100);

dc.SelectObject(&bm);

dc.TextOut(0,0, "Hi");

Then this is added to the OnDraw:

pDC->BitBlt(0, 0, 100, 100, &dc, 0, 0, SRCCOPY);

and I get "Hi" in black on a white background, then a black box to the right and below that for a total of 100 pixels.