Jump to content

Icon to a MenuItem


Recommended Posts

Hi,

my English isn't perfect so I'm not sure to speak correctly. Forgive me if I'm wrong ^^

So, my current problem is to assign an icon to a MenuItem, icon that is exported correctly from a dll through a function (which I created, tested and classified as working xD)

As long as I draw an icon and I apply for a button or pull it out through the function GUICtrlCreateIcon ok, the problem comes when I assign an icon to a MenuItem through the function _GUICtrlMenu_SetItemBmp

In fact, the icon in question does not appear in the MenuItem.

The help of AutoIt gives the following example:

#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.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

   ; 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, 0, _WinAPI_CreateSolidBitmap ($hGUI, 0xFF0000, 11, 11))
   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

Here appears a solid next to the text of the MenuItem, solid created by the function _WinAPI_CreateSolidBitmap

This is the function that I created for extract the icon to read from a dll (it is the classic function _WinAPI_LoadShell32Icon slightly modified), but, as I wrote, the icon doesn't appear.

Func Leggi_Dll ($Dll, $iIconID)
       Local $tIcons = DllStructCreate("ptr Data")
       Local $pIcons = DllStructGetPtr($tIcons)
       Local $iIcons = _WinAPI_ExtractIconEx($Dll, Number ($iIconID) - 1, 0, $pIcons, 1)
       Return DllStructGetData($tIcons, "Data")
EndFunc

Can you help me?

Link to comment
Share on other sites

I could be wrong, but I'm sure I remember reading you can't set an icon (.ico) as a bitmap (.bmp) you must use correct functions with correct file types.

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

You may also want to take a look at the Menu Icon UDF by Holger.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I could be wrong, but I'm sure I remember reading you can't set an icon (.ico) as a bitmap (.bmp) you must use correct functions with correct file types.

There is no way to use files. ico as an icon for the MenuItem?

You may also want to take a look at the Menu Icon UDF by Holger.

Thanks but I've already seen that UDF

I'd like to add only the icon, and not the colors and styles other than the default

Edited by Sbarabau
Link to comment
Share on other sites

Have a look here: http://www.autoitscript.com/forum/index.php?showtopic=115491&view=findpost&p=806834

It might help you!

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

I have a small problem: I load the icon from a dll file using the function that I wrote before.

The icon isn't displayed properly, and instead appears the icon of an unknown file.

How can I resolve this problem?

Link to comment
Share on other sites

Maybe the icon id is wrong!

I modified my code a little bit:

#include <GDIPlus.au3>
#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WinAPIEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
Global $iMemo


$file = FileOpenDialog("Select file", @ScriptDir, ("Icons from (*.ico; *.dll; *.exe)"))

If @error Then Exit
$iIndex = 0
$Ret = DllCall("shell32", "long", "ExtractAssociatedIcon", "int", 0, "str", $file, "int*", $iIndex)
$hIcon = $Ret[0]

_GDIPlus_Startup()
$hImage = DllCall($ghGDIPDll,"int", "GdipCreateBitmapFromHICON", "ptr", $hIcon, "int*", 0)
$hImage = $hImage[2]
_WinAPI_DestroyIcon($hIcon)

$iWidth = 16
$iHeight = 16
$hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iWidth, $iHeight)
$hIcon = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)

_Main()

_GDIPlus_GraphicsDispose($hContext)
_WinAPI_DeleteObject($hIcon)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

Exit


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, 0, $hIcon)
    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

Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[6]
EndFunc ;==>_GDIPlus_BitmapCreateFromScan0

Change $iIndex to get the desired icon displayed!

BR,

UEZ

Edited by 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

Maybe the icon id is wrong!

I modified my code a little bit:

#include <GDIPlus.au3>
#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WinAPIEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
Global $iMemo


$file = FileOpenDialog("Select file", @ScriptDir, ("Icons from (*.ico; *.dll; *.exe)"))

If @error Then Exit
$iIndex = 0
$Ret = DllCall("shell32", "long", "ExtractAssociatedIcon", "int", 0, "str", $file, "int*", $iIndex)
$hIcon = $Ret[0]

_GDIPlus_Startup()
$hImage = DllCall($ghGDIPDll,"int", "GdipCreateBitmapFromHICON", "ptr", $hIcon, "int*", 0)
$hImage = $hImage[2]
_WinAPI_DestroyIcon($hIcon)

$iWidth = 16
$iHeight = 16
$hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iWidth, $iHeight)
$hIcon = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)

_Main()

_GDIPlus_GraphicsDispose($hContext)
_WinAPI_DeleteObject($hIcon)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

Exit


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, 0, $hIcon)
    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

Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[6]
EndFunc ;==>_GDIPlus_BitmapCreateFromScan0

Change $iIndex to get the desired icon displayed!

BR,

UEZ

This code works perfectly and i haven't any kind of problem.

Thank you! ^^

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