Jump to content

How to create icons on context menus?


Recommended Posts

I was using this:

#include <GDIPlus.au3>
#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>

_Main()

Exit

Func _GUICtrlMenu_CreateBitmap($file, $iIndex = 0, $iX = 18, $iY = 18)
    If FileExists($file) Then
        Local $iStride = 0, $iPixelFormat = $GDIP_PXF32ARGB, $pScan0 = 0
        Local $GDIp = 1, $aRet, $hIcon, $hImage, $hBitmap, $hContext, $hIcon
        $aRet = DllCall("shell32", "long", "ExtractAssociatedIcon", "int", 0, "str", $file, "int*", $iIndex)
        If @error Then Return SetError(@error, @extended, 0)
        $hIcon = $aRet[0]
        If Not $ghGDIPDll Then
            $GDIp = 0
            _GDIPlus_Startup()
        EndIf
        $aRet = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHICON", "ptr", $hIcon, "int*", 0)
        If @error Then Return SetError(@error, @extended, 0)
        $hImage = $aRet[2]
        _WinAPI_DestroyIcon($hIcon)
        $aRet = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iX, "int", $iY, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
        If @error Then Return SetError(@error, @extended, 0)
        $hBitmap = $aRet[6]
        $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
        _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iX, $iY)
        $hIcon = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap, 0xFF000000)
        _GDIPlus_GraphicsDispose($hContext)
        _GDIPlus_BitmapDispose($hBitmap)
        _GDIPlus_BitmapDispose($hImage)
        If Not $GDIp Then _GDIPlus_Shutdown()
        Return SetError(0, 0, $hIcon)
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_GUICtrlMenu_CreateBitmap


Func _Main()
    Local $hGUI, $hFile, $hEdit, $hHelp, $hMain
    Local Enum $idNew = 1000, $idOpen, $idSave, $idExit, $idCut, $idCopy, $idPaste, $idAbout
    $hGUI = GUICreate("Menu", 400, 300)
    $hFile = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hFile, 0, "&New", $idNew)
    $hMain = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hMain, 0, "&File", 0, $hFile)
    _GUICtrlMenu_SetMenu($hGUI, $hMain)
    GUISetState()
    Local $hBMP_File_New = _GUICtrlMenu_CreateBitmap(@systemdir & "\shell32.dll", 10)
    _GUICtrlMenu_SetItemBmp($hFile, 0, $hBMP_File_New)
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    Exit
EndFunc   ;==>_Main

but it's really not as good as i want.. because the icon quality is sooooooo bad...! is there any other way...?

Link to comment
Share on other sites

Link to comment
Share on other sites

I usually use Holgers owner-drawn menus to accomplish this easily.

I would look for the link but for some reason I seem to recall modifying his ModernMenuRaw.au3 file and probably so it would work with current versions of AutoIt.

If you are still interested in this you can PM me and I'll send you the file with an example of how to use it with a context menu. Actually I can send you a bit of the code from one of my current projects and that will demonstrate it as well.

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

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