Jump to content

Menu Item(s) Picture(s)


 Share

Go to solution Solved by UEZ,

Recommended Posts

I am having a bit of trouble setting a bitmap to a menu item.

First: Could I use a link to a picture (.png, .jpeg, ect..)?

Second: I tried downloading the picture and saving as .bmp and used _GUICtrlMenu_SetItemBmp() but that didn't work either.

Third: Does it have to be a certain size or will it just auto re-size?

Fourth: If I had to have the file and couldn't use a link how could I put multiple pictures into a .dll file and call up several pictures from 1 file to use in menu items?

Fifth: My current code consists of GUICtrlCreateMenu() & GUICtrlCreateMenuItem() with that would I have to convert all of them to _GUICtrlMenu_CreateMenu() & _GUICtrlMenu_InsertMenuItem() for _GUICtrlMenu_SetItemBmp() to work or can it be used with them?

The only code I have been trying was the example "_GUICtrlMenu_SetItemBmp" and changing that single line to @ScriptDir&"test.bmp". Yes I did save the example to another file and edited the example and placed the test.bmp in the directory as well. Also I tried re-sized the image and still nothing shows up.

 

Any help would be appreciated

#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Global $iMemo

_Main()

Func _Main()
    Local $hGUI, $hFile, $hEdit, $hHelp, $hMain
    Local Enum $idNew = 1000, $idOpen, $idSave, $idExit, $idCut, $idCopy, $idPaste, $idAbout

    ; Create GUI
    $hGUI = GUICreate("Menu", 400, 300)

    ; Create File menu
    $hFile = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hFile, 0, "&New", $idNew)
    _GUICtrlMenu_InsertMenuItem($hFile, 1, "&Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem($hFile, 2, "&Save", $idSave)
    _GUICtrlMenu_InsertMenuItem($hFile, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hFile, 4, "E&xit", $idExit)

    ; Create Edit menu
    $hEdit = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hEdit, 0, "&Cut", $idCut)
    _GUICtrlMenu_InsertMenuItem($hEdit, 1, "C&opy", $idCopy)
    _GUICtrlMenu_InsertMenuItem($hEdit, 2, "&Paste", $idPaste)

    ; Create Help menu
    $hHelp = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hHelp, 0, "&About", $idAbout)

    ; Create Main menu
    $hMain = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hMain, 0, "&File", 0, $hFile)
    _GUICtrlMenu_InsertMenuItem($hMain, 1, "&Edit", 0, $hEdit)
    _GUICtrlMenu_InsertMenuItem($hMain, 2, "&Help", 0, $hHelp)

    ; Set window menu
    _GUICtrlMenu_SetMenu($hGUI, $hMain)

    ; Create memo control
    $iMemo = GUICtrlCreateEdit("", 2, 2, 396, 276, 0)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    GUISetState()

    ; Set New menu item to have a bitmap
    _GUICtrlMenu_SetItemBmp($hFile, 1, @ScriptDir&"\test.bmp")
;~  MemoWrite("Item bitmap handle: 0x" & Hex(_GUICtrlMenu_GetItemBmp($hFile, 0)))

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

; Write message to memo
Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite
Link to comment
Share on other sites

  • Solution

Since the AutoIt.de forum is down (download link) here the code to set an image to the menu:

_GUICtrlMenu_CreateBitmap.au3

;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include-once
#include <GDIPlus.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()
OnAutoItExitRegister("_Exit_GUICtrlMenu_CreateBitmap")

; #FUNCTION# ============================================================================================================
; Name...................:  _GUICtrlMenu_CreateBitmap
; Description .......:      Extracts the icon from $sFile and converts it to a HBitmap format
; Syntax................:   _GUICtrlMenu_CreateBitmap($sFile, $iIndex = 0, $iX = 18, $iY = 18)
; Parameters ......:        $sFile      - file name where the icon should be extracted from or an image should be loaded (*.dll, *.ico, *.exe, *.jpg, *.png, *.bmp, *.gif, *.tif)
;                                       $iIndex - index of the icon from $sFile
;                                       $iX         - set the width of the extracted icon
;                                       $iY         - set the height of the extracted icon
; Return values .:          Success - handle to a HBITMAP
;                           Failure     - Returns 0 and sets error to 1-7
; Author ..............:    UEZ
; Version .............:    v0.70 Build 2012-10-13 beta
; Remarks ...........:      Don't forget to use _WinAPI_DeleteObject($<handle to the HBITMAP>) when closing to to release the resources
; =======================================================================================================================
Func _GUICtrlMenu_CreateBitmap($sFile, $iIndex = 0, $iW = 18, $iH = 18)
    Local $hIcon, $Ret, $hBitmap, $hContext, $bBinary = False
    Local $aChk = _GDIPlus_ImageGetFlags($sFile)
    If Not @error Then $bBinary = True
    If FileExists($sFile)  Or $bBinary Then
        Local Const $iStride = 0, $iPixelFormat = $GDIP_PXF32ARGB, $pScan0 = 0
        Local $fext = StringMid($sFile, StringLen($sFile) - 3)
        If $bBinary Then $fext = "0815"
        Switch $fext
            Case ".dll", ".exe", ".ico"
                Return _WinAPI_GetFileIcon($sFile, $iIndex, $iW, $iH)
            Case ".jpg", ".png", ".bmp", ".gif", ".tif", "0815"
                Local $hImage
                If Not $bBinary Then
                    $hImage = _GDIPlus_ImageLoadFromFile($sFile)
                    If @error Then Return SetError(4, @extended, 0)
                Else
                    $hImage = $sFile
                EndIf
                $Ret = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
                If @error Then Return SetError(5, @extended, 0)
                $hBitmap = $Ret[6]
                $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
                DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "handle", $hContext, "int", 7)
                _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iW, $iH)
                $hIcon =  _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
                _GDIPlus_GraphicsDispose($hContext)
                _GDIPlus_BitmapDispose($hBitmap)
                _GDIPlus_BitmapDispose($hImage)
                If Not $hIcon Then Return SetError(6, 0, 0)
            Case Else
                Return SetError(7, 0, 0)
        EndSwitch
        Return $hIcon
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_GUICtrlMenu_CreateBitmap

Func _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
    Local $tBIHDR, $Ret, $tData, $pBits, $hResult = 0
    $Ret = DllCall($ghGDIPDll, 'uint', 'GdipGetImageDimension', 'ptr', $hBitmap, 'float*', 0, 'float*', 0)
    If (@error) Or ($Ret[0]) Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $Ret[2], $Ret[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, 'Scan0')
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate('dword;long;long;ushort;ushort;dword;dword;long;long;dword;dword')
    DllStructSetData($tBIHDR, 1, DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, 2, $Ret[2])
    DllStructSetData($tBIHDR, 3, $Ret[3])
    DllStructSetData($tBIHDR, 4, 1)
    DllStructSetData($tBIHDR, 5, 32)
    DllStructSetData($tBIHDR, 6, 0)
    $hResult = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', 'hwnd', 0, 'ptr', DllStructGetPtr($tBIHDR), 'uint', 0, 'ptr*', 0, 'ptr', 0, 'dword', 0)
    If (Not @error) And ($hResult[0]) Then
        DllCall('gdi32.dll', 'dword', 'SetBitmapBits', 'ptr', $hResult[0], 'dword', $Ret[2] * $Ret[3] * 4, 'ptr', DllStructGetData($tData, 'Scan0'))
        $hResult = $hResult[0]
    Else
        $hResult = 0
    EndIf
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    Return $hResult
EndFunc   ;==>_GDIPlus_BitmapCreateDIBFromBitmap

Func _WinAPI_GetFileIcon($sFile, $iIndex = 0, $iW = 18, $iH = 18, $iColor = -1)
    Local $aRet, $hIcon, $hHBitmap
    Local $hDC, $hBackDC, $hBackSv

    $aRet = DllCall('shell32.dll', 'int', 'SHExtractIconsW', 'wstr', $sFile, 'int', $iIndex, 'int', $iW, 'int', $iH, 'ptr*', 0, 'ptr*', 0, 'int', 1, 'int', 0)
    If @error Then Return SetError(6, @extended, 0)
    $hIcon = $aRet[5]

    $hDC = _WinAPI_GetDC(0)
    $hBackDC = _WinAPI_CreateCompatibleDC($hDC)
    If $iColor = -1 Then $iColor = _WinAPI_GetSysColor($COLOR_MENU)
    $hHBitmap = _WinAPI_CreateSolidBitmap(0, $iColor, $iW, $iH)
    $hBackSv = _WinAPI_SelectObject($hBackDC, $hHBitmap)
    _WinAPI_DrawIconEx($hBackDC, 0, 0, $hIcon, $iW, $iH)
    _WinAPI_DestroyIcon($hIcon)

    _WinAPI_SelectObject($hBackDC, $hBackSv)
    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteDC($hBackDC)
    Return $hHBitmap
EndFunc   ;==>_GUICtrlMenu_CreateBitmap

Func _Exit_GUICtrlMenu_CreateBitmap()
    _GDIPlus_Shutdown()
EndFunc

An example:

#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include "_GUICtrlMenu_CreateBitmap.au3"

Opt('MustDeclareVars', 1)
Global $iMemo

_Main()

Func _Main()
    Local $hGUI, $hFile, $hEdit, $hHelp, $hMain
    Local Enum $idNew = 1000, $idOpen, $idSave, $idExit, $idCut, $idCopy, $idPaste, $idAbout
    #region
    ; Create GUI
    $hGUI = GUICreate("Menu", 400, 300)

    ; Create File menu
    $hFile = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hFile, 0, "&New", $idNew)
    _GUICtrlMenu_InsertMenuItem($hFile, 1, "&Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem($hFile, 2, "&Save", $idSave)
    _GUICtrlMenu_InsertMenuItem($hFile, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hFile, 4, "E&xit", $idExit)

    ; Create Edit menu
    $hEdit = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hEdit, 0, "&Cut", $idCut)
    _GUICtrlMenu_InsertMenuItem($hEdit, 1, "C&opy", $idCopy)
    _GUICtrlMenu_InsertMenuItem($hEdit, 2, "&Paste", $idPaste)

    ; Create Help menu
    $hHelp = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hHelp, 0, "&About", $idAbout)

    ; Create Main menu
    $hMain = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hMain, 0, "&File", 0, $hFile)
    _GUICtrlMenu_InsertMenuItem($hMain, 1, "&Edit", 0, $hEdit)
    _GUICtrlMenu_InsertMenuItem($hMain, 2, "&Help", 0, $hHelp)

    ; Set window menu
    _GUICtrlMenu_SetMenu($hGUI, $hMain)

    ; Create memo control
    $iMemo = GUICtrlCreateEdit("", 2, 2, 396, 276, 0)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    GUISetState()
    #endregion
    Local $hBMP_File_New = _GUICtrlMenu_CreateBitmap(StringReplace(@AutoItExe, "autoit3.exe", "Examples\GUI\Torus.png"), 0, 32, 32)
    ; Set New menu item to have a bitmap
    _GUICtrlMenu_SetItemBmp($hFile, 0, $hBMP_File_New)
    MemoWrite("Item bitmap handle: 0x" & Hex(_GUICtrlMenu_GetItemBmp($hFile, 0)))

    Local $hBMP_File_Open = _GUICtrlMenu_CreateBitmap(@SystemDir  & "\Shell32.dll", 4, 32, 32)
    ; Set New menu item to have a bitmap
    _GUICtrlMenu_SetItemBmp($hFile, 1, $hBMP_File_Open)
    MemoWrite("Item bitmap handle: 0x" & Hex(_GUICtrlMenu_GetItemBmp($hFile, 1)))

    Local $hBMP_File_Save = _GUICtrlMenu_CreateBitmap(@SystemDir  & "\Shell32.dll", 6, 32, 32)
    ; Set New menu item to have a bitmap
    _GUICtrlMenu_SetItemBmp($hFile, 2, $hBMP_File_Save)
    MemoWrite("Item bitmap handle: 0x" & Hex(_GUICtrlMenu_GetItemBmp($hFile, 2)))

    Local $hBMP_File_Quit = _GUICtrlMenu_CreateBitmap(@SystemDir  & "\Shell32.dll", 27, 32, 32)
    ; Set New menu item to have a bitmap
    _GUICtrlMenu_SetItemBmp($hFile, 4, $hBMP_File_Quit)
    MemoWrite("Item bitmap handle: 0x" & Hex(_GUICtrlMenu_GetItemBmp($hFile, 4)))

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    _WinAPI_DeleteObject($hBMP_File_New)
    _WinAPI_DeleteObject($hBMP_File_Open)
    _WinAPI_DeleteObject($hBMP_File_Save)
    _WinAPI_DeleteObject($hBMP_File_Quit)
EndFunc   ;==>_Main

; Write message to memo
Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Now just creating a resource .dll. I have searched and searched and just found it easier to edit an existing one.  With your code above it did finally get them in there for me. :thumbsup:   Sad thing was I had to re-structure a lot of my existing code to get it to work properly. (All for icons...  :mad2: ) .

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...