News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bitmaps and DC

Started by msmith, July 11, 2006, 02:34:04 AM

Previous topic - Next topic

msmith

If I already have a DC (it already contains a retangle and text) and I have a valid bitmap handle, how do I get the bitmap to appear in the rectangle with the text?

I have seen examples of CreateCompatibleDC which the bitmap is selected into and then there is a bitblt form the compatible dc to the "viewing" (or rectangle) dc.

Is there no way to just put the bitmap into the rectangle dc?

msmith

I have tried the following code where ControlDCTemp is the display DC and ToolBMP is the variable containing a valid bitmap, but the bitmap does not appear in the display.


invoke CreateCompatibleDC,[ControlDCTemp]
mov [MemDC],eax
invoke SelectObject,[MemDC],[ToolBMP]
invoke BitBlt,[ControlDCTemp],0,0,16,16,[MemDC],0,0,SRCCOPY



ChrisLeslie

msmith

I have used the following:
QuoteWndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM 
    LOCAL ps:PAINTSTRUCT
    LOCAL hBmp :DWORD
    LOCAL memDC:DWORD   
    etc
    etc

.IF etc etc etc

.ELSEIF uMsg==WM_PAINT             
      invoke BeginPaint,hWnd,ADDR ps
      invoke CreateCompatibleDC, ps.hdc
      mov    memDC, eax
      invoke CreateCompatibleBitmap, ps.hdc, 750, 550
      mov    hBmp,  eax
      invoke SelectObject, memDC, hBmp

           ; paint to memDC here

      invoke BitBlt,       ps.hdc, 10, 10, 750, 550, memDC, 0, 0, SRCCOPY
      invoke DeleteObject, hBmp
      invoke DeleteDC,     memDC   
      invoke EndPaint,hWnd,ADDR ps
.ELSEIF
    etc
    etc
    etc


I hope you can get what you need from this snippet.

Chris

msmith

Chris,

Thanks for the quick reply.

I don't understand the CreateCompatibleBitmap part. I don't see where you actually got the bitmap bits (image). Where does my valid bitmap handle fit into this?

Mike

ChrisLeslie

My interpretation is that it is where the bit map is created. It then should be associated and compatible with the DC, as well as specifying pixel width and height. Have you read the msdn for CreateCompatibleBitmap? When you invoke a TextOut, for example, instead of specifying the handle to the DC, specify the handle to the compatible bit map in memory, memDC in my snippet.

Chris

msmith

I am trying to say... where is the actual bitmap (in my example ToolBMP) even mentioned. How is the handle of the actual bitmap assigned in you example?

msmith

I got it to work by adapting Icezlion's example in Tutorial 25. The following code is excerpted from that tutorial:


invoke CreateCompatibleDC,hdc
mov hMemDC,eax
invoke SelectObject,hMemDC,hBitmap
invoke GetClientRect,hWnd,addr rect
invoke BitBlt,hdc,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY
invoke DeleteDC,hMemDC


The key thing for me is the line where hBitmap is a valid handle to an actual bitmap being assinged in the SelectectObject function.

Chris, I really appreaciate your help, but I could not see where a valid bitmap handle was ever assigned, so I couldn't get your example to work for me.

Tedd

BeginPaint sets up a 'screen dc' for you to draw your stuff onto the visible screen.
CreateCompatibleDC creates a dc in memory for you to draw into (this is not associated with the screen; though it does have compatible attributes, hence the name)
CreateCompaibleBitmap creates an empty bitmap in memory (of the size requested)
SelectObject (as used in this case) associates the bitmap (just created above) with the (in-memory) dc - this has the effect of making the dc contain the actual bitmap pixels (and sizing the dc to the size of the given bitmap)

[at this point the in-memory bitmap is actually blank/junk so you draw whatever you like in the bitmap/dc and those pixels are stored]

BitBlt (in this instance) copies the pixels from one dc (the in-memory one that contains our in-memory bitmap) to the screen dc - causing the pixels to be visible on the screen (in the window)
DeleteObject.. - cleanup dc and bitmap (say 'no!' to memory leaks)
EndPaint cleans up a few things and makes sure the window is redrawn (and thus the pixels we BitBlt'd appear)


If you already have a bitmap (handle) you want to draw to the screen, then there's no need to CreateCompatibleBitmap as you can just SelectObject it into the compatible dc and that will have required effect.
(And don't forget to delete a bitmap once you've finished with it - this should be AFTER you've deleted any dc it was selected into.)
No snowflake in an avalanche feels responsible.

falcon

 thanks for the long explanation Tedd...it helped me understand this stuff better :U