How to use transparent png as GUI background?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

How to use transparent png as GUI background?

29 Mar 2014, 20:55

Code: Select all

; Code from http://www.autohotkey.com/board/topic/80924-make-windows-background-transparent-but-not-its-content/?p=514404

#SingleInstance, Force
#NoEnv
DetectHiddenWindows, On
SetBatchLines, -1
OnExit, ExitSub

#include <gdip> ; http://www.autohotkey.com/forum/topic32238.html 

Gui, -Caption +E0x80000 +AlwaysOnTop +Hwndhwnd
Gui, Show, NA

pToken := Gdip_Startup()
sFile := "bg.png"

pBitmap:=Gdip_CreateBitmapFromFile(sFile)
Gdip_GetDimensions(pBitmap, w, h)

hbm := CreateDIBSection(w,h)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
pGraphics := Gdip_GraphicsFromHDC(hdc)

Gdip_DrawImage(pGraphics, pBitmap,0,0,w,h)
UpdateLayeredWindow(hwnd, hdc, (A_ScreenWidth-w)//2, (A_ScreenHeight-h)//2, w,h)

OnMessage(0x201, "WM_LBUTTONDOWN")

Gui, Add, ListView, x30 h50 w200 h200, Colum1|Colum2 ; <----- This control can't be seen
return

GuiEscape:
GuiClose:
ExitSub:
	SelectObject(hdc, obm)
	DeleteObject(hbm)
	DeleteDC(hdc)
	Gdip_DeleteGraphics(pGraphics)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
ExitApp

WM_LBUTTONDOWN() {
	PostMessage, 0xA1, 2
}
Above code can display transparent png perfectly, but how can I add controls inside the image? Gui, Add, ListView, x30 h50 w200 h200, Colum1|Colum2 ; <----- This control can't be seen

Image

:arrow: Download: bg.png | Testing files
Last edited by tmplinshi on 26 Mar 2018, 22:24, edited 1 time in total.
CreeAll
Posts: 23
Joined: 01 Oct 2013, 12:18

Re: How to use transparent png as GUI background?

30 Mar 2014, 17:33

I use CGUI lib to do my GUIs, and I have to disable() the background to be sure the next controls overlap it. Works very well and make Gui codes prettier.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to use transparent png as GUI background?

30 Mar 2014, 21:38

Thanks for the info :), but the disable can't help in this example.
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to use transparent png as GUI background?

01 Apr 2014, 03:37

Hi tmplinshi,

it seems to be very difficult to draw controls on layered windows after you used the UpdateLayeredWindow() with per-pixel alpha. I suppose it happens because the controls are drawn with GDI functions and the alpha byte of the colors is set to zero (transparent). So this is the best I've got:

Code: Select all

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

#include <gdip>

Global HMain, XMain, YMain, HChild, XChild, YChild
sFile := "bg.png"
; ----------------------------------------------------------------------------------------------------------------------
; Main GUI
Gui, Main: New, -Caption +AlwaysOnTop +LastFound +HwndHMain +E0x80000 +LabelGui
pToken := Gdip_Startup()
pBitmap:=Gdip_CreateBitmapFromFile(sFile)
Gdip_GetDimensions(pBitmap, w, h)
hbm := CreateDIBSection(w,h)
hdc := GetDC(HMain)
mdc := CreateCompatibleDC(hdc)
obm := SelectObject(mdc, hbm)
pGraphics := Gdip_GraphicsFromHDC(mdc)
Gdip_DrawImage(pGraphics, pBitmap,0,0,w,h)
UpdateLayeredWindow(HMain, mdc, (A_ScreenWidth-w)//2, (A_ScreenHeight-h)//2, w,h)
DeleteObject(hbm)
DeleteDC(mdc)
ReleaseDC(hdc, HMain)
Gdip_DeleteGraphics(pGraphics)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
; ----------------------------------------------------------------------------------------------------------------------
; Child GUI
Gui, Child: New, -Caption +LastFound +HwndHChild +OwnerMain
Gui, Margin, 0, 0
Gui, Color, 123456
Gui, Add, ListView, x0 y0 w200 h200, Colum1|Colum2 ; <----- This control can't be seen
Loop, 10
   LV_Add("", "Col1 " . A_Index, "Col2 " . A_Index)
Gui, Add, Edit, x+10 yp w50, Edit
Gui, Add, Button, xp y+10 wp gClicked, Button
WinSet, TransColor, 123456
; ----------------------------------------------------------------------------------------------------------------------
Gui, Main: Show, w%w% h%h%
WinGetPos, XMain, YMain, W, H, ahk_id %HMain%
XChild := XMain + 33, YChild := YMain + 90
Gui, Child: Show, x%XChild% y%YChild%

OnMessage(0x0201, "WM_LBUTTONDOWN")
OnMessage(0x0216, "WM_MOVING")
OnMessage(0x0232, "WM_EXITSIZEMOVE")


Return

Clicked:
Gui, +OwnDialogs
MsgBox, Bingo!
Return

GuiEscape:
ExitApp

WM_LBUTTONDOWN() {
   If (A_Gui = "Main")
	   PostMessage, 0xA1, 2, 0, , ahk_id %HMain%
}
WM_EXITSIZEMOVE() {
   If (A_Gui = "Main")
	   WinActivate, ahk_id %HChild%
}
WM_MOVING(W, L) {
   If (A_Gui = "Main") {
      DX := NumGet(L + 0, 0, "Int") - XMain, DY := NumGet(L + 0, 4, "Int") - YMain
      WinMove, ahk_id %HChild%, , % (XChild += DX), % (YChild += DY)
      XMain += DX, YMain += DY
   }
}
BTW: The GDIP.ahk included in your ZIP doesn't work properly in an x64 environment.
Shazz

Re: How to use transparent png as GUI background?

01 Apr 2014, 05:12

Wow, someone else's post to someone else's problem just clued me in.

I was using a GDIP library not compatible with x64..

seriously,

I'm back on track, I'm an idiot
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to use transparent png as GUI background?

01 Apr 2014, 08:10

Thank you just me, tested and works very well :o. Ok, I'll use Gdip_All.ahk next time.

BTW: The "Vista Dialog" image comes from this article, which describes the techniques about Semi-transparent and Shaped Dialogs.
ozzii
Posts: 482
Joined: 30 Oct 2013, 06:04

Re: How to use transparent png as GUI background?

02 Apr 2014, 02:30

CreeAll wrote:I use CGUI lib to do my GUIs, and I have to disable() the background to be sure the next controls overlap it. Works very well and make Gui codes prettier.
Just out of curiosity.
can you post an example of a code with this lib and the link where to find the lib

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot] and 357 guests