The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: xandaz on July 26, 2011, 04:33:25 PM

Title: How to keep the Device Context unchanged?
Post by: xandaz on July 26, 2011, 04:33:25 PM
   How does one maintain the Device Context unchanged when, for example, there is a sizeing operation that covers it? Everything disappears... I tried gdiflush, i tried SaveDC restore DC but nothing...Thanks
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on July 26, 2011, 05:03:34 PM
Create your drawing in an memory DC. While handling WM_PAINT, you can then copy this DC to the screen(-DC).
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 26, 2011, 05:14:20 PM
   i get i qword but does that works with LineTo and Arc and all those drawing functions? dont i need to select the object to bitblt it from a backbuffer? Can you show a small example? Thanks
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 26, 2011, 05:19:58 PM
   Qword you mean something like:
.if uMsg==WM_PAINT
   push hMemDC´
   pop pt.hdc
   invoke BeginPaint,hWnd,addr pt
   invoke EndPaint,hWnd,addr pt


???? HELP
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on July 26, 2011, 05:28:24 PM
see attachment
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 26, 2011, 05:34:42 PM
    Thanks Q.... thanks a lot. Bye and laters
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 26, 2011, 05:59:58 PM
    Q? Does it take all that? Why do you Create a Compatible bitmap then select it , then delete the previous object, then paint the currently selected one? I'm not sure i follow....
   thanks
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on July 26, 2011, 06:07:26 PM
The code creates an new DC and selects a bitmap with the current desktop-bit-depth  -> this our memory DC, where the drawing goes.
The DeleteObject() is not really needed because the DC is create with an default 1bpp, 1x1pixel bitmap, which can't be deleted.

WndProc proc uses ebx esi edi hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL ps:PAINTSTRUCT

.if uMsg == WM_CLOSE
invoke PostQuitMessage,NULL
.elseif uMsg == WM_DESTROY
invoke DeleteDC,rv(GetWindowLong,hWnd,WND_DATA.hdc)
.elseif uMsg == WM_CREATE
; create memory DC
mov esi,rv(GetDC,rv(GetDesktopWindow))
mov ebx,rv(CreateCompatibleDC,esi)
mov edi,rv(GetSystemMetrics,SM_CXSCREEN)
invoke SelectObject,ebx,rv(CreateCompatibleBitmap,esi,edi,rv(GetSystemMetrics,SM_CYSCREEN))
invoke ReleaseDC,rv(GetDesktopWindow),esi
invoke SetWindowLong,hWnd,WND_DATA.hdc,ebx

; clear DC
invoke BitBlt,ebx,0,0,10000,10000,ebx,0,0,PATCOPY

; draw to the DC
invoke MoveToEx,ebx,0,0,0
invoke LineTo,ebx,1000,1000
invoke LineTo,ebx,0,1000
invoke LineTo,ebx,1000,0

.elseif uMsg == WM_PAINT
invoke BeginPaint,hWnd,ADDR ps

; copy from memory DC to screen (ps.hdc)
mov ebx,rv(GetWindowLong,hWnd,WND_DATA.hdc)
mov edx,ps.rcPaint.right
sub edx,ps.rcPaint.left
mov ecx,ps.rcPaint.bottom
sub ecx,ps.rcPaint.top
invoke BitBlt,ps.hdc,ps.rcPaint.left,ps.rcPaint.top,edx,ecx,ebx,ps.rcPaint.left,ps.rcPaint.top,SRCCOPY
invoke EndPaint,hWnd,ADDR ps
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif

xor eax,eax
ret
WndProc endp
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 26, 2011, 06:25:56 PM
    Thanks a lot Q... It worked in my little gismo. I'll post it when i have something decent.... Thanks once more.
    All Hail MASM32 Forum....!!!
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 31, 2011, 05:32:08 PM
    Actually i came across another problem Qword. The draw comes only in black and white. Can you showme how to fix it? Do i need something instead of PATPAINT when creating the CompatibleBitmap or is it in WM_PAINT? Thanks
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on July 31, 2011, 05:57:20 PM
Quote from: xandaz on July 31, 2011, 05:32:08 PMThe draw comes only in black and white.
Probably you pass the DC from CreateCompatibleDC() directly to CreateCompatibleBitmap() - this cause creation of a monochrome bitmap. You must use a DC with a higher bit depth (e.g. the desktop DC) -> see my previous example.
[You may show us your code]
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 31, 2011, 07:07:38 PM
   This is the drawing gismo im working on qw. I tried using the desktop dc to use a higher depth as in your example but still nothing.
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on July 31, 2011, 07:40:49 PM
you need to replace hMemDC in line 255 with hDC.
I've seen, that you do not delete object returned by SelectObject().
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on July 31, 2011, 07:53:48 PM
    right .... that works . ty qw. I wonder if i'm ever going to fully understand this stuff. thanks
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on August 01, 2011, 12:13:36 AM
I've played a bit with you code -  maybe it helps you.

qWord
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on August 06, 2011, 11:25:15 PM
   Well thanks qword.... this is what i had done. I see that some of what you added is kind of better than what i did. I counldnt go around the region stuff when it relatesto filled spheres and also your segments work better than mine. You select a standing point and then grow the segement to wherever the mouse is going. Its good. Thanks a lot. See my stuff pls and say what you think.
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on August 07, 2011, 04:24:27 PM
   I'm trying to understand why some of the figures dont stick to the DC. I don't fully understand what you did in xandaz_paint. Why does it take all those DC's. I took what i think to understand of the things you did. I created an alternate DC to draw the figures and then redraw the bitmap so that theres no dragging when a specific figure is growing. look at q.
   I'd also like to say that you're pretty good at this Qw.
   Thanks and best regards.
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on August 07, 2011, 04:54:14 PM
hi,
while handling WM_PAINT,you must use TransparentBlt() to draw the preview (hTDC) to the screen. When the right mouse button releases (WM_LBUTTONUP), it is necessary to merge the preview layer hTDC with the memory DC hMemDC - this must also done using TransparentBlt(). After merging, the preview layer hTDC must cleared with the transparent color and the window must be invalidtaed.
Also:
- you do not delete object, if not longer needed (CreatePen)
- the BitBlt,..,10000,... was only example - you should use the bitmap's size.
- genrally: stay away from global variables - especially in context to your MDI stuff.

qWord
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on August 08, 2011, 07:04:05 PM
   Qw... i'm really sorry for being so presistent. What do is to merge the bitmap in hTDC with hMemDC using transparent blt and then the bitmap in hTDC using the transparent color which should be the color actually being used to draw right? I was looking at your example and it is very complicated to understand. Thanks for all the help Q.
   Best regards from X. :U
Title: Re: How to keep the Device Context unchanged?
Post by: qWord on August 08, 2011, 09:37:15 PM
hi,
in the attachment a simpler example with comments.
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on August 08, 2011, 09:41:18 PM
   thanks a lot Qw. I'll check it out. ty ty and nite
Title: Re: How to keep the Device Context unchanged?
Post by: xandaz on August 10, 2011, 11:20:13 PM
    well....thanks Q. It was hard to understand but things are finally settling down. Thanks a lot.