Click to See Complete Forum and Search --> : bitmap problem I think


scomet1
August 13th, 2004, 11:38 AM
I'm having some trouble with using bitmaps I think. Below I have code from my paint function. It draws the text, but when it does the portion of the drawing rectangle above the text is black - even if I fill the background with a white brush and there is a constant flicker. I've tried double buffering with the main window but it still flickers the same. So I'm thinking maybe my bitmap size is off or something? Another thing is that if I comment out the text drawing and run it again - many times the text still shows up! Is something not being cleared correctly???



//hdc has been passed in


HDC memdc = CreateCompatibleDC(hdc);



// determine size for bitmap - this is my window size
RECT blankRc;
rcMyBounds.left = 0;
rcMyBounds.top = 30;
rcMyBounds.right = 240;
rcMyBounds.bottom = 100;


// create the bitmap of the right size
const int width = rcMyBounds.right-rcMyBounds.left;
const int height = rcMyBounds.bottom-rcMyBounds.top;
HBITMAP textBmp = CreateCompatibleBitmap( hdc, width, height );

// load the bitmap in and draw the text onto it
HBITMAP oldBmp = (HBITMAP)SelectObject(memdc, textBmp);

CComBSTR text("example");
tx = text;
DrawText(memdc, tx, -1, &blankRc, DT_LEFT);

SelectObject(memdc, oldBmp);
DeleteDC(memdc);


//now blit it to the main screen
memdc = CreateCompatibleDC(hdc);

SelectObject(memdc, textBmp);
BitBlt(hdc, rcMyBounds.left,rcMyBounds.top, width, height, memdc, blankRc.left,0, SRCCOPY);


SelectObject(memdc, HBITMAP(NULL));
DeleteDC(memdc);

Bond
August 13th, 2004, 11:50 AM
When you create your bitmap and give it a width and height, it has coordinates of:
(0, 0, width, height)

This doesn't match the coordinates you specify in your blankRC rect.

For example, you create a bitmap that is 240 x 70. Therefore, the bitmap coordiates are (0, 0, 240, 70). When you draw text on it, though, you are specifying a rect with dims (0, 30, 240, 70), so your text may appear lower than you want. Unless this IS what you want?

scomet1
August 13th, 2004, 12:48 PM
thank you Bond - I'm very tired, I have to finish this project today and I've been working forever so I'm missing things like that. However, I'm still having the same problem where there is a bad flicker and for some reason stuff is being left over I guess from previous runs - because there is all kinds of noise showing up. Any ideas?

scomet1
August 13th, 2004, 12:50 PM
This may also be a problem. I'm working in visual c++ embedded 4.0. The commands have been the same though so far for this stuff.

Bond
August 13th, 2004, 02:47 PM
...stuff is being left over I guess from previous runs - because there is all kinds of noise showing up. Any ideas?
Hmm. Sounds like maybe WM_ERASEBKGND isn't doing it's job. When Windows updates an invalidated region, it will send a couple of messages your way. The first is WM_ERASEBKGND and the second is WM_PAINT. WM_ERASEBKGND allows you to do custom erasing of this invalidated region. If you return TRUE from this message, then you are telling Windows that you have erased it yourself and that it shouldn't bother. If you pass the message on to Windows (or return FALSE from a DialogProc), then Windows will use the brush specified in the WNDCLASS to erase the background of the invalidated region.

May be something to check out...

scomet1
August 13th, 2004, 04:28 PM
Thank you again,

I tried adding in some code for that message:

case WM_ERASEBKGND:
return DefWindowProc(hWnd, message, wParam, lParam);
break;

The weird thing was that it seemed to work - but then it stopped and went back to the old way! I don't know whats up with this thing....I have to go and post another problem now!

Thank you.