Add icons with subset of images to CodeJock CommandBars

From DataFlex Wiki
Revision as of 01:36, 2 March 2019 by Wil (talk | contribs) (Fix category Codejock spelling into the expected one)
Jump to navigationJump to search

How to add icons with subset of images to CodeJock CommandBars.

If you add an icon with a subset of images to the CodeJock Commandbars, it will use the first icon in the set and in most cases it will look awful.

This function will use a different approach and use CodeJock's imagelist to load the icon. The result is that the appropriate icon in the set will be used instead of the first. The drawback is that the icon files must be in the path, embedding the images in the exe-file is not enough.

Just add the function below to your project (below "Use cCJCommandBarSystem.pkg" or such).

    Function AddImageType for cCJCommandBarSystem Boolean bIsIcon String sImage Integer iId Integer eImageType Returns Integer
        Handle hBitmap
        Variant vImageManager
        Handle hoImageIcons
        Integer iVoid eType 
        Boolean bOk
        String sImageName
        
        Move sImage to sImageName
        
        If (iId=0) Begin
            Get piLastImageId to iId
            Increment iId
            Set piLastImageId to iId
        End
        
        Move (If(bIsIcon,IMAGE_ICON,IMAGE_BITMAP)) to eType
        
        Move (LoadImage(GetModuleHandle(0), sImage, eType, 0, 0, 0)) to hBitmap
        If (hBitmap =0) Begin // the bitmap was not in the EXE resource
            Get_File_Path sImage to sImage // find path in DFPATH, if appropriate
            If (sImage <>"") Begin // The image was found!
                Move (LoadImage(0, sImage, eType, 0, 0, LR_LOADFROMFILE)) to hBitmap
            End
        End
    
        If hBitmap Begin
            // if a bitmap it appears we must add this via an image manager
            If (not(bIsIcon)) Begin
                Get Create U_cCJImageManagerIcons to hoImageIcons
                Get ComIcons to vImageManager
                Set pvComObject of hoImageIcons to vImageManager
                Send ComAddBitmap of hoImageIcons hBitmap iId eImageType False
                Send destroy of hoImageIcons
            End
            Else Begin
                // First check if we can find the icon file.
                // Loading from file works much better with subset of icons
                Get_File_Path sImageName to sImageName // find path in DFPATH, if appropriate
                If (sImageName <>"") Begin // The image was found!
                    Get Create U_cCJImageManagerIcons to hoImageIcons
                    Get ComIcons to vImageManager
                    Set pvComObject of hoImageIcons to vImageManager
                    Send ComLoadIcon of hoImageIcons sImageName iId eImageType 
                    Send destroy of hoImageIcons
                End
                // We where not able to find the file, use original code.
                Else Begin
                    // this works with alpha blends - even when passed false
                    Send ComAddIconHandle hBitmap iId eImageType False
                End
            End
            Move (DeleteObject(hBitmap)) to iVoid
        End
        Function_Return (If(hBitmap<>0,iId,0))
    End_Function