Jump to content


Photo

[Function] CreateSolidBitmap


  • Please log in to reply
No replies to this topic

#1 jballi

jballi
  • Members
  • 940 posts

Posted 30 June 2012 - 09:24 PM

Assembled this from stuff I found on the forum not to be named. Great way to create single color images/icons on the fly. I'm a gdi amateur so please let me know 1) if you find a problem with code or 2) if the functionality has been duplicated elsewhere.

;------------------------------
;
; Function: CreateSolidBitmap
;
; Description:
;
;   Create a solid color bitmap.
;
; Parameters:
;
;   hWnd - Handle to the window where the bitmap will be displayed.
;
;   p_Color - Color to paint the bitmap.
;
;   p_Width, p_Height - Width and height of the bitmap in pixels.
;
;   p_Convert2BRG - If TRUE (the default), the color value in the p_Color
;       parameter is converted from RGB format to BRG format.  If FALSE, the
;       color value in the p_Color parameter is assumed to already be in BRG
;       format.
;
; Returns:
;
;   The handle to the bitmap object.
;
; Remarks:
;
;   The returned bitmap object remains in memory until deleted.  Use the
;   the "DeleteObject" API function to delete the object.
;
;-------------------------------------------------------------------------------
;-- ##### Not 64-bit ready/tested
CreateSolidBitmap(hWnd,p_Color,p_Width,p_Height,p_Convert2BRG=True)
    {
    if p_Convert2BRG
        p_Color:=((p_Color&0xFF)<<16)+(p_Color&0xFF00)+((p_Color>>16)&0xFF)

    ;-- Get the device context for the specified window
    hDC:=DllCall("GetDC"
        ,"UInt",hWnd        ;-- hWnd
        ,"UInt")            ;-- Return Type

    ;-- Create a temporary compatible device context to work with
    hMemDC:=DllCall("CreateCompatibleDC"
        ,"UInt",hDC         ;-- hdc
        ,"UInt")            ;-- Return Type

    ;-- Create a compatible bitmap with the specified dimensions
    hBitmap:=DllCall("CreateCompatibleBitmap"
        ,"UInt",hDC         ;-- hdc
        ,"Int",p_Width      ;-- nWidth
        ,"Int",p_Height     ;-- nHeight
        ,"UInt")            ;-- Return Type

    ;-- Select the bitmap into the temporary device context
    ;   Note: When a memory device context is created, it initially has a 1-by-1
    ;   monochrome bitmap selected into it.  This step is necessary to create a
    ;   color bitmap.
    DllCall("SelectObject"
        ,"UInt",hMemDC      ;-- hdc
        ,"UInt",hBitmap     ;-- hgdiobj
        ,"UInt")            ;-- Return Type

    ;-- Create a brush using the specified color
    hBrush:=DllCall("CreateSolidBrush"
        ,"UInt",p_Color     ;-- crColor
        ,"UInt")            ;-- Return Type

    ;-- Fill the bitmap with the specified color using the specified dimensions
    VarSetCapacity(RECT,16,0)
    NumPut(p_Width, RECT,8,"Int")   ;-- right
    Numput(p_Height,RECT,12,"Int")  ;-- bottom
    DllCall("FillRect"
        ,"UInt",hMemDC      ;-- hDC
        ,"UInt",&RECT       ;-- *lprc
        ,"UInt",hBrush)     ;-- hbr

    ;-- Delete the brush 
    DllCall("DeleteObject","UInt",hBrush)

    ;-- Delete the temporary device context
    DllCall("DeleteDC","UInt",hMemDC)

    ;-- Release the original device context
    DllCall("ReleaseDC"
        ,"UInt",hWnd        ;-- hWnd
        ,"UInt",hDC)        ;-- hDC

    ;-- Return handle to bitmap
    Return hBitmap
    }


;------------------------------
;
; Function: IL_AddBitmap
;
; Description:
;
;   Add a bitmap to an image list.
;
; Parameters:
;
;   hIL - A handle to an image list.
;
;   hImage - A handle to the bitmap that contains the image.
;
;   hMask - A handle to the bitmap that contains the mask. [Optional]  Set to
;       zero (0) (the default) to not use a mask.
;
;   p_DeleteBitmaps - Set to TRUE (the default) to delete the bitmap object(s)
;       object(s) _after_ they have been copied to the image list.  Set to FALSE
;       if the bitmaps will be used elsewhere in the script.
;
; Returns:
;
;   The zero-based index of new image, or -1 otherwise.
;
; Remarks:
;
;   The *ImageList_Add* API function copies the bitmap(s) to an internal data
;   structure.  If p_DeleteBitmaps is set to FALSE, be sure to use the
;   *DeleteObject* API function to delete the bitmaps when they are no longer
;   needed.
;
;-------------------------------------------------------------------------------
;-- ##### Not 64-bit ready/tested
IL_AddBitmap(hIL,hImage,hMask=0,p_DeleteBitmaps=True)
    {
    iIL:=DllCall("ImageList_Add"
        ,"UInt",hIL             ;-- himl 
        ,"UInt",hImage          ;-- hbmImage
        ,"UInt",hMask           ;-- hbmMask
        ,"Int")                 ;-- Return Type

    ;-- Delete the original bitmaps?
    if p_DeleteBitmaps
        {
        DllCall("DeleteObject","UInt",hImage)
        if hMask
            DllCall("DeleteObject","UInt",hMask)
        }

    Return iIL
    }

Example of use:

#NoEnv
#SingleInstance Force
ListLines Off
SetBatchLines -1  ;-- Just to start

;-- Identify handle for the to-be-created window (GUI=0)
gui +LastFound
WinGet hWindow,ID

;-- Create and populate image lists
hILSmallIcons :=IL_Create(100,100,False)
hILLargeIcons :=IL_Create(100,100,True)
outputdebug hILSmallIcons=%hILSmallIcons%, hILLargeIcons=%hILLargeIcons%

;;;;;IL_AddBitmap(hILSmallIcons,CreateSolidBitmap(hWindow,0xFF0000,16,16))    ;-- Red
;;;;;IL_AddBitmap(hILSmallIcons,CreateSolidBitmap(hWindow,0x00FF00,16,16))    ;-- Green
;;;;;IL_AddBitmap(hILSmallIcons,CreateSolidBitmap(hWindow,0x0000FF,16,16))    ;-- Blue

;;;;;IL_AddBitmap(hILLargeIcons,CreateSolidBitmap(hWindow,0xFF0000,32,32))    ;-- Red
;;;;;IL_AddBitmap(hILLargeIcons,CreateSolidBitmap(hWindow,0x00FF00,32,32))    ;-- Green
;;;;;IL_AddBitmap(hILLargeIcons,CreateSolidBitmap(hWindow,0x0000FF,32,32))    ;-- Blue

Loop 99
    {
    Random RandomColor,0x000000,0xFFFFFF
    IL_AddBitmap(hILSmallIcons,CreateSolidBitmap(hWindow,RandomColor,16,16))    ;-- Random color
    IL_AddBitmap(hILLargeIcons,CreateSolidBitmap(hWindow,RandomColor,32,32))    ;-- Random color
    }

;-- Options
gui Margin,0,0
gui -MinimizeBox -MaximizeBox

;-- Objects
gui Add,ListView,w600 r20 +Grid hWndhLV vLV,Column 1|Column 2|Column 3

;-- Attach image lists to the ListView control
LV_SetImageList(hILSmallIcons,1)
LV_SetImageList(hILLargeIcons,0)

;-- Populate the ListView control
Loop 99
    LV_Add("Icon" . A_Index,"Just some text for row " . A_Index,"Row" . A_Index . "Col2","Row" . A_Index . "Col3")

LV_ModifyCol(1,"AutoHdr")
LV_ModifyCol(2,"AutoHdr")
LV_ModifyCol(3,"AutoHdr")

;-- Buttons
gui Add,Button,xm      gIconView,%A_Space%    Icon    %A_Space%
gui Add,Button,x+0  wp gSmallIconView,Small Icon
gui Add,Button,x+0  wp gListView,List
gui Add,Button,x+0  wp gTileView,Tile
gui Add,Button,x+0  wp gReportView,Report
gui Add,Button,X+10 wp gReload,Reload...

;-- Show it
gui Show
SetBatchLines 10ms  ;-- System default
return


GUIClose:
GUIEscape:
ExitApp

IconView:
GUIControl +Icon,LV
return

SmallIconView:
GUIControl +IconSmall,LV
return

ListView:
GUIControl +List,LV
return

TileView:
GUIControl +Tile,LV
return

ReportView:
GUIControl +Report,LV
return

Reload:
Reload
return