News:

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

Bitmap Icon

Started by RuiLoureiro, September 17, 2010, 09:29:29 PM

Previous topic - Next topic

RuiLoureiro


Hi all
          I have a bitmap image to be set as an icon in the main window.
          I need to do this.
          How to do it ? Could you help me ?

Thanks
RuiLoureiro

MichaelW

One possibility is load the icon and send the icon handle to the window in a WM_SETICON message:

http://msdn.microsoft.com/en-us/library/ms632643(VS.85).aspx

eschew obfuscation

Vortex

Hi RuiLoureiro,

Here is an example for you.


.IF uMsg==WM_INITDIALOG

        invoke  GetModuleHandle,0
        invoke  LoadIcon,eax,ICON_ID
        invoke  SendMessage,hWnd,WM_SETICON,ICON_SMALL,eax


xandaz

   Olá Rui. This example shows how to turn a bitmap into an icon. What you do afterwards its up to you.
   
.586
.model flat,stdcall
option casemap:none

WinMain PROTO   :DWORD,:DWORD,:DWORD,:DWORD

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\comctl32.inc
include \masm32\include\gdi32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\gdi32.lib


.const

MainWindowWidth equ     400
MainWindowHeight        equ     300

.data

hInstance       DWORD           ?
CommandLine     LPSTR           ?

hMainWindow     dd              ?
MainWindowClass db              'MainWindowClass',0
MainWindowName  db              'Bitmap 2 Icon',0

szBitmap                db              '.\bmp.bmp',0
hBmp                    dd               ?
hDC                       HDC           ?
hMemDC                   HDC            ?
bi                        BITMAPINFO            <>
BitmapBits              db                      512 dup(0)

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

WinMain PROC    hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD

        local   wc:WNDCLASSEX
        local   msg:MSG

        mov     wc.cbSize,sizeof wc
        mov     wc.style,CS_HREDRAW+CS_VREDRAW
        mov     wc.lpfnWndProc,offset WndProc
        mov     wc.lpszMenuName,NULL
        mov     wc.lpszClassName,offset MainWindowClass
        mov     wc.cbClsExtra,NULL
        mov     wc.cbWndExtra,NULL
        mov     wc.hbrBackground,COLOR_WINDOW
        push    hInst
        pop     wc.hInstance
        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  GetSystemMetrics,SM_CYSCREEN
        shr     eax,1
        sub     eax,MainWindowHeight/2
        push    eax
        invoke  GetSystemMetrics,SM_CXSCREEN
        shr     eax,1
        sub     eax,MainWindowWidth/2
        pop     ebx
        invoke  CreateWindowEx,NULL,addr MainWindowClass,addr MainWindowName,WS_TILEDWINDOW,\
                eax,ebx,MainWindowWidth,MainWindowHeight,NULL,NULL,hInstance,NULL
        mov     hMainWindow,eax
        invoke  ShowWindow,eax,SW_SHOWNORMAL
        invoke  UpdateWindow,hMainWindow

        .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:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD

        local   ps:PAINTSTRUCT
        local   ii:ICONINFO
       
        .if     uMsg==WM_DESTROY

                invoke  PostQuitMessage,NULL
        .elseif uMsg==WM_CREATE

        .elseif uMsg==WM_PAINT

                invoke  BeginPaint,hWnd,addr ps
                invoke  LoadImage,hInstance,addr szBitmap,IMAGE_BITMAP,64,64,LR_LOADFROMFILE
                mov     hBmp,eax                     
                mov     ii.fIcon,TRUE
                push    hBmp
                pop     ii.hbmMask
                push    hBmp
                pop     ii.hbmColor
                invoke  CreateIconIndirect,addr ii
                invoke  DrawIconEx,ps.hdc,66,0,eax,64,64,NULL,NULL,DI_IMAGE
                invoke  CreateCompatibleDC,ps.hdc
                mov     hMemDC,eax
                invoke  SelectObject,hMemDC,hBmp
                invoke  StretchBlt,ps.hdc,0,0,64,64,hMemDC,0,0,64,64,SRCCOPY
                invoke  EndPaint,hWnd,addr ps
               
        .else

                invoke  DefWindowProc,hWnd,uMsg,wParam,lParam
                ret

        .endif

        xor     eax,eax
        ret

WndProc endp

end     start

RuiLoureiro

MichaelW,
          Thanks. It doesnt help me
Vortex,
         Thanks. It doesnt do what i want
         You have an icon  «100 ICON "Smiley.ico"»  defined in rsrc.rc
         I have only an image «Image1.bmp»

Olá xandaz,
         Thanks. It doesnt do what i want.
         It prints the bmp image in the client area.
         The icon in the main window is defined in
         
         invoke  LoadIcon,NULL,IDI_APPLICATION

         I want «Image1.bmp» as the main window icon

Thanks

xandaz

this is all you should do then:


     
     ...some other WNDCLASSEX initializations....

     invoke  LoadImage,hInstance,addr szBitmap,IMAGE_BITMAP,64,64,LR_LOADFROMFILE
                mov     hBmp,eax                     
                mov     ii.fIcon,TRUE
                push    hBmp
                pop     ii.hbmMask
                push    hBmp
                pop     ii.hbmColor
                invoke  CreateIconIndirect,addr ii
     mov wc.hIcon,eax
     mov wc.hIconSm,eax
    ...
     invoke RegisterClassEx,addr wc

Bye

Vortex

Combining the code from xandaz with mine :



    .IF uMsg==WM_INITDIALOG

        invoke  GetModuleHandle,0
        mov     hInst,eax

        ; Code portion from xandaz

        invoke  LoadImage,eax,ADDR szBmp,IMAGE_BITMAP,ICON_SIZE,ICON_SIZE,LR_LOADFROMFILE
        mov     hBmp,eax                     
        mov     ii.fIcon,TRUE
        push    hBmp
        pop     ii.hbmMask
        push    hBmp
        pop     ii.hbmColor
        invoke  CreateIconIndirect,ADDR ii

        invoke  SendMessage,hWnd,WM_SETICON,ICON_SMALL,eax


xandaz

oh... and you should have a second bitmap with the mask or else it doesn't work so good. Where's white becomes black.

   ...some other WNDCLASSEX initializations....

WinMain PROC blah,blah...
    invoke  LoadImage,hInstance,addr szBitmap,IMAGE_BITMAP,64,64,LR_LOADFROMFILE
               mov     hBmp,eax                      
    invoke  LoadImage,hInstance,addr szBitmapMask,IMAGE_BITMAP,?,?,LR_LOADFROMFILE
               mov     ii.fIcon,TRUE
               push    eax
               pop     ii.hbmMask
               push    hBmp
               pop     ii.hbmColor
               invoke  CreateIconIndirect,addr ii
    mov wc.hIcon,eax
    mov wc.hIconSm,eax
   ...
    invoke RegisterClassEx,addr wc
    ....
ret
WinMain endp


ty vortex... i thought he might want to do it in WinMainProc... i guess i should have mentioned.

RuiLoureiro

Vortex,
            Obrigado pelo teu exemplo,  :wink
            Thank you so much for your example !
Rui

Olá xandaz,
           It works correctly as a file or in rsrc.rc !
            Muito obrigado pelo teu código  :wink
           Thank you so much
            The problem is solved !
RuiLoureiro