The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: RHL on April 20, 2012, 09:10:51 PM

Title: BegingPaint to GetDC - Problems
Post by: RHL on April 20, 2012, 09:10:51 PM
hi all guys, i have problems for "convert" this code:
this work code correctly, it use the WM_PAINT message for drawing a bitmap
( first copy a 64x64 from bitmap to temporary memory, next, copy 32x32 from bitmap temporary to destiny final.... )

all this in the WM_PAINT,but i want do it in any part of program, for example, i want do this process when a button control is press...

I investigated, and i think that i will have do it with GETDC function because the BegingPaint only work in WM_PAINT messsage, right? :)
I tried with GETDC but i do not work : (

the work code ( in WM_PAINT ) thanks to dedndave, he helped me do this code, well... he did this code  :bg thanks a lot, man  :bg


       LOCAL   PS:PAINTSTRUCT
       LOCAL   hdc1:HDC
       LOCAL   hdc2:HDC
       LOCAL   hdc3:HDC
       LOCAL   hBmpOrg2:DWORD
       LOCAL   hBmpOrg3:DWORD


.elseif eax==WM_PAINT


    ;invoke BeginPaint,hstatic,addr PS   ; hstatic, handle to static control ( final destiny )
    invoke GetDC,NULL                      ;also i tried do this ( with NULL ) , but it drawn on the screen :|

    mov hdc1,eax

    invoke CreateCompatibleDC,hdc1
    mov hdc2,eax

    invoke CreateCompatibleDC,hdc1
    mov hdc3,eax

    invoke SelectObject,hdc2,hBITMAP
    mov hBmpOrg2,eax

            invoke  CreateCompatibleBitmap,hdc1,64,64

           invoke  SelectObject,hdc3,eax
           mov     hBmpOrg3,eax                           ;save the original handle from hdc3

           invoke  BitBlt,hdc3,0,0,64,64,hdc2,0,0,SRCCOPY ;i try copy to temporary....

           invoke  BitBlt,hdc1,0,0,32,32,hdc3,0,0,SRCCOPY ;now, copy to final

           invoke  SelectObject,hdc3,hBmpOrg3
           invoke  DeleteObject,eax

           invoke  SelectObject,hdc2,hBmpOrg2
           invoke  DeleteObject,eax

           invoke  DeleteDC,hdc3
           invoke  DeleteDC,hdc2
           invoke  ReleaseDC,hstatic,hdc1


I tried do it, so:


invoke GetDC, Hanldestatic control

....


but i do not work :/
any help, thanks guys


EDIT: investigate some code done in C++ but I find nothing :/
in actually, it works with GetDC but it draw 32x32 size, ok, but all colour black and it not the bitmap
Title: Re: BegingPaint to GetDC - Problems
Post by: jj2007 on April 21, 2012, 07:32:19 AM
Quote from: RHL on April 20, 2012, 09:10:51 PM
I tried with GETDC but i do not work : (

And we have to guess what did not work, right?
Title: Re: BegingPaint to GetDC - Problems
Post by: RHL on April 21, 2012, 07:38:24 AM
Quote from: jj2007 on April 21, 2012, 07:32:19 AM
Quote from: RHL on April 20, 2012, 09:10:51 PM
I tried with GETDC but i do not work : (

And we have to guess what did not work, right?

mm? because you say it, jochen? : P
I not know, because does not work : P
Title: Re: BegingPaint to GetDC - Problems
Post by: jj2007 on April 21, 2012, 09:22:32 AM
"does not work" =

1. Error XXX in line nnn
2. assembles fine but don't see the result

In both cases, to understand where the problem is, we need to see the full code. See also my tips & tricks (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm), third-last bullet.
Title: Re: BegingPaint to GetDC - Problems
Post by: RHL on April 21, 2012, 04:37:39 PM
sorry, ok, my full code:

.386
.model flat,stdcall
option casemap:none

include \MASM32\INCLUDE\masm32rt.inc

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD



IDBITMAP equ 300   ; BITMAP

.data
    ClassName db "MainWinClass",0
    AppName  db "Main Window",0
   
    classbutton db "Button",0
    anyname db "name",0
classStatic db "Static",0

hstatic dd 0
hbutton dd 0

hBITMAP dd 0
.data?
   hInstance HINSTANCE ?
   CommandLine LPSTR ?

.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
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,NULL
push  hInstance
pop   wc.hInstance
mov   wc.hbrBackground,COLOR_BTNFACE+1
mov   wc.lpszMenuName,NULL
mov   wc.lpszClassName,OFFSET ClassName

invoke LoadIcon,NULL,IDI_APPLICATION
mov   wc.hIcon,eax
mov   wc.hIconSm,eax

invoke LoadCursor,NULL,IDC_ARROW
mov   wc.hCursor,eax

invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
           CW_USEDEFAULT,100h,100h,NULL,NULL,\
           hInst,NULL
mov   hwnd,eax

invoke ShowWindow, hwnd,SW_SHOWNORMAL
invoke UpdateWindow, hwnd

.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW

mov     eax,msg.wParam
ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL   PS:PAINTSTRUCT
        LOCAL   hdc1:HDC
        LOCAL   hdc2:HDC
        LOCAL   hdc3:HDC
        LOCAL   hBmpOrg2:DWORD
        LOCAL   hBmpOrg3:DWORD

.IF uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_CREATE
invoke CreateWindowEx,NULL,addr classbutton,addr anyname,WS_VISIBLE or WS_CHILD or WS_VISIBLE, \
0,0,50h,30h,hWnd,NULL,hInstance,NULL
mov hbutton,eax

invoke CreateWindowEx,NULL,addr classStatic,addr anyname,5000000Eh, \
70h,0,50,30,hWnd,NULL,hInstance,NULL
mov hstatic,eax

invoke LoadBitmap,hInstance,IDBITMAP
mov hBITMAP,eax

.ELSEIF uMsg==WM_COMMAND

invoke GetDC,hstatic
mov hdc1,eax
invoke CreateCompatibleDC,hdc1
mov hdc2,eax
invoke CreateCompatibleDC,hdc1
mov hdc3,eax

invoke SelectObject,hdc2,hBITMAP
mov hBmpOrg2,eax
invoke  CreateCompatibleBitmap,hdc1,64,64
        nvoke  SelectObject,hdc3,eax
mov     hBmpOrg3,eax                           ;save the original handle from hdc3
invoke  BitBlt,hdc3,0,0,64,64,hdc2,0,0,SRCCOPY ;i try copy to temporary....
        invoke  BitBlt,hdc1,0,0,64,64,hdc3,0,0,SRCCOPY ;now, copy to static control
        invoke  SelectObject,hdc3,hBmpOrg3
        invoke  DeleteObject,eax
        invoke  SelectObject,hdc2,hBmpOrg2
        invoke  DeleteObject,eax
        invoke  DeleteDC,hdc3
        invoke  DeleteDC,hdc2
        invoke  ReleaseDC,hstatic,hdc1
    xor eax,eax
; result , the picture all black color
    ret
.ELSE
;invoke CallWindowProc,WndProc,hWnd,uMsg,wParam,lParam
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF

xor eax,eax
ret
WndProc endp
end start
Title: Re: BegingPaint to GetDC - Problems
Post by: jj2007 on April 21, 2012, 05:27:38 PM
It works.

   .ELSEIF uMsg==WM_COMMAND
      invoke   GetDC,hstatic
      mov      hdc1,eax
      invoke   CreateCompatibleDC,hdc1
      mov      hdc2,eax
      ;   deb 4, "A", hdc1, hdc2,hBITMAP
      ;   invoke   CreateCompatibleDC,hdc1
      ;   mov      hdc3,eax

      invoke   SelectObject,hdc2,hBITMAP
      mov      hBmpOrg2,eax
      ;   invoke  CreateCompatibleBitmap,hdc1,64,64
      ;        invoke  SelectObject,hdc3,eax
      ;   mov     hBmpOrg3,eax                           ;save the original handle from hdc3
      ;   invoke  BitBlt,hdc3,0,0,64,64,hdc2,0,0,SRCCOPY ;i try copy to temporary....
      invoke  BitBlt,hdc1,0,0,64,64,hdc2,0,0,SRCCOPY ;now, copy to static control
      ;        invoke  SelectObject,hdc3,hBmpOrg3
      ;        invoke  DeleteObject,eax
      invoke  SelectObject,hdc2,hBmpOrg2
      invoke  DeleteObject,eax
      ;          invoke  DeleteDC,hdc3
      invoke  DeleteDC,hdc2
      invoke  ReleaseDC,hstatic,hdc1
      ; invoke ValidateRect, hWnd, 0
      xor eax, eax
      ; result , the picture all black color
      ret
Title: Re: BegingPaint to GetDC - Problems
Post by: qWord on April 21, 2012, 05:36:50 PM
I think this "out of order" drawing is not a good idea, because the control can override it. Why not assign the bitmap to the control?
Title: Re: BegingPaint to GetDC - Problems
Post by: dedndave on April 21, 2012, 06:12:07 PM
buttons are hard to work with   :P

but - there is an example in the examples folder called "bmbutton" - see if that is any help  :U
Title: Re: BegingPaint to GetDC - Problems
Post by: dedndave on April 21, 2012, 06:20:44 PM
also - please show us the resource file   :U
Title: Re: BegingPaint to GetDC - Problems
Post by: RHL on April 21, 2012, 09:40:22 PM
jochen, no, that code i do not work : P does not show nothing

qWord, but I do not want to draw all bitmap, for that , I thought first draw in something temporary : P

dave, my resoure file  :D

#include "\masm32\include\resource.h"
#define IDI_BITMAP1  123
IDI_BITMAP1 BITMAP    DISCARDABLE "tool1.bmp"


hey the sample, bmbutton- is very interesant
Title: Re: BegingPaint to GetDC - Problems
Post by: dedndave on April 21, 2012, 10:09:40 PM
#include "\masm32\include\resource.h"
#define IDI_BITMAP1  123
IDI_BITMAP1 BITMAP    DISCARDABLE "tool1.bmp"


there's your problem - or at least, one problem

IDBITMAP  equ 300   ; BITMAP

invoke LoadBitmap,hInstance,IDBITMAP
mov hBITMAP,eax
;put a temporary MessageBox here to display the handle
invoke MessageBox,0,uhex$(eax),0,0
;if it displays 00000000, then you have no bitmap to select

Title: Re: BegingPaint to GetDC - Problems
Post by: qWord on April 21, 2012, 10:39:27 PM
Quote from: RHL on April 21, 2012, 09:40:22 PMqWord, but I do not want to draw all bitmap, for that , I thought first draw in something temporary : P
You can create a subclass which has a own data set and adds some new messages for storing, replacing and deleting (...) associated bitmaps.
Title: Re: BegingPaint to GetDC - Problems
Post by: RHL on April 21, 2012, 11:01:41 PM
Quote from: dedndave on April 21, 2012, 10:09:40 PM
#include "\masm32\include\resource.h"
#define IDI_BITMAP1  123
IDI_BITMAP1 BITMAP    DISCARDABLE "tool1.bmp"


there's your problem - or at least, one problem

IDBITMAP  equ 300   ; BITMAP

invoke LoadBitmap,hInstance,IDBITMAP
mov hBITMAP,eax
;put a temporary MessageBox here to display the handle
invoke MessageBox,0,uhex$(eax),0,0
;if it displays 00000000, then you have no bitmap to select



dave, for a moment, i think this was the error : P
but change the name and ID, and nothing happened : P same result ( the picture all black color )

in fact, the code works in the WM_PAINT message : P using BeginPaint... also I tried use GetDC in WM_PAINT y get the same result ( the picture all black color )
maybe the error is a problem with monochrome...


qWord: well yeah man, I think leave it and do that you say , use bitblt and subclassing : P
Title: Re: BegingPaint to GetDC - Problems
Post by: dedndave on April 21, 2012, 11:11:35 PM
according to the documentation, the only time you are supposed to paint stuff is during WM_PAINT or WM_DRAWITEM
and - maybe one or two others that don't come to mind (NCPAINT i think - for non-client area - ERASEBACKGROUND)
that does not mean that it can't be done at other times

however - let's say you did have the picture in there
now - the user moves another window over your window
then, he removes that window
you have no way of knowing that the picture needs to be redrawn, unless you get a message telling you to redraw it   :P
those messages are WM_PAINT and WM_DRAWITEM

so - ok - did you put a test MessageBox in ?   :bg
Title: Re: BegingPaint to GetDC - Problems
Post by: jj2007 on April 21, 2012, 11:26:18 PM
Quote from: dedndave on April 21, 2012, 10:09:40 PM
#include "\masm32\include\resource.h"
#define IDI_BITMAP1  123
IDI_BITMAP1 BITMAP    DISCARDABLE "tool1.bmp"


there's your problem - or at least, one problem

IDBITMAP  equ 300   ; BITMAP

invoke LoadBitmap,hInstance,IDBITMAP
mov hBITMAP,eax
;put a temporary MessageBox here to display the handle
invoke MessageBox,0,uhex$(eax),0,0
;if it displays 00000000, then you have no bitmap to select



RHL,

My code works perfectly, but remember I had no resource file, so I created one, with a correct ID. I suggest you copy & paste my code above, and fix the ID problem in the resource file. If that doesn't help, install MasmBasic (http://www.masm32.com/board/index.php?topic=12460), then console assemble & link and run the attached asm file (*.asc if for use with Richmasm, *.asm for use with another editor).
You should see your bitmap when clicking on the button, and something similar to the output below.

WM_Create OK    hBITMAP         -1241184147

A
hdc1            2046888890
hdc2            -385808612
hBITMAP         -1241184147
SelObj  hBmpOrg2        25493519
BitBlt  eax             1
SelObj  eax             -1241184147
DelObj  eax             1
DelDC   eax             1
RelDC   eax             1
Title: Re: BegingPaint to GetDC - Problems
Post by: RHL on April 21, 2012, 11:28:28 PM
hey! , it worked :D  :red
I do not know, but now work yes! dave,  surely was the bitmap ID :S
im sorry guys, my bad!! fuk! sorry :S but is rare...


.ELSEIF uMsg==WM_COMMAND
;invoke BeginPaint,hstatic,addr PS
invoke GetDC,hstatic
mov hdc1,eax
invoke CreateCompatibleDC,hdc1
mov hdc2,eax
invoke CreateCompatibleDC,hdc1
mov hdc3,eax

invoke SelectObject,hdc2,hBITMAP
mov hBmpOrg2,eax
invoke  CreateCompatibleBitmap,hdc1,64,64
       invoke  SelectObject,hdc3,eax
mov     hBmpOrg3,eax                           ;save the original handle from hdc3
invoke  BitBlt,hdc3,0,0,64,64,hdc2,0,0,SRCCOPY ;i try copy to temporary....
       invoke  BitBlt,hdc1,0,0,30,30,hdc3,0,0,SRCCOPY ;now, copy to static control
       invoke  SelectObject,hdc3,hBmpOrg3
       invoke  DeleteObject,eax
       invoke  SelectObject,hdc2,hBmpOrg2
       invoke  DeleteObject,eax
      invoke  DeleteDC,hdc3
      invoke  DeleteDC,hdc2
      invoke  ReleaseDC,hstatic,hdc1
  xor eax,eax
  ret


thanks all, for your time :bg, jochen ,sorry, you had reason, should have copied all the code
Title: Re: BegingPaint to GetDC - Problems
Post by: dedndave on April 22, 2012, 01:16:42 AM
try putting another window in front of it, then moving it away   :P
Title: Re: BegingPaint to GetDC - Problems
Post by: jj2007 on April 22, 2012, 01:23:38 AM
Quote from: dedndave on April 22, 2012, 01:16:42 AM
try putting another window in front of it, then moving it away   :P

WM_PAINT does not trigger the redraw, you have to click again. It's by design (but RHL may change design soon, I guess)

Greetz to Z,
J
Title: Re: BegingPaint to GetDC - Problems
Post by: dedndave on April 22, 2012, 01:31:29 AM
Z sends a K, Jochen   :P

yah - it's hard for him to read all that painting and drawing documentation
even so - it's a lot to absorb if you speak English - lol
then - you have to play around with it before it really sinks in

at the moment, i am working on some image display code that Keith wanted to play with
it takes a lot of careful examination to see when scroll bars etc are updated
i just learned that status bar borders don't seem to work with xp theme/common controls 6
if i disable the manifest, i get exactly what i asked for - no xp theme, of course
and another one that apparently never did work - TBS_DOWNISLEFT
point being - reading the documentation is not always enough