News:

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

BegingPaint to GetDC - Problems

Started by RHL, April 20, 2012, 09:10:51 PM

Previous topic - Next topic

RHL

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

jj2007

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?

RHL

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

jj2007

"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, third-last bullet.

RHL

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

jj2007

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

qWord

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?
FPU in a trice: SmplMath
It's that simple!

dedndave

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

dedndave

also - please show us the resource file   :U

RHL

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

dedndave

#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


qWord

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.
FPU in a trice: SmplMath
It's that simple!

RHL

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

dedndave

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

jj2007

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, 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