Can someone tell me what I need to do in order to draw a bitmap onto the client area of a window? I have no problems with loading the bitmap (and I can "select" it into a Bitmap Static control), but I can't figure out how to draw it directly to the window DC. I'd like to know how to do this so that I could have more control over size and placement - placing images side to side for example. Any help will be appreciated.

Spara
Posted on 2004-09-06 14:53:09 by Sparafusile
getdc / selectobject / bitblt
Posted on 2004-09-06 15:13:19 by lifewire
getdc / selectobject / bitblt


I've done that, but the image doesn't show up at all.

Actualy I used BeginPaint/EndPaint, but it's the same thing.

When I use BitBlt, what do I pass as the source DC? The handle to the bitmap?
Posted on 2004-09-06 15:38:06 by Sparafusile
From Iczelion's Simple Bitmap tutorial:


WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL ps:PAINTSTRUCT
LOCAL hdc:HDC
LOCAL hMemDC:HDC
LOCAL rect:RECT
.if uMsg==WM_CREATE
invoke LoadBitmap,hInstance,IDB_MAIN
mov hBitmap,eax
.elseif uMsg==WM_PAINT
invoke BeginPaint,hWnd,addr ps
mov hdc,eax
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
invoke EndPaint,hWnd,addr ps
.
.
.
Posted on 2004-09-06 15:45:57 by Vortex
Thank you, that's exactly what I needed. I'm ashamed I didn't check there first, but now alteast I know :)
Posted on 2004-09-06 15:52:24 by Sparafusile
From Iczelion's Simple Bitmap tutorial:


WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL ps:PAINTSTRUCT
LOCAL hdc:HDC
LOCAL hMemDC:HDC
LOCAL rect:RECT
.if uMsg==WM_CREATE
invoke LoadBitmap,hInstance,IDB_MAIN
mov hBitmap,eax
.elseif uMsg==WM_PAINT
invoke BeginPaint,hWnd,addr ps
mov hdc,eax
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
invoke EndPaint,hWnd,addr ps
.
.
.


There is one thing to note with this example. You will have resource leaks on certain OSes because you aren't selecting the Bitmap back out before deleting the DC.

Just an FYI.

Also, if you want clean this up a little, don't make the call to GetClientRect and use the rectangle already in PS variable (which is the small size to be displayed. Just use those coords).

Relvinian
Posted on 2004-09-07 17:35:01 by Relvinian
The OSes to get resource leaks are Win95,98, 98SE and WinME iirc. Maybe WinCE too
Posted on 2004-09-08 03:15:37 by Ultrano