Jump to content

_GUICtrlMenu_SetItemImage() - Create an Icon on Tray Menu/GUI Menu


 Share

Recommended Posts

I posted this Example in the (Examples) Forum Post created by Yashied >> And not being an advocate of double posting I hope this isn't construed as a double post.

But I am not an advanced Expert with GDIPlus (but still good) so I want to know if my Example of creating Icons on a Tray Menu or GUI Menu could be improved with regards to clearing of Resources? Or is there a different approach of doing it?

The Function _GUICtrlMenu_SetItemImage() works, but I am not happy with _WinAPI_DeleteObject($gIcon) not being enabled for example.

Note: I used this post >> from UEZ as a basis for creating the Function!

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_UseX64=N

#include <GDIPlus.au3>
#include <GUIMenu.au3>
Opt("TrayMenuMode", 3)

_GDIPlus_Startup()
_Example()

Func _Example()
    Local $GUI = GUICreate("")
    Local $SettingsItem = TrayCreateMenu("Settings")
    _GUICtrlMenu_SetItemBmp(TrayItemGetHandle(0), 0, _WinAPI_CreateSolidBitmap($GUI, 0xFF0000, 15, 15)) ; Set Image to Settings.

    Local $DisplayItem = TrayCreateItem("Display", $SettingsItem)
    Local $Return = _GUICtrlMenu_SetItemImage(@ScriptDir & "\Example.png", 0, $SettingsItem) ; Set Image to Sub-Menu in Settings under Display.
    TrayCreateItem("Printer", $SettingsItem)
    $Return = _GUICtrlMenu_SetItemImage(@ScriptDir & "\Example.png", 1, $SettingsItem) ; Set Image to Sub-Menu in Settings under Printer.

    TrayCreateItem("Delete")
    $Return = _GUICtrlMenu_SetItemImage(@ScriptDir & "\Example.png", 1) ; Set Image to Delete.

    TrayCreateItem("")
    TrayCreateItem("About")
    TrayCreateItem("")
    Local $ExitItem = TrayCreateItem("Exit")

    TraySetState()

    While 1
        Switch TrayGetMsg()
            Case $ExitItem
                _WinAPI_DeleteObject($Return) ; <<<< Delete Icon Image.
                _GDIPlus_Shutdown()
                ExitLoop

            Case $DisplayItem
                MsgBox(0, "Display/About", "Display Or About Was Clicked!")

        EndSwitch
        Sleep(10)
    WEnd
EndFunc   ;==>_Example

Func _GUICtrlMenu_SetItemImage($gImageFile, $gIndex, $gHandle = 0, $gWidth = 35, $gHeight = 35) ; Set Image to Tray/GUI Menu.
    $gHandle = TrayItemGetHandle($gHandle)
    Local $gImage = _GDIPlus_BitmapCreateFromFile($gImageFile)
    Local $gImageWidth = _GDIPlus_ImageGetWidth($gImage)
    Local $gImageHeight = _GDIPlus_ImageGetHeight($gImage)
    If ($gImageWidth < 0) Or ($gImageHeight < 0) Then Return SetError(1, 1, 0)
    If $gImageWidth < $gImageHeight Then
        $gImageWidth = $gWidth * $gImageWidth / $gImageHeight
        $gImageHeight = $gHeight
    Else
        $gImageHeight = $gHeight * $gImageHeight / $gImageWidth
        $gImageWidth = $gWidth
    EndIf
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $gWidth, "int", $gHeight, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    Local $gBitmap = $aResult[6]
    Local $gContext = _GDIPlus_ImageGetGraphicsContext($gBitmap)
    _GDIPlus_GraphicsDrawImageRect($gContext, $gImage, 0, 0, $gWidth, $gHeight)
    Local $gIcon = _GDIPlus_BitmapCreateHBITMAPFromBitmap($gBitmap)
    _GUICtrlMenu_SetItemBmp($gHandle, $gIndex, $gIcon)
    _GDIPlus_GraphicsDispose($gContext)
    _GDIPlus_BitmapDispose($gBitmap)
    Return SetError(0, 0, $gIcon)
EndFunc   ;==>_GUICtrlMenu_SetItemImage
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

You can return the handle of $gIcon and use _WinAPI_DeleteObject() to delete it afterwards.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_UseX64=N

#include <GDIPlus.au3>
#include <GUIMenu.au3>
Opt("TrayMenuMode", 3)

_GDIPlus_Startup()
_Example()

Func _Example()
    Local $GUI = GUICreate("")
    Local $SettingsItem = TrayCreateMenu("Settings")
    _GUICtrlMenu_SetItemBmp(TrayItemGetHandle(0), 0, _WinAPI_CreateSolidBitmap($GUI, 0xFF0000, 20, 20)) ; Set Image to Settings.

    Local $DisplayItem = TrayCreateItem("Display", $SettingsItem)
    Local $hBmp  = _GUICtrlMenu_SetItemImage($SettingsItem, 0, "IMAGE_FILE") ; Set Image to Sub-Menu in Settings under Display.
    _WinAPI_DeleteObject($hBmp)
    
    TrayCreateItem("Printer", $SettingsItem)
    TrayCreateItem("")
    TrayCreateItem("About")
    TrayCreateItem("")
    Local $ExitItem = TrayCreateItem("Exit")

    TraySetState()

    While 1
        Switch TrayGetMsg()
            Case $ExitItem
                _GDIPlus_Shutdown()
                ExitLoop

            Case $DisplayItem
                MsgBox(0, "Display", "Display was Clicked!")

        EndSwitch
        Sleep(10)
    WEnd
EndFunc   ;==>_Example

Func _GUICtrlMenu_SetItemImage($gHandle, $gIndex, $gImageFile, $gWidth = 20, $gHeight = 20) ; Set Image to Tray/GUI Menu.
    $gHandle = TrayItemGetHandle($gHandle)
    Local $gImage = _GDIPlus_BitmapCreateFromFile($gImageFile)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $gWidth, "int", $gHeight, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    Local $gBitmap = $aResult[6]
    Local $gContext = _GDIPlus_ImageGetGraphicsContext($gBitmap)
    _GDIPlus_GraphicsDrawImageRect($gContext, $gImage, 0, 0, $gWidth, $gHeight)
    Local $gIcon = _GDIPlus_BitmapCreateHBITMAPFromBitmap($gBitmap)
    _GUICtrlMenu_SetItemBmp($gHandle, $gIndex, $gIcon)
    _GDIPlus_GraphicsDispose($gContext)
    ; _WinAPI_DeleteObject($gIcon) ; <<<<< Would love to enable this!
    _GDIPlus_BitmapDispose($gBitmap)
    Return SetError(0, 0, $gIcon)
EndFunc   ;==>_GUICtrlMenu_SetItemImage

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

Cheers UEZ! Your example of using _WinAPI_DeleteObject($hBmp) after the Image is Set is the same problem I had in the Function, but I suppose I could add this when the Program Exits. I take it my use of Functions in _GUICtrlMenu_SetItemImage() were correct then :)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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