Hi from France
I'm newbie in masm.
I try to center my bitmap to client area, and this is a problem for me.
Please, Could you help me !!!

--------------------------

.code
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke GetCommandLine
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
mov wc.cbSize,SIZEOF WNDCLASSEX ;size in bytes of WNDCLASSEX structure
mov wc.style, CS_HREDRAW or CS_VREDRAW ;window style
mov wc.lpfnWndProc, OFFSET WndProc ;address of user lpfnWndProc function
mov wc.cbClsExtra,NULL ;extra bytes to allocate set to 0
mov wc.cbWndExtra,NULL ;class directive in rc file
push hInstance
pop wc.hInstance
mov wc.hbrBackground,02h ;background,1=background(parameter+1)
mov wc.lpszMenuName,NULL ;menu name in resource file,0=no menu
mov wc.lpszClassName,OFFSET ClassName ;name of windows class
invoke LoadIcon,NULL,IDI_APPLICATION ;- API Function -
mov wc.hIcon,eax
mov wc.hIconSm,eax ;iconhandle in eax
invoke LoadCursor,NULL,IDC_ARROW ;- API Function -
mov wc.hCursor,eax ;handle of the cursor
invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,
NULL,
ADDR ClassName,
NULL,
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
250,
250,
NULL,
NULL,
hInstance,
NULL
mov hwnd,eax ;hwnd,return value=handle of window
invoke ShowWindow, hwnd,SW_SHOWMAXIMIZED ;Hwnd,handle of window, nCmdShow,show state 1=SW_SHOWNORMAL
invoke UpdateWindow, hwnd ;handle window (updates the client area sending a WM_PAINT)
.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg ;translates virtual-key messages -->character messages(ASCII
invoke DispatchMessage, ADDR msg ;dispatches a message to a window procedure.
.ENDW
mov eax,msg.wParam
ret
WinMain endp

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
.elseif uMsg==WM_DESTROY
invoke DeleteObject,hBitmap
invoke PostQuitMessage,NULL
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF
xor eax,eax
ret
WndProc endp
end start

------------------------------------
mailto:\\fabrice.metge@m6net.fr
-------------------------------------
Thanks
Posted on 2001-09-11 06:17:06 by HAPKI
get rect.right and divide it by 2 ; put in say a variable X1

get your bitmap width and divide by 2 ; put in say a variaable BMW1

mov eax, x1
sub eax, bmw1

eax will now contain the x value for putting the bitmap on screen

do the same for rect.bottom and the bitmap height
Posted on 2001-09-11 11:56:15 by titan
or..
subtract bitmap width from client width, divide by 2. (same for height)
Posted on 2001-09-11 15:44:01 by vcv
HAPKI i can see that by your post you probably have the same problem i was having for a while being new to MASM.

____________________________________________________
invoke BitBlt,hdc,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY

____________________________________________________


Your bitmap in this case is always shown in the upper left corner of the screen.....no?

Take advantage of the variables you are allowed.

like this

invokeBitBlt,hdc,widthmiddle,heightmiddle,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY

Widthmiddle is the (width of the client area minus the width of your bitmap) divided by 2. Widthmidddle will contain the x value for bitmap.

You can use the registers to accomplish the mathematical operations like in the posts above.

LOOK AT THIS:

mov eax,rect.right

you now have the client area width in eax
in other words if your display is 640 * 480 then eax == 640

__________________________________________________
rect.right gets defined when you call this

invoke GetClientRectangle

because the function fills in a structure with certain values
______________________________________________
These are also values that get defined

rect.left ( the x origin of the client area )

so is rect.top ( the y origin of the client area)

and finally rect.bootom will contain 480 ( the height of the client area)

Actually because of the window bar with menu etc ...these values are not exact.

The point is that you don't need the exact value if you have the rect.whatever variables.


Of course you need to know your bitmap width and heght values if you really want it centered.

You can use "GetObject" with the handle to your bitmap and a pointer to the buffer for the bitmap to get these values.



Live Long And Prosper.
Posted on 2001-09-13 22:38:59 by titan
You need to know width and hight of bmp you load.
Then you can calculate centering.
Posted on 2001-09-14 06:49:40 by The Svin