The MASM Forum

General => The Campus => Topic started by: xandaz on February 14, 2022, 06:30:27 AM

Title: how to transform an Icon into a Bitmap?
Post by: xandaz on February 14, 2022, 06:30:27 AM
  Hi guys. I'm using the following code but doesn't work. Help is appreciated invoke SHGetFileInfo,edi,0,addr shfi,sizeof shfi,SHGFI_SMALLICON or SHGFI_SHELLICONSIZE
invoke GetIconInfo,shfi.hIcon,addr iinfo
invoke CreateBitmap,20,20,1,16,addr iinfo.hbmColor
Title: Re: how to transform an Icon into a Bitmap?
Post by: Vortex on February 14, 2022, 06:46:49 AM
Hi xandaz,

Here is an example based on OleCreatePictureIndirect :

    mov         hLib,eax
    invoke      LoadIcon,eax,100
    mov         hIcon,eax

    mov         pd.cbSizeofstruct,SIZEOF PICTDESC   ; initialize the PICTDESC structure
    mov         pd.picType,PICTYPE_ICON
    push        hIcon
    pop         pd.icon.hicon
    invoke      OleCreatePictureIndirect,ADDR pd,ADDR IID_IPicture,TRUE,ADDR pBitmap
                                                    ; create the OLE image
    invoke      CreateStreamOnHGlobal,NULL,TRUE,ADDR pStream
                                                    ; create the destination stream to save the icon

    coinvoke    pBitmap,IPicture,SaveAsFile,pStream,TRUE,OFFSET pcbSize
    invoke      GetHGlobalFromStream,pStream,ADDR hGlobal
    invoke      GlobalLock,hGlobal                  ; get the address pointing the icon in memory
    invoke      WriteFileToDisc,ADDR IconName,eax,pcbSize
                                                    ; write the icon to disc
    coinvoke    pBitmap,IPicture,Release
    coinvoke    pStream,IStream,Release
    invoke      FreeLibrary,hLib
Title: Re: how to transform an Icon into a Bitmap?
Post by: xandaz on February 14, 2022, 07:28:35 AM
   Thanks Vortex. That's kinda complicated code. I was wondering if the code i wrote could be tweaked around a little bit to perform the same action. Can't it? ty
Title: Re: how to transform an Icon into a Bitmap?
Post by: Greenhorn on February 14, 2022, 07:49:30 AM
Doin' it the GDI way ...


hbm      HBITMAP   NULL
hIcon   HICON   NULL
hdcMem   HDC      NULL
hObjOld   HGDIOBJ   NULL

;...

invoke CreateCompatibleDC, 0
mov  hdcMem, rax
invoke CreateCompatibleBitmap, rax, nWidth, nHeight
mov  hbm, rax
invoke SelectObject, hdcMem, hbm
mov  hObjOld, rax
invoke DrawIcon, hdcMem, 0, 0, hIcon
invoke SelectObject, hdcMem, hObjOld
invoke DeleteDC, hdcMem
invoke DeleteObject, hIcon   ; Delete icon if no longer needed

;...
Title: Re: how to transform an Icon into a Bitmap?
Post by: xandaz on February 14, 2022, 08:06:45 AM
   That's a lot simpler Greenhorn. The problem i have is that it's for a menu. I'm not just drawing on a Device Context. So the problem is this.
Title: Re: how to transform an Icon into a Bitmap?
Post by: Greenhorn on February 14, 2022, 08:29:45 AM
Quote from: xandaz on February 14, 2022, 08:06:45 AM
The problem i have is that it's for a menu. I'm not just drawing on a Device Context. So the problem is this.
:biggrin:
You always draw into a Device Context.

However, if you want to display icons in your (ownerdrawn) menus, use image lists (https://docs.microsoft.com/en-us/windows/win32/controls/image-list-reference).
Title: Re: how to transform an Icon into a Bitmap?
Post by: xandaz on February 15, 2022, 06:51:14 AM
   Does anyone know what the includes are for GetIconInfoEx? Thanks
Title: Re: how to transform an Icon into a Bitmap?
Post by: jj2007 on February 15, 2022, 08:34:52 AM
Quote from: xandaz on February 15, 2022, 06:51:14 AM
   Does anyone know what the includes are for GetIconInfoEx? Thanks

Not included in user32.inc, so you need LoadLibrary plus GetProcAddress
Title: Re: how to transform an Icon into a Bitmap?
Post by: nidud on February 15, 2022, 09:04:51 AM
deleted
Title: Re: how to transform an Icon into a Bitmap?
Post by: jj2007 on February 15, 2022, 09:55:22 AM
Quote from: nidud on February 15, 2022, 09:04:51 AMadding the proto type as well would be simpler.

Nonsense, the PROTO is a simple :DWORD, :DWORD but that won't be helpful if you don't have the static lib.

ICONINFOEXA STRUCT
cbSize    DWORD ?
fIcon      BOOL ?
xHotspot  DWORD ?
yHotspot  DWORD ?
hbmMask    HBITMAP ?
hbmColor  HBITMAP ?
wResID    WORD ?
szModName CHAR MAX_PATH dup(?
szResName CHAR MAX_PATH dup(?
ICONINFOEXA ENDS
Title: Re: how to transform an Icon into a Bitmap?
Post by: xandaz on February 16, 2022, 04:16:12 AM
    I still don't know which linrary to Load. ty
Title: Re: how to transform an Icon into a Bitmap?
Post by: jj2007 on February 16, 2022, 04:30:02 AM
Quote from: xandaz on February 16, 2022, 04:16:12 AM
    I still don't know which linrary to Load. ty

Quote from: jj2007 on February 15, 2022, 08:34:52 AMNot included in user32.inc, so you need LoadLibrary plus GetProcAddress

Quoteif you don't have the static lib

No library. Read my posts: you need a DLL. Which one, I don't know. User32.dll on my Windows 7-64 does not have GetIconInfoEx
Title: Re: how to transform an Icon into a Bitmap?
Post by: Vortex on February 16, 2022, 04:47:26 AM
Hi xandaz,

The function GetIconInfoEx is exported by user32.dll :

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-geticoninfoexa

You need to update \masm32\include\user32.inc to add the prototype of the function GetIconInfoEx :

GetIconInfoExA PROTO STDCALL :DWORD,:DWORD

IFNDEF __UNICODE__
  GetIconInfoEx equ <GetIconInfoExA>
ENDIF

GetIconInfoExW PROTO STDCALL :DWORD,:DWORD

IFDEF __UNICODE__
  GetIconInfoEx equ <GetIconInfoExW>
ENDIF


You can install the latest version of PellesC to get the latest versions of the command-line tools. You can copy this import library from the PellesC installation :

copy D:\PellesC\lib\Win\user32.lib G:\masm32\lib

A quick verification :

D:\PellesC\Bin>podump /EXPORTS ..\lib\Win\user32.lib | findstr "GetIconInfoEx"
User32.dll: GetIconInfoExA (_GetIconInfoExA@8)
User32.dll: GetIconInfoExW (_GetIconInfoExW@8)


Don't forget to backup the original user32.def and user32.lib supplied with the Masm32 package.
Title: Re: how to transform an Icon into a Bitmap?
Post by: Vortex on February 16, 2022, 04:50:18 AM
Hi Jochen,

My Windows 7 64-bit installation is providing the function GetIconInfoEx :

D:\PellesC\Bin>podump /EXPORTS C:\Windows\System32\user32.dll | findstr "GetIconInfoEx"
            719   134  7DCACFF8  GetIconInfoExA
            71A   135  7DCACF3F  GetIconInfoExW
Title: Re: how to transform an Icon into a Bitmap?
Post by: nidud on February 16, 2022, 05:17:26 AM
deleted
Title: Re: how to transform an Icon into a Bitmap?
Post by: jj2007 on February 16, 2022, 06:39:52 AM
Quote from: Vortex on February 16, 2022, 04:50:18 AM
Hi Jochen,

My Windows 7 64-bit installation is providing the function GetIconInfoEx :

D:\PellesC\Bin>podump /EXPORTS C:\Windows\System32\user32.dll | findstr "GetIconInfoEx"
            719   134  7DCACFF8  GetIconInfoExA
            71A   135  7DCACF3F  GetIconInfoExW


It's also in my User32.dll; GetProcAddress returned error proc_not_found, but then I had the bright idea to ask for GetIconInfoExA :biggrin: