CreateDDB() : Create alpha hBitmap with series of AARRGGBB hex values.

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

CreateDDB() : Create alpha hBitmap with series of AARRGGBB hex values.

11 Sep 2020, 20:31

The function: (along with a demo)
 

Code: Select all

CreateDDB(PixelData, W, H) {                                  ; v0.50 by SKAN on D39B/D39C
Local n:=VarSetCapacity(BMBITS, W*H*4),  pBMBITS:=&BMBITS     ;        @ tiny.cc/createddb
  Loop, Parse, PixelData, |, %A_Space%
    pBMBITS := NumPut("0x" . A_LoopField, pBMBITS+0, "Int")
Return DllCall("CreateBitmap", "Int",W, "Int",H, "Int",1, "Int",32, "Ptr",&BMBITS, "UPtr")
}




; Demo follows

#NoEnv
#Warn
#SingleInstance, Force
SetBatchLines, -1
GoSub, CreatePixelData

Gui, New,, CreateDDB() demo
Gui, Color, FFF0EE
Gui, Margin, 24, 24
Gui, Font, S10, Segoe UI

Gui, Add, Picture, xm  ym  w196 h196 hwndhPic1 0x4E
hBM := CreateDDB(PixelData1, 8, 8)
GuiControl,,%hpic1%, *w0 *h0 HBITMAP:%hBM%

Gui, Add, Picture, x+m yp   wp   hp  hwndhPic2 0x4E
hBM := CreateDDB(PixelData2, 8, 8)
GuiControl,,%hpic2%, *w0 *h0 HBITMAP:%hBM%

Gui, Add, Text,    xm  y+12 wp   h24  Center, 32bpp RGB  (non-alpha bitmap)
Gui, Add, Text,    x+m yp   wp   hp   Center, 32bpp ARGB (Opaque)

Gui, Add, Picture, xm  y+24 wp   h196 hwndhPic3 0x4E
hBM := CreateDDB(PixelData3, 8, 8)
GuiControl,,%hpic3%, *w0 *h0 HBITMAP:%hBM%

Gui, Add, Picture, x+m yp   wp   hp   hwndhPic4 0x4E
hBM := CreateDDB(PixelData4, 8, 8)
GuiControl,,%hpic4%, *w0 *h0 HBITMAP:%hBM%

Gui, Add, Text,    xm  y+12 wp   h24  Center, 32bpp ARGB (Transparency)
Gui, Add, Text,    x+m yp   wp   hp   Center, 32bpp ARGB (Transparency)

Gui, Show
Return


CreatePixelData:

PixelData1 := "
( LTrim Join|
  FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000
  000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF
  FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000
  000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF
  FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000
  000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF
  FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000
  000000|FFFFFF|000000|FFFFFF|000000|FFFFFF|000000|FFFFFF
)"

PixelData2 := "
( LTrim Join|
  FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000
  FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF
  FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000
  FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF
  FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000
  FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF
  FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000
  FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF
)"

PixelData3 := "
( LTrim Join|
  FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000
  DD000000|DDFFFFFF|DD000000|DDFFFFFF|DD000000|DDFFFFFF|DD000000|DDFFFFFF
  BBFFFFFF|BB000000|BBFFFFFF|BB000000|BBFFFFFF|BB000000|BBFFFFFF|BB000000
  99000000|99FFFFFF|99000000|99FFFFFF|99000000|99FFFFFF|99000000|99FFFFFF
  77FFFFFF|77000000|77FFFFFF|77000000|77FFFFFF|77000000|77FFFFFF|77000000
  55000000|55FFFFFF|55000000|55FFFFFF|55000000|55FFFFFF|55000000|55FFFFFF
  33FFFFFF|33000000|33FFFFFF|33000000|33FFFFFF|33000000|33FFFFFF|33000000
  11000000|11FFFFFF|11000000|11FFFFFF|11000000|11FFFFFF|11000000|11FFFFFF
)"

PixelData4 := "
( LTrim Join|
  00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000
  FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF
  00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000
  FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF
  00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000
  FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF
  00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000
  FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF|FF000000|00FFFFFF
)"

Return
 
  •    
    createddb_demo.png
    createddb_demo.png (18.51 KiB) Viewed 2876 times
 
 
 
Notes:

CreateDDB() will work only if the screen color depth is 32bpp, which has become default since Windows 8.
There is an alt version CreateDIB() which can produce normal/gradient 24bpp images but can't do alpha bitmaps.
Bitmaps created with CreateDDB() wouldn't look nice if upscaled unless target OS is Windows 7 or older.
I suggest you to create a picture control with SS_REALSIZECONTROL and then use GuiControl to update control,
without resizing and let OS fill the picture control.
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

CreateBitmap() : Links to demo scripts

11 Sep 2020, 20:32


User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: CreateDDB() : Create alpha hBitmap with series of AARRGGBB hex values.

19 Sep 2020, 07:05

Thanks @SirSocks.
I'm about to post couple of more demos for Windows API function CreateBitmap()
Do check that out too. :) :thumbup:
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

How to create single pixel bitmap?

19 Sep 2020, 07:07

The following script demonstrates how to create 1 pixel bitmap and stretch it to form lines or blocks.
 
  • Image
 

Code: Select all

; How to create single pixel bitmap?          - By SKAN. Last updated: 19-Sep-2020
; Why: Single pixel bitmaps can be streched to appear as horizontal/vertical lines

; More demos: https://www.autohotkey.com/boards/viewtopic.php?p=352102#p352102
; CreateBitmap() :  https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createbitmap

#NoEnv
#SingleInstance, Force

hbmColor1 := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0xFF0000, "Ptr")
hbmColor2 := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x79FF79, "Ptr")
hbmColor3 := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x0000FF, "Ptr")
hbmColor4 := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x000000, "Ptr")

Gui, Ex1_:New, -MinimizeBox, CreateBitmap()
Gui, Margin, 24, 24
Gui, Add, Picture, w1        h100,         HBITMAP: %hbmColor1%
Gui, Add, Picture, x+m w100  h100 Section, HBITMAP: %hbmColor2%
Gui, Add, Picture, x+m w1    h100,         HBITMAP: %hbmColor3%
Gui, Add, Picture, xs  w100  h1   y+m,     HBITMAP:*%hbmColor4% ; copy of bitmap
Gui, Add, Picture, xs  w100  h1   y+m,     HBITMAP:*%hbmColor4% ; copy of bitmap
Gui, Show
Return

Ex1_GuiClose:
 ExitApp
Return
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

How to create single pixel alpha bitmap?

19 Sep 2020, 07:11

The following script demonstrates how to create,stretch and overlay 1 pixel alpha image over picture controls.
 
Image
 

Code: Select all

; How to create single pixel alpha bitmap? - By SKAN. Last updated: 19-Sep-2020
; Why: Single pixel alpha bitmaps can be streched and overlayed over picture controls
;      to make them appear lighter/darker or tinted

; More info: https://www.autohotkey.com/boards/viewtopic.php?p=352102#p352102
; CreateBitmap() :  https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createbitmap

#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1

If ! FileExist("Butterflies.jpg")
    URLDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=10628, Butterflies.jpg

hBm_KF   := LoadPicture("Butterflies.jpg", "GDI+")
hWhite   := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x44FFFFFF, "Ptr")
hBlack   := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x44000000, "Ptr")

hTintR   := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x0AFF0000, "Ptr")
hTintG   := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x0A00FF00, "Ptr")
hTintB   := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "UIntP",0x0A0000FF, "Ptr")

Gui, Ex2_:New, -MinimizeBox, CreateBitmap()
Gui, Margin, 24, 24
Gui, Font, s10, Segoe UI

Gui, Add, Picture, xm w150 h150,  HBITMAP:*%hBm_KF%
Gui, Add, Picture, xm xp yp wp hp BackgroundTrans,  HBITMAP:*%hWhite% ; white overlay

Gui, Add, Picture, x+m, HBITMAP:*%hBm_KF%

Gui, Add, Picture, x+m, HBITMAP:*%hBm_KF%
Gui, Add, Picture, xm xp yp wp hp BackgroundTrans,  HBITMAP:*%hBlack% ; black overlay

Gui, Add, Text, xm  wp y+05 Center, Lighter
Gui, Add, Text, x+m wp      Center, Normal
Gui, Add, Text, x+m wp      Center, Darker

Gui, Add, Picture, xm w150 h150,  HBITMAP:*%hBm_KF%
Gui, Add, Picture, xm xp yp wp hp BackgroundTrans,  HBITMAP:*%hTintR% ; Red overlay

Gui, Add, Picture, x+m, HBITMAP:*%hBm_KF%
Gui, Add, Picture, xm xp yp wp hp BackgroundTrans,  HBITMAP:*%hTintG% ; Green overlay

Gui, Add, Picture, x+m, HBITMAP:*%hBm_KF%
Gui, Add, Picture, xm xp yp wp hp BackgroundTrans,  HBITMAP:*%hTintB% ; Blue overlay

Gui, Add, Text, xm  wp y+05 Center, Warmer
Gui, Add, Text, x+m wp      Center, Greener
Gui, Add, Text, x+m wp      Center, Cooler

Gui, Show
Return


Ex2_GuiClose:
 ExitApp
Return
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

GetSysColor() + quick reference

16 Jun 2021, 20:14

Code: Select all

GetSysColor(nIndex) {
Return Format("0x{5:}{6:}{3:}{4:}{1:}{2:}"
     , StrSplit(Format("{:06X}", DllCall("User32.dll\GetSysColor", "Int",nIndex)))*)
}


#NoEnv
#Warn
#SingleInstance, Force
SetBatchLines, -1


List := "
(
COLOR_3DDKSHADOW 21 Dark shadow for three-dimensional display elements.
COLOR_3DFACE 15 Face color for three-dimensional display elements and for dialog box backgrounds.
COLOR_3DHIGHLIGHT 20 Highlight color for three-dimensional display elements (for edges facing the light source.)
COLOR_3DHILIGHT 20 Highlight color for three-dimensional display elements (for edges facing the light source.)
COLOR_3DLIGHT 22 Light color for three-dimensional display elements (for edges facing the light source.)
COLOR_3DSHADOW 16 Shadow color for three-dimensional display elements (for edges facing away from the light source).
COLOR_ACTIVEBORDER 10 Active window border.
COLOR_ACTIVECAPTION 2 Active window title bar. The associated foreground color is COLOR_CAPTIONTEXT. Specifies the left side color in the color gradient of an active window's title bar if the gradient effect is enabled.
COLOR_APPWORKSPACE 12 Background color of multiple document interface (MDI) applications.
COLOR_BACKGROUND 1 Desktop.
COLOR_BTNFACE 15 Face color for three-dimensional display elements and for dialog box backgrounds. The associated foreground color is COLOR_BTNTEXT.
COLOR_BTNHIGHLIGHT 20 Highlight color for three-dimensional display elements (for edges facing the light source.)
COLOR_BTNHILIGHT 20 Highlight color for three-dimensional display elements (for edges facing the light source.)
COLOR_BTNSHADOW 16 Shadow color for three-dimensional display elements (for edges facing away from the light source).
COLOR_BTNTEXT 18 Text on push buttons. The associated background color is COLOR_BTNFACE.
COLOR_CAPTIONTEXT 9 Text in caption, size box, and scroll bar arrow box. The associated background color is COLOR_ACTIVECAPTION.
COLOR_DESKTOP 1 Desktop.
COLOR_GRADIENTACTIVECAPTION 27 Right side color in the color gradient of an active window's title bar. COLOR_ACTIVECAPTION specifies the left side color. Use SPI_GETGRADIENTCAPTIONS with the SystemParametersInfo function to determine whether the gradient effect is enabled.
COLOR_GRADIENTINACTIVECAPTION 28 Right side color in the color gradient of an inactive window's title bar. COLOR_INACTIVECAPTION specifies the left side color.
COLOR_GRAYTEXT 17 Grayed (disabled) text. This color is set to 0 if the current display driver does not support a solid gray color.
COLOR_HIGHLIGHT 13 Item(s) selected in a control. The associated foreground color is COLOR_HIGHLIGHTTEXT.
COLOR_HIGHLIGHTTEXT 14 Text of item(s) selected in a control. The associated background color is COLOR_HIGHLIGHT.
COLOR_HOTLIGHT 26 Color for a hyperlink or hot-tracked item. The associated background color is COLOR_WINDOW.
COLOR_INACTIVEBORDER 11 Inactive window border.
COLOR_INACTIVECAPTION 3 Inactive window caption. The associated foreground color is COLOR_INACTIVECAPTIONTEXT. Specifies the left side color in the color gradient of an inactive window's title bar if the gradient effect is enabled.
COLOR_INACTIVECAPTIONTEXT 19 Color of text in an inactive caption. The associated background color is COLOR_INACTIVECAPTION.
COLOR_INFOBK 24 Background color for tooltip controls. The associated foreground color is COLOR_INFOTEXT.
COLOR_INFOTEXT 23 Text color for tooltip controls. The associated background color is COLOR_INFOBK.
COLOR_MENU 4 Menu background. The associated foreground color is COLOR_MENUTEXT.
COLOR_MENUHILIGHT 29 The color used to highlight menu items when the menu appears as a flat menu (see SystemParametersInfo). The highlighted menu item is outlined with COLOR_HIGHLIGHT.
COLOR_MENUBAR 30 The background color for the menu bar when menus appear as flat menus (see SystemParametersInfo). However, COLOR_MENU continues to specify the background color of the menu popup.
COLOR_MENUTEXT 7 Text in menus. The associated background color is COLOR_MENU.
COLOR_SCROLLBAR 0 Scroll bar gray area.
COLOR_WINDOW 5 Window background. The associated foreground colors are COLOR_WINDOWTEXT and COLOR_HOTLITE.
COLOR_WINDOWFRAME 6 Window frame.
COLOR_WINDOWTEXT 8 Text in windows. The associated background color is COLOR_WINDOW.
)"

Gui, New,, GetSysColors()
Gui, Font, S9, Consolas
Gui, Margin, 0, 0

Gui, Add, ListView, w640 0x4 LV0x8200 Grid R38, Color|#|Constant|Description
LV_ModifyCol(1, "100 Integer"), LV_ModifyCol(2, "Integer 40"), LV_ModifyCol(3, "240"), LV_ModifyCol(4, "2000")

ImageListID := IL_Create(37)
LV_SetImageList(ImageListID)

Loop, Parse, List, `n
{
   L := StrSplit(A_LoopField, " ",,3)
   SysColor := GetSysColor(L.2)
   hBM := DllCall("CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",32, "IntP",SysColor+0, "Ptr")
   IL_Add(ImageListID, "HBITMAP:" . hBM, 0xFFFFFF, 1)
   LV_Add("Icon" . A_Index, SysColor, L.2, L.1, L.3)
}
Gui, Show
Return

GuiEscape:
  ExitApp
 
image.png
image.png (80.1 KiB) Viewed 2446 times
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: CreateDDB() : Create alpha hBitmap with series of AARRGGBB hex values.

10 Feb 2022, 08:46

jly wrote:
10 Feb 2022, 03:17
great work!
:thumbup:
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

CreateDDB() for V2.0

20 Sep 2023, 06:55

Changes:
LTrim not required for PixelData owing to intro of Smart LTrim
NumPut's parameters were reordered to allow a sequence of values.. and that allows input of PixelData with string manipulation.
 
 
The function: (along with a demo)
 

Code: Select all

CreateDDB(PixelData, W, H)               ; By SKAN for ah2 on D39B/D69K @ autohotkey.com/r?p=539946
{
    Local  BMBITS := Buffer(W*H*4, 0)
    NumPut( StrSplit("int,0x" StrReplace(PixelData, "|", ",int,0x") "," BMBITS.Ptr, ",")* )
    Return  DllCall("Gdi32\CreateBitmap", "int",W, "int",H, "int",1, "int",32, "ptr",BMBITS, "ptr")
}





#Requires AutoHotkey v2.0
#SingleInstance

PixelData := "
(   Join|
    FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000|FFFFFFFF|FF000000
    DD000000|DDFFFFFF|DD000000|DDFFFFFF|DD000000|DDFFFFFF|DD000000|DDFFFFFF
    BBFFFFFF|BB000000|BBFFFFFF|BB000000|BBFFFFFF|BB000000|BBFFFFFF|BB000000
    99000000|99FFFFFF|99000000|99FFFFFF|99000000|99FFFFFF|99000000|99FFFFFF
    77FFFFFF|77000000|77FFFFFF|77000000|77FFFFFF|77000000|77FFFFFF|77000000
    55000000|55FFFFFF|55000000|55FFFFFF|55000000|55FFFFFF|55000000|55FFFFFF
    33FFFFFF|33000000|33FFFFFF|33000000|33FFFFFF|33000000|33FFFFFF|33000000
    11000000|11FFFFFF|11000000|11FFFFFF|11000000|11FFFFFF|11000000|11FFFFFF
)"

hBM := CreateDDB(PixelData, 8, 8)
MyGui  :=  Gui(,"CreateDDB() demo")
MyPic  :=  MyGui.AddPicture("w196 h196 0x40")
MyPic.Value := "*w0 *h0 HBITMAP:" hBM
MyGui.Show

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 178 guests