Jump to content

How to set icon for a sysmenu entry?


Recommended Posts

I'm searching for a way to set an icon for a system menu entry in the console window. I can add / remove entries but I didn't find a way to set an icon for the entry I made.

Example:

#AutoIt3Wrapper_Change2CUI=y
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WindowsConstants.au3>


Global $id_Test = 5000
Global $hConsole = HWnd(DllCall("kernel32.dll", "hwnd", "GetConsoleWindow")[0])
If Not $hConsole Then Exit

HotKeySet("{ESC}", "_Exit")

Global $hSysmenu = _GUICtrlMenu_GetSystemMenu($hConsole)
Global $iCount = _GUICtrlMenu_GetItemCount ($hSysmenu)
_GUICtrlMenu_InsertMenuItem($hSysmenu, $iCount, "Test", $id_Test)
_GDIPlus_Startup()
$hBitmap_GDI = _GDIPlus_BitmapCreateFromMemory(_Test(), True)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hBitmap_GDI = ' & $hBitmap_GDI & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$hBmp1 = _WinAPI_CreateSolidBitmap($hConsole, 0xFF0000, 16, 16)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hBmp1 = ' & $hBmp1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$hBmp2 = _WinAPI_CreateSolidBitmap($hConsole, 0x00FF00, 16, 16)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hBmp2 = ' & $hBmp2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

;ConsoleWrite(_GUICtrlMenu_SetItemBmp($hSysmenu, $id_Test, $hBitmap_GDI, False) & @CRLF)
ConsoleWrite(_GUICtrlMenu_SetItemBitmaps($hSysmenu, $id_Test, $hBmp1, $hBmp2, False) & ", " & @error & @CRLF)
;_GUICtrlMenu_SetItemBmp($hSysmenu, $id_Test, 8, False) ;set the default close icon
_GDIPlus_Shutdown()

Do
    Sleep(1000)
Until False

Func _Exit()
    ConsoleWrite("Bye..." & @CRLF)
    _GUICtrlMenu_DeleteMenu ($hSysmenu, $id_Test, False)
    _WinAPI_DeleteObject($hBitmap_GDI)
    _WinAPI_DeleteObject($hBmp1)
    _WinAPI_DeleteObject($hBmp2)
    Exit
EndFunc

;Code below was generated by: 'File to Base64 String' Code Generator v1.20 Build 2020-06-05

Func _Test($bSaveBinary = False, $sSavePath = @ScriptDir)
    Local $Test
    $Test &= 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA3XAAAN1wFCKJt4AAADKElEQVQ4yz2RT2gcdRTHP7+Z36y7yzZZdvJv22i0rBpiAwnWCMZrUCGIBxEvpQSEHAQhePLkyUKg6Em9iZCcahH0pFYxkIqiIdHKYmPUUHcDmU2TuJnZzc7s/H7PQ5M8ePAu7/M+X56y1hKGYY+IOEDqOI6TzWY1gFIKEcF1XRzHUWmaJnF8bHBy5pfbnz9fb0hBh2F4bm5ubjWKogudTqdVqVT0/Py853kehUKBQqHA2tqaVKtVfenS2P6HH396PDri9D9+cdC/8dkXNW2t9fb29spRFPmtVqtULBaVMYZ8Pk82m8X3farVKjdv3qCn+FbpnFuXfNpQkkwlX33z3RtaKYXW2mqtcV1XtNZks9kzQCaTIeNp4sSh8rAnzSe0HO5bt1arHR51y79pEcEYo05Aylp7OqOUIk0TRp+aYO5Ki3B3VR3s70pictzZGlC5jHW0UgprLVEU4Xke29vbLCwsoLXGcRRxYjk/2MtLz2X4/tvbaO3y9XoPlUe7CI7VAMYYPM+jXC5jrcUYAwipUeSzHhV/k59+bFK5OMytOxco9ezQTQ2A4ujoqDQ+Ph4sLS2JiNiDgwM5PDyQ3SCQ/5ptubv6rsy9XJIfvnxbNv/8W+7d+9deu/aeTE9PB2EYljSAiKje3l7SNCUIAkDQXp77f3zE4uJ1Xnv9KoWRK+zs1DlfHiCXy2OtBcA5AdDpdHBdl4F+n77BEdz2z3zw/iKvvHqVZ194h0wmz0B/Ca094jjmtE4NEBFarRa7jfvkHgqp/rrKxOQzjF6+wtY/u4g9Jo5TfL+EiGCtxVqLFhGUUhhjsNZgrcvqrU948rFhKlNvEkUdtNvBoFEqPVtUSqGUQiulaDabtNttPC/D5t3fqTU0Y5dfpNFogFhEOLsaxzHtdpsgCNT6+rrSSZIwOztrhoeH'
    $Test &= 'qdfrRmutJp6e4q+tKsbYs3in7+7r65OhoSE9MzNjkiRBt9ttkiRxjTHkcjl3bGxMPYhjSdP0rLvdLt1uF9d1BcDzPHdyclL0yspKuLGxcT0IgkeKxaINgsADXBFxAefEQB4ImNT3/W4Yhk6z2awtLy9H/wPnrsNEnFPl4QAAAABJRU5ErkJggg=='
    Local $bString = _WinAPI_Base64Decode($Test)
    If @error Then Return SetError(1, 0, 0)
    $bString = Binary($bString)
    If $bSaveBinary Then
        Local Const $hFile = FileOpen($sSavePath & "\AutoSave_16x16_04.png", 18)
        If @error Then Return SetError(2, 0, $bString)
        FileWrite($hFile, $bString)
        FileClose($hFile)
    EndIf
    Return $bString
EndFunc   ;==>_Test

Func _WinAPI_Base64Decode($sB64String)
    Local $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "ptr", 0, "dword*", 0, "ptr", 0, "ptr", 0)
    If @error Or Not $aCrypt[0] Then Return SetError(1, 0, "")
    Local $bBuffer = DllStructCreate("byte[" & $aCrypt[5] & "]")
    $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "struct*", $bBuffer, "dword*", $aCrypt[5], "ptr", 0, "ptr", 0)
    If @error Or Not $aCrypt[0] Then Return SetError(2, 0, "")
    Return DllStructGetData($bBuffer, 1)
EndFunc   ;==>_WinAPI_Base64Decode

 

You must compile and run it to see the menu entry in the console window.

Console-Icon.jpg

 

Any idea?

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

6 hours ago, UEZ said:

Any idea?

Hi @UEZ,

maybe setting the correct parameters in the $ tagMENUITEMINFO structure (as shown here: https://docs.microsoft.com/en-us/windows/win32/menurc/using-menus#using-menu-item-bitmaps ) and then using _GUICtrlMenu_InsertMenuItemEx ()?

Trying to wildly guess I messed up some parameters ... but to no avail

; just wild guessing..... without success ....
Global $hxx = $hBmp1 ; trying with the  _WinAPI_CreateSolidBitmap
Global $tInfo = _GUICtrlMenu_GetItemInfo($hSysmenu, $iCount)
DllStructSetData($tInfo, "Type", $MFT_BITMAP)
DllStructSetData($tInfo, "Size", DllStructGetSize($tInfo))
DllStructSetData($tInfo, "ID", 0)
DllStructSetData($tInfo, "SubMenu", 0)
DllStructSetData($tInfo, "Mask", BitOR($MIIM_ID, $MFT_BITMAP, $MIIM_DATA))
DllStructSetData($tInfo, "BmpChecked", $hxx)
DllStructSetData($tInfo, "BmpChecked", $hxx)
DllStructSetData($tInfo, "TypeData", $hxx)
DllStructSetData($tInfo, "State",BitOR($MFS_ENABLED,  $MFS_HILITE))
DllStructSetData($tInfo, "BmpItem", $hxx)
 _GUICtrlMenu_InsertMenuItemEx($hSysmenu, $iCount, $tInfo, True)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Thanks @Chimp but I tried also this version without success.

 

Btw, this is the proper way to get the entry but without an icon:

Global $tInfo = DllStructCreate($tagMENUITEMINFO)
Global $tBuffer = DllStructCreate("wchar Text[" & StringLen("Test") + 1 & "]")
DllStructSetData($tBuffer, "Text", "Test")

DllStructSetData($tInfo, "Size", DllStructGetSize($tInfo))
DllStructSetData($tInfo, "Type", $MFT_STRING)
DllStructSetData($tInfo, "ID", $id_Test)
DllStructSetData($tInfo, "SubMenu", 0)
DllStructSetData($tInfo, "Mask", BitOR($MIIM_ID, $MFT_BITMAP, $MIIM_DATA, $MIIM_STRING))
DllStructSetData($tInfo, "BmpItem", $hBmp1)
DllStructSetData($tInfo, "BmpChecked", $hBmp1)
DllStructSetData($tInfo, "BmpUnChecked", $hBmp2)
DllStructSetData($tInfo, "TypeData", DllStructGetPtr($tBuffer))
DllStructSetData($tInfo, "State", $MFS_ENABLED)
_GUICtrlMenu_InsertMenuItemEx($hSysmenu, $iCount, $tInfo, True)

 

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

... peeking  the icon from the first item and setting it directly to the "Test" menu item ,  then that icon appears.... :huh2: ??

_GUICtrlMenu_SetItemBmp($hSysmenu, $iCount, _GUICtrlMenu_GetItemBmp($hSysmenu, 0))

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

44 minutes ago, Chimp said:

... peeking  the icon from the first item and setting it directly to the "Test" menu item ,  then that icon appears.... :huh2: ??

_GUICtrlMenu_SetItemBmp($hSysmenu, $iCount, _GUICtrlMenu_GetItemBmp($hSysmenu, 0))

 

Yes, check out line 28

_GUICtrlMenu_SetItemBmp($hSysmenu, $id_Test, 8, False) ;set the default close icon

_GUICtrlMenu_SetItemBmp($hSysmenu, $id_Test, 8, False) ;set the default close icon

😉

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

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

×
×
  • Create New...