benners

Icon transparency on menu controls

3 posts in this topic

#1 ·  Posted (edited)

Hi,

I am trying to add icons to menu items and thought I had sorted it. I was just testing using shell32.dll and randomly selected icon # 239. For some reason out of the 50 i tested this so far has been the only one with an issue of black showing where the alpha? mask should be transparent. I have also saved the icon to a file and the issues persists.

Searching through the forum I found this thread and tried the code UEZ supplied in post #4 with the saved ico file but the issue was still there so I went back to my orignal code.

The icon seems to have transparency like the others but it is not displayed properly. I won't be using this icon when the program is finished but hope to stop this happening with any other icons, does anyone have any ideas on how to correct this?.

 

Thanks

My code

#include <GUIConstants.au3>
#include <GUIMenu.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

_GDIPlus_Startup()
Global $g_h_MainGUI = GUICreate("My Prog", 180, 117, -1, -1, $WS_SYSMENU)
Global $g_h_File_mnu = GUICtrlCreateMenu("File")
Global $g_id_FileOpen_mnu = GUICtrlCreateMenuItem("Open", $g_h_File_mnu)
Global $h_MenuItemBmp = MenuItem_SetIcon('Shell32.dll', 239, $g_h_File_mnu, $g_id_FileOpen_mnu)
Global $g_id_FileExit_mnu = GUICtrlCreateMenuItem("Exit", $g_h_File_mnu)
$h_MenuItemBmp = MenuItem_SetIcon('Shell32.dll', 28, $g_h_File_mnu, $g_id_FileExit_mnu)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $g_id_FileExit_mnu
            Program_Exit()
    EndSwitch
WEnd

Func MenuItem_SetIcon($s_FilePath, $i_IconID, $h_Menu, $i_MenuID, $iW = 16, $iH = 16)
    Local $h_Module = _WinAPI_GetModuleHandle($s_FilePath)
    Local $h_Icon = _WinAPI_LoadImage($h_Module, $i_IconID, $IMAGE_ICON, $iW, $iH, 0)
    Local $h_Bitmap = _GDIPlus_BitmapCreateFromHICON($h_Icon)
    Local $h_GDIBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($h_Bitmap)

    _GUICtrlMenu_SetItemBmp(GUICtrlGetHandle($h_Menu), $i_MenuID, $h_GDIBITMAP, False)
    _WinAPI_DestroyIcon($h_Icon)
    _GDIPlus_BitmapDispose($h_Bitmap)
EndFunc   ;==>MenuItem_SetIcon

Func Program_Exit()
    _WinAPI_DeleteObject($h_MenuItemBmp)
    _GDIPlus_Shutdown()
    Exit
EndFunc

 

Edited by benners
Spelling

Share this post


Link to post
Share on other sites



Hi UEZ, sorry for the delay  have been trying to fix problems with other dlls. Yes the addition of two extra digits on the function name :evil: fixed the issue so thanks for that. It's a simple fix but I would never have caught it.

After your help I started to try other dlls and exes. My script creates a dynamic menu from an ini file and adds items or sub items based on a users settings. The idea is to the check the registry for the file types default icon using code by Yashied. I will then use the info returned to specify an icon to convert to bitmap and add to the menu.

For some reason with other dlls and exes the _WinAPI_LoadImage function returns an error "The specified resource type cannot be found in the image file." but using Nirsofer's IconExt or the enumicons.au3 the icon resources are there. Yashieds and the the enumicons code both use GUICtrlCreateIcon but I am sure once I get the file name and the icon number it should be as easy as with the shell32.dll to convert.

I looked into the _WinAPI_LoadImage function but dll calls are above my head.

I'll have to search further when I have time unless you can again identify the problem easily.

I have modded the code to add simple error msgs to find the issues

#include <GUIConstants.au3>
#include <GUIMenu.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>


_GDIPlus_Startup()
Global $g_h_MainGUI = GUICreate("My Prog", 180, 117, -1, -1, $WS_SYSMENU)
Global $g_h_File_mnu = GUICtrlCreateMenu("File")
Global $g_id_FileOpen_mnu = GUICtrlCreateMenuItem("Open", $g_h_File_mnu)
Global $h_MenuItemBmp = MenuItem_SetIcon('imageres.dll', 2, $g_h_File_mnu, $g_id_FileOpen_mnu)
Global $g_id_FileExit_mnu = GUICtrlCreateMenuItem("Exit", $g_h_File_mnu)
$h_MenuItemBmp = MenuItem_SetIcon('Shell32.dll', 28, $g_h_File_mnu, $g_id_FileExit_mnu)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $g_id_FileExit_mnu
            Program_Exit()
    EndSwitch
WEnd

Func MenuItem_SetIcon($s_FilePath, $i_IconID, $h_Menu, $i_MenuID, $i_Load = 0, $iW = 16, $iH = 16)
    Local $h_Module = 0

    ; check if ico file was passed
    if StringRight($s_FilePath, 4) <> '.exe' And StringRight($s_FilePath, 4) <> '.dll' then
        $i_IconID = $s_FilePath
        $i_Load = $LR_LOADFROMFILE
    Else ; exe or dll
        $h_Module = _WinAPI_GetModuleHandle($s_FilePath)
        if @error then MsgBox(0,'', '_WinAPI_GetModuleHandle')
    EndIf

    Local $h_Icon = _WinAPI_LoadImage($h_Module, $i_IconID, $IMAGE_ICON, $iW, $iH, $i_Load)

    if not $h_Icon then
        MsgBox(0,'_WinAPI_GetModuleHandle', _WinAPI_GetLastErrorMessage())
        Return
    EndIf

    Local $h_Bitmap = _GDIPlus_BitmapCreateFromHICON32($h_Icon)
    if @error then MsgBox(0,@extended, '_GDIPlus_BitmapCreateFromHICON32')
    Local $h_GDIBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($h_Bitmap)
    if @error then MsgBox(0,'', '_GDIPlus_BitmapCreateHBITMAPFromBitmap')

    _GUICtrlMenu_SetItemBmp(GUICtrlGetHandle($h_Menu), $i_MenuID, $h_GDIBITMAP, False)
    if @error then MsgBox(0,'', '_GUICtrlMenu_SetItemBmp')
    _WinAPI_DestroyIcon($h_Icon)
    _GDIPlus_ImageDispose($h_Bitmap)
EndFunc   ;==>MenuItem_SetIcon

Func Program_Exit()
    _WinAPI_DeleteObject($h_MenuItemBmp)
    _GDIPlus_Shutdown()
    Exit
EndFunc

Thanks again

Share this post


Link to post
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