Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does SelectObject(dc, hBMP) do?

Tags:

winapi

gdi

I guess I do understand what happens if I select "an object" into a "device contect", and if this object is a brush, font, pen: The charactersicts of these objects are hence forth used for the drawing onto the device context.

However, I don't understand what happens after I select a bitmap into the device context, much less, why it is necessary to do so.

I have a program that somehwere does

HDC dc = ::GetDC(hWnd);

ffBitmap   = ::CreateCompatibleBitmap(dc, windowWidth, windowHeight);
ffMemoryDc = ::CreateCompatibleDC(dc);

hOldBitmap          = (HBITMAP) ::SelectObject(ffMemoryDc, ffBitmap);

and later draws unto the ffMemoryDc and then *BitBlt*s the ffMemoyDc into the real device context of a window.

BitBlt ( dc, 
         0, 0, windowWidth, windowHeight,
         ffMemoryDc,
         0, 0,
         SRCCOPY
       );

The variable ffBitmap is referenced nowhere else in the entire program, yet, if I don't SelectObject(ffBitmap), nothing is drawn, so it is necessary.

I'd appreciate if someone could shed some light what happens here.

like image 712
René Nyffenegger Avatar asked Jan 17 '11 00:01

René Nyffenegger


1 Answers

That's how it work. The bitmap you select in the DC is the bitmap that is "painted on". Quoting MSDN:

Before an application can use a memory device context for drawing operations, it must select a bitmap of the correct width and height into the device context.

like image 68
jpalecek Avatar answered Nov 23 '22 07:11

jpalecek