Jump to content

Create Icon-File from Hex-Color - (Moved)


Go to solution Solved by Macrostop,

Recommended Posts

Dear Ladies and Sirs

I want to apply a colored icon to my Tab-Control.

This works well if I have the file present as ".ico" file.

Now I want to apply an Icon which is defined by HEX-color: e.g.  "0xC0C0C0", but can not get it to work.

I have a similiar code to create Bitmap-files (temporary):

;~ ;-------------------------------------------------------------------------------
#include-once
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiMenu.au3>
#include <File.au3>
;~ ;-------------------------------------------------------------------------------
_GDIPlus_Startup() ; Color Icons + Menu Icons ; _GDIPlus_Shutdown()
;~; -------------------------------------------------------------------------------

Local $hWnd = GUICreate("Icon Test", 200, 200)
GUISetState()

Local $idContextmenu = GUICtrlCreateContextMenu()

GUICtrlCreateMenuItem("Item 1", $idContextmenu)
Local $Item2 = GUICtrlCreateMenuItem("Item 2", $idContextmenu)
Local $Menu = GUICtrlCreateMenu("Menu", $idContextmenu)
Local $Item3 = GUICtrlCreateMenuItem("Item 3", $Menu)

Local $hTMP = _Return_colored_BitMapFile("0xC0C0C0", @TempDir) ; ($vColor, $pTargetFolder = Default(TEMP), $vWidth = Default(16), $vHeight = Default(16), $iBitMapFileOut = Default(_TempFile))
;~ Local $hTMP = _Return_colored_BitMapFile("0x345345", @TempDir,Default ,Default,"C:\sdfgdfhg.bmp") ; ($vColor, $pTargetFolder = Default(TEMP), $vWidth = Default(16), $vHeight = Default(16), $iBitMapFileOut = Default(_TempFile))
Local $hBitM = _WinAPI_LoadImage(0, $hTMP, $IMAGE_BITMAP, 16, 16, BitOR($LR_LOADTRANSPARENT, $LR_LOADFROMFILE, $LR_SHARED))
_GUICtrlMenu_SetItemBitmaps(GUICtrlGetHandle($idContextmenu), $Item2, $hBitM, $hBitM, False)
_GUICtrlMenu_SetItemBitmaps(GUICtrlGetHandle($Menu), $Item3, $hBitM, $hBitM, False)

While GUIGetMsg() <> -3
WEnd

_GDIPlus_Shutdown()


; #FUNCTION# ====================================================================================================================
; Name...........: _Return_colored_BitMapFile
; Description ...: Creates a Bitmap file based on a HEX Color
; Syntax.........: _Return_colored_BitMapFile($HexColor, $pTargetFolder = Default, $Width = Default, $Height = Default, $iBitMapFileOut = Default)
; Parameters ....: $HexColor ; Format: 0xDADADA
; Parameters ....: $pTargetFolder ; where to store the file
; Parameters ....: $Width ; width of the file to create
; Parameters ....: $Height ; Height of the file to create
; Parameters ....: $iBitMapFileOut ; Path and extension of the file to create
; Return values .: Success:     ; returns path of created bitmap-file
; Return values .: @Error = 1   ; if file was not created
; Remarks .......:
; Related .......: _GDIPlus_BitmapCreateFromScan0, _GDIPlus_ImageSaveToFile
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================

;~; ------------------------------------------------------------------------------------------------------
Func _Return_colored_BitMapFile($HexColor, $pTargetFolder = Default, $Width = Default, $Height = Default, $iBitMapFileOut = Default) ; ($Color, $pTargetFolder = Default(TEMP), $Width = Default(16), $Height = Default(16), $iBitMapFileOut = Default(_TempFile))
;~; ------------------------------------------------------------------------------
    If $Width = Default Then $Width = 16
    If $Height = Default Then $Height = 16
    If $pTargetFolder = Default Then
        $pTargetFolder = @TempDir
    ElseIf StringIsSpace($pTargetFolder) Then
        $pTargetFolder = @TempDir
    ElseIf DirCreate($pTargetFolder) = 0 Then
        $pTargetFolder = @TempDir
    EndIf

    If $iBitMapFileOut = Default Then $iBitMapFileOut = _TempFile($pTargetFolder, "~", ".bmp", 7) ; NUR bmp !!!

    If StringLen($HexColor) <> 8 Then Return SetError(1)
    Local $0xAARRGGBB = "0xFF" & StringRight($HexColor, 6) ; weiß(00) ist hier der AlphaKanal

    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($Width, $Height) ;          create an empty bitmap
    Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) ;                  get the graphics context of the bitmap
    _GDIPlus_GraphicsSetSmoothingMode($hBmpCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ; muss angegeben werden
    _GDIPlus_GraphicsClear($hBmpCtxt, $0xAARRGGBB) ; fill bitmap with color ;       Alpha, Red, Green and Blue components.
    _GDIPlus_ImageSaveToFile($hBitmap, $iBitMapFileOut) ;                           save bitmap to disk

    If @error Then Return SetError(1) ; file was not created

    If Not @Compiled Then ConsoleWrite('> ' & @TAB & @ScriptLineNumber & @TAB & '$hBmpCtxt    ' & @TAB & $hBmpCtxt & @CRLF) ; > = blue text color   BLAU
    If Not @Compiled Then ConsoleWrite('+ ' & @TAB & @ScriptLineNumber & @TAB & '$hBitmap     ' & @TAB & $hBitmap  & @CRLF) ; > = blue text color   BLAU

    If FileExists($iBitMapFileOut) Then
        Return $iBitMapFileOut ;            kein ERROR ; --> Datei-Pfad als ReturnValue
    Else
        Return SetError(1)
    EndIf

EndFunc   ;==>_Return_colored_BitMapFile
;~; ------------------------------------------------------------------------------------------------------

Is there a chance to turn the colored bitmap to icon?

Have mercy, I am new to the GDIPlus thing!

Thank you in advance.   M.

Link to post
Share on other sites
  • Developers

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

Hera ya go.

#include <GuiMenu.au3>
#include <WinAPIGdi.au3>
#include <WinAPIHObj.au3>

Local $hWnd = GUICreate("Icon Test", 200, 200)
GUISetState()

Local $idContextmenu = GUICtrlCreateContextMenu()

GUICtrlCreateMenuItem("Item 1", $idContextmenu)
Local $Item2 = GUICtrlCreateMenuItem("Item 2", $idContextmenu)
Local $Menu = GUICtrlCreateMenu("Menu", $idContextmenu)
Local $Item3 = GUICtrlCreateMenuItem("Item 3", $Menu)

$hBMP_1 = _WinAPI_CreateSolidBitmap($hWnd, 0xC0C0C0, 16, 16)
$hBMP_2 = _WinAPI_CreateSolidBitmap($hWnd, 0x345345, 16, 16)

_GUICtrlMenu_SetItemBitmaps(GUICtrlGetHandle($idContextmenu), $Item2, $hBMP_1, $hBMP_1, False)
_GUICtrlMenu_SetItemBitmaps(GUICtrlGetHandle($Menu), $Item3, $hBMP_2, $hBMP_2, False)

While GUIGetMsg() <> -3
WEnd


_WinAPI_DeleteObject($hBMP_1)
_WinAPI_DeleteObject($hBMP_2)

 

Link to post
Share on other sites

Thank you KaFu for the quick reply :thumbsup:
Your suggestion simplifies the creation of color symbols excellently. Even for MenuItems. :frantics:
This has brought me a big step forward - thank you.
However, it is not yet clear (for me) how to attach such color symbols to a tab control.

Here again a script with applied code: (I apologize for my simple-minded attempt at implementation.)

; icon_test_3
#include-once
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiMenu.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiTab.au3>

_GDIPlus_Startup() ; Color Icons + Menu Icons ; _GDIPlus_Shutdown()

Local $hWnd = GUICreate("Icon Test", 300, 200)

Local $FileMenu = GUICtrlCreateMenu("    File    ")
Local $FileMenuItem = GUICtrlCreateMenuItem("Exit", $FileMenu)

;~; -------------------------------------------------------------------------------
Local $hBMP_2 = _WinAPI_CreateSolidBitmap($hWnd, 0xC0C0C0, 16, 16)
_GUICtrlMenu_SetItemBitmaps(GUICtrlGetHandle($FileMenu), $FileMenuItem, $hBMP_2, $hBMP_2, False) ; works on Win7
;~; -------------------------------------------------------------------------------

Local $hTab = GUICtrlCreateTab(10, 50, 280, 140)
_GUICtrlTab_SetMinTabWidth($hTab, 70) ;

Local $nTab1 = GUICtrlCreateTabItem("Tab 1")
;~ GUICtrlSetImage(-1, "shell32.dll", -28, 0) ; works on Win7

;~; -------------works NOT on Win7------------------------------------------------------------------
Local $hBMP_2 = _WinAPI_CreateSolidBitmap($hWnd, 0xC0C0C0, 16, 16)
_GUICtrlMenu_SetItemBitmaps(GUICtrlGetHandle($hTab), $nTab1, $hBMP_2, $hBMP_2, False) ;
;~; -------------works NOT on Win7------------------------------------------------------------------

GUICtrlCreateTabItem("Tab 2")
GUICtrlSetImage(-1, "shell32.dll", -35, 0) ; works on Win7

GUICtrlCreateTabItem("") ; end tabitem definition ;


GUISetState()

While GUIGetMsg() <> -3
WEnd

_GDIPlus_Shutdown()

I must admit I am using old style of Tab creation what I do not want to change.

 

Link to post
Share on other sites

Hello KaFu

Thank you for this hint. 👍

Unfortunately the script you pointed to is using exactly those "new style Tabs" (_GUICtrlTab_InsertItem), which I do not want to use.

It may be that this is an easy way to create a tab with color, but it makes all further steps more difficult (for me).
I am looking for a way to create an icon file in a hex color which I can assign to the tab (for example).
It doesn't matter if a colored bitmap has to be created first. (see post #1)

I would be grateful for any advice in this direction.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...