Jump to content

png with background transparent into gui without border


Recommended Posts

Here is a small example (from @UEZ:

#include <ButtonConstants.au3>
#include <MsgBoxConstants.au3>
#include <StructureConstants.au3>
#include <WinAPIConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


_GDIPlus_Startup()
Global Const $SC_DRAGMOVE = 0xF012
Global $iW, $iH, $hImage, $hBitmap, $hGUI
$hImage = _GDIPlus_BitmapCreateFromFile("C:\Program Files\AutoIt3\Examples\GUI\Torus.png")
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)
$hGUI = GUICreate("", $iW, $iH, -1, -1, $WS_POPUP, $WS_EX_LAYERED)
GUISetState()
_WinAPI_BitmapDisplayTransparentInGUI($hBitmap, $hGUI)
GUIRegisterMsg($WM_LBUTTONDOWN, "_WM_LBUTTONDOWN")


Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($hBitmap)
_GDIPlus_BitmapDispose($hImage)
_GDIPlus_Shutdown()
GUIDelete()

Func _WinAPI_BitmapDisplayTransparentInGUI(ByRef $hHBitmap, ByRef $hGUI, $iOpacity = 0xFF, $bReleaseGDI = True)
    If Not BitAND(GUIGetStyle($hGUI)[1], $WS_EX_LAYERED) = $WS_EX_LAYERED Then Return SetError(1, 0, 0)
    Local $tDim = DllStructCreate($tagBITMAP)
    If Not _WinAPI_GetObject($hHBitmap, DllStructGetSize($tDim), DllStructGetPtr($tDim)) Then Return SetError(2, 0, 0)
    Local $tSize = DllStructCreate($tagSIZE), $tSource = DllStructCreate($tagPOINT), $tBlend = DllStructCreate($tagBLENDFUNCTION)
    Local Const $hScrDC = _WinAPI_GetDC(0), $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC), $hOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
    $tSize.X = $tDim.bmWidth
    $tSize.Y = $tDim.bmHeight
    $tBlend.Alpha = $iOpacity
    $tBlend.Format = 1
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, DllStructGetPtr($tSize), $hMemDC, DllStructGetPtr($tSource), 0, DllStructGetPtr($tBlend), $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteDC($hMemDC)
    If $bReleaseGDI Then _WinAPI_DeleteObject($hHBitmap)
    Return True
EndFunc

Func _WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    _SendMessage($hGUI, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
EndFunc   ;==>_WM_LBUTTONDOWN

 

Edited by AutoBert
Link to comment
Share on other sites

Here a other example I also think it is from UEZ

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

 _GDIPlus_Startup()

$hSplashlogo = _GDIPlus_ImageLoadFromFile("C:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png")


$logoheight = (@DesktopHeight/2) - (_GDIPlus_ImageGetHeight($hSplashlogo)/2) - 50
$logowidth = (@DesktopWidth /2) - (_GDIPlus_ImageGetWidth($hSplashlogo)/2)
$SplashGUIlogo = GUICreate("", _GDIPlus_ImageGetWidth($hSplashlogo), _GDIPlus_ImageGetHeight($hSplashlogo), $logowidth, $logoheight, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))
_SetBitmap($SplashGUIlogo, $hSplashlogo, 255, _GDIPlus_ImageGetWidth($hSplashlogo), _GDIPlus_ImageGetHeight($hSplashlogo))
GUISetState(@SW_SHOWNA, $SplashGUIlogo)


While 1
    Sleep(20)
WEnd

 _GDIPlus_Shutdown()


Func _SetBitmap($hGUI, $hImage, $iOpacity, $n_width = 200, $n_height = 200)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>_SetBitmap

 

Edited by nend
Link to comment
Share on other sites

  • 2 years later...
  • 2 years later...
On 5/24/2016 at 3:27 PM, nend said:

Here a other example I also think it is from UEZ

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

 _GDIPlus_Startup()

$hSplashlogo = _GDIPlus_ImageLoadFromFile("C:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png")


$logoheight = (@DesktopHeight/2) - (_GDIPlus_ImageGetHeight($hSplashlogo)/2) - 50
$logowidth = (@DesktopWidth /2) - (_GDIPlus_ImageGetWidth($hSplashlogo)/2)
$SplashGUIlogo = GUICreate("", _GDIPlus_ImageGetWidth($hSplashlogo), _GDIPlus_ImageGetHeight($hSplashlogo), $logowidth, $logoheight, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))
_SetBitmap($SplashGUIlogo, $hSplashlogo, 255, _GDIPlus_ImageGetWidth($hSplashlogo), _GDIPlus_ImageGetHeight($hSplashlogo))
GUISetState(@SW_SHOWNA, $SplashGUIlogo)


While 1
    Sleep(20)
WEnd

 _GDIPlus_Shutdown()


Func _SetBitmap($hGUI, $hImage, $iOpacity, $n_width = 200, $n_height = 200)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>_SetBitmap

 

i know this  nearly 5 years ago, but can maybe @UEZ explain me how to get on top of this gui comboboxes or sliders or labels

Best Regards

Link to comment
Share on other sites

Hi Räuber @Hotzenplotz

try this:

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WinAPIConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

 _GDIPlus_Startup()

Global Const $SC_DRAGMOVE = 0xF012

$hSplashlogo = _GDIPlus_ImageLoadFromFile("C:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png")

$iw = _GDIPlus_ImageGetWidth($hSplashlogo)
$ih = _GDIPlus_ImageGetHeight($hSplashlogo)
$SplashGUIlogo = GUICreate("", $iw, $ih, -1, -1, $WS_POPUP,  $WS_EX_LAYERED)
_SetBitmap($SplashGUIlogo, $hSplashlogo, 255, $iw, $ih)
$hGUI_c = GUICreate("Test", $iw, $ih, 0, 0, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $SplashGUIlogo)
GUISetBkColor(0x123456, $hGUI_c)
_WinAPI_SetLayeredWindowAttributes($hGUI_c, 0x123456)
$idComboBox = GUICtrlCreateCombo("Item 1",  $iw / 2 - 35, 20, 50, 20)
GUICtrlSetData($idComboBox, "Item 2|Exit", "Item 2")
$idButton_Close = GUICtrlCreateButton("X", $iw / 2 - 30, $ih - 60, 35, 25)
$idLabel = GUICtrlCreateLabel("Test GUI", 0, $ih / 2 - 15, $iw - 20, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE, $SS_SUNKEN))
GUICtrlSetBkColor(-1, 0x404040)
GUICtrlSetColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOWNA, $SplashGUIlogo)
GUISetState(@SW_SHOW, $hGUI_c)

GUIRegisterMsg($WM_LBUTTONDOWN, "_WM_LBUTTONDOWN")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $idButton_Close
            ExitLoop
        Case $idComboBox
            $sComboRead = GUICtrlRead($idComboBox)
            If $sComboRead = "Exit" Then ExitLoop
    EndSwitch
WEnd

GUIDelete($hGUI_c)
GUIDelete($SplashGUIlogo)
_GDIPlus_ImageDispose($hSplashlogo)
_GDIPlus_Shutdown()
Exit

Func _WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    _SendMessage($SplashGUIlogo, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
EndFunc   ;==>_WM_LBUTTONDOWN

Func _SetBitmap($hGUI, $hImage, $iOpacity, $n_width = 200, $n_height = 200)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>_SetBitmap

Should look like this:

Result-W10.jpg

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

UEZ ended before us :)
This is what I had so far, based on AutoBert's 1st post above (another script by UEZ), after having added this :

$hGUI2 = GUICreate("GUI2", 120, 120, -1, -1, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $hGUI)
GUISetBkColor( 0xABCDEF, $hGUI2) ; transparent
_WinAPI_SetLayeredWindowAttributes($hGUI2, 0xABCDEF)
GUICtrlCreateLabel("HELLO THERE", 60, 12, 60, 60)
GUICtrlSetColor(-1, 0xFFFF00) ; yellow
GUICtrlSetFont(-1, 10, 600)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

582580920_pnglabelintransparentgui.png.733804fda88e961d6996fb24d733fc44.png

@UEZ : could you please explain BitOR($WS_POPUP, $WS_CHILD) in your script ?
I always read that these two shouldn't be found together, also it seems to work with $WS_POPUP only.
Thanks !

 

Link to comment
Share on other sites

21 minutes ago, pixelsearch said:

@UEZ : could you please explain BitOR($WS_POPUP, $WS_CHILD) in your script ?
I always read that these two shouldn't be found together, also it seems to work with $WS_POPUP only.
Thanks !

This is a leftover from the coding session - can be removed as MS states: "The window is a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style."

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

another question,

 

why 

this works 

$close = GUICtrlCreateButton("", 363, 622, 75,35)
GUICtrlSetOnEvent($close, "Close")
GUICtrlSetTip($close, "Ausschalten")

but if i try this with GUICtrlCreateLabel the event not is not working

Best Regards

Link to comment
Share on other sites

5 minutes ago, Hotzenplotz said:

another question,

 

why 

this works 

$close = GUICtrlCreateButton("", 363, 622, 75,35)
GUICtrlSetOnEvent($close, "Close")
GUICtrlSetTip($close, "Ausschalten")

but if i try this with GUICtrlCreateLabel the event not is not working

Best Regards

seems the label must have a text to work, can i get it working without a text 

Link to comment
Share on other sites

@Hotzenplotz : probably because you created the label without caption, like this :

$idLabel = GUICtrlCreateLabel("", ...)

Just add some spaces as caption ("     ", ...) using the _StringRepeat() function if you prefer, maybe add a $SS_SUNKEN style to the label during the coding session, to display a border around your label etc...

@UEZ : in your script just above, or even in your other script found in AutoBert's post at the beginning of this thread, can't we bypass the functions _SetBitmap() or _WinAPI_BitmapDisplayTransparentInGUI() and replace them with a one-liner _WinAPI_UpdateLayeredWindowEx(), like this :

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

Global Const $SC_DRAGMOVE = 0xF012
Global $hGUI

$sFileName = @ScriptDir & "\torus.png"
If Not FileExists($sFileName) Then Exit MsgBox(0, "Exit", "Image not found")

_GDIPlus_Startup()
$hImage = _GDIPlus_BitmapCreateFromFile($sFileName)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)

$hGUI = GUICreate("", $iW, $iH, -1, -1, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_TOPMOST))
_WinAPI_UpdateLayeredWindowEx($hGUI, -1, -1, $hBitmap) ; <=============================

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_LBUTTONDOWN, "_WM_LBUTTONDOWN")

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

GUIDelete()
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_BitmapDispose($hImage)
_GDIPlus_Shutdown()

Func _WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    _SendMessage($hGUI, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
EndFunc   ;==>_WM_LBUTTONDOWN

Thanks for your advice

Edited by pixelsearch
added exit test (in case image not found)
Link to comment
Share on other sites

3 hours ago, pixelsearch said:

@UEZ : in your script just above, or even in your other script found in AutoBert's post at the beginning of this thread, can't we bypass the functions _SetBitmap() or _WinAPI_BitmapDisplayTransparentInGUI() and replace them with a one-liner _WinAPI_UpdateLayeredWindowEx(), like this :

You are right, under the hood it is the same function. I don't know each of the Winapi GDI function which Yashied has added to his famous WinAPIEx UDF. WinAPIEx UDF was added to Autoit meanwhile. 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@pixelsearch thanks, some things to easy if you know them ^^

now finally i am nearly finished, but i have one annoying thing. if i start the script its an streaming radio, i put in labels to switch between favorite stations, and if i change the stations the name of it and fav numbers are shown on the script in an other label with text. the strange thing is after i start the script and i press a fav then it change the station but the label with the info stays empty until i press the fav again, then the info is shown.'

Solution found in call the guis into the fav functions

Edited by Hotzenplotz
solution found
Link to comment
Share on other sites

  • 1 year later...

Hello,

after such a long time, the posts here have been very helpful for me today. Thanks for that.

I can now display .png files with transparency. My question about this:

Can I also display a transparent png scaled? I want it to scale proportionally to the resolution (or number of pixels).
That shouldn't be a problem if I could even manage to scale a png in any way when displaying it.

It's easy with jpg's, but as a beginner I still don't have a solution for png's.:wacko:

If anyone wants to try it out, here is my example script, which I adapted
from the examples above for this purpose without knowing much about it: 2in1 (haha).au3

I've simplified my question here:D:

how.jpg

PNG1.png

PNG2.png

2in1 (haha).au3

Link to comment
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
 Share

×
×
  • Create New...