Jump to content

help with png and window transparency


Recommended Posts

I have a question with a problem I have with png files in my program. I need to get rid of the window border around my window and want everything except my image file (which is a fish) to be transparent. (you're going to have to get a subsitute image file that's 26 by 26) How do I do this?

#include <GDIPlus.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#include <misc.au3>

Global $GUI_OBJECT[1][5]; 0=hwnd, 1=width, 2=height, 3=graphics obj, 4 = img obj

Opt("GUIOnEventMode",1)
$hwnd=GUICreate("Parent",400,300)
GUISetOnEvent($GUI_EVENT_CLOSE,"close")
GUISetState()


_GDIPlus_Startup()

$GUI_OBJECT[0][0]=$hwnd
$GUI_OBJECT[0][1]=26
$GUI_OBJECT[0][2]=26
$GUI_OBJECT[0][3]=_GDIPlus_GraphicsCreateFromHWND($hwnd)
$GUI_OBJECT[0][4]=_GDIPlus_ImageLoadFromFile("background.png")
_RePaint($GUI_OBJECT[0][0],0,0,0)

GUIRegisterMsg($WM_PAINT,"_RePaint")



Do
    Sleep(20)
    If _IsPressed("01") And WinActive($hwnd) Then
        _CreateChild($hwnd,Random(0,@DesktopWidth,1),Random(0,@DesktopHeight,1),26,26,"background.png")
        Do
            Sleep(10)
        Until Not _IsPressed("01")
    EndIf
    
    
Until False


Func _CreateChild($parent,$x,$y,$w,$h,$imagename)
    ReDim $GUI_OBJECT[Ubound($GUI_OBJECT)+1][5]
    
    $GUI_OBJECT[Ubound($GUI_OBJECT)-1][0]=GUICreate("Child number: "&Ubound($GUI_OBJECT),$w,$h,$x,$y)
    GUISetState(@SW_SHOW,$GUI_OBJECT[Ubound($GUI_OBJECT)-1][0])
    $GUI_OBJECT[Ubound($GUI_OBJECT)-1][1]=$w
    $GUI_OBJECT[Ubound($GUI_OBJECT)-1][2]=$h
    $GUI_OBJECT[Ubound($GUI_OBJECT)-1][3]=_GDIPlus_GraphicsCreateFromHWND($GUI_OBJECT[Ubound($GUI_OBJECT)-1][0])
    $GUI_OBJECT[Ubound($GUI_OBJECT)-1][4]=_GDIPlus_ImageLoadFromFile($imagename)
    GUISetOnEvent($GUI_EVENT_CLOSE,"close",$GUI_OBJECT[Ubound($GUI_OBJECT)-1][0])
    _RePaint($GUI_OBJECT[Ubound($GUI_OBJECT)-1][0],0,0,0)
    
    

EndFunc


Func _RePaint($hwndparam,$msg,$wparam,$lparam)
    For $i=0 To UBound($GUI_OBJECT)-1
        If $hwndparam=$GUI_OBJECT[$i][0] Then
            _GDIPlus_GraphicsDrawImageRect($GUI_OBJECT[$i][3],$GUI_OBJECT[$i][4],0,0,$GUI_OBJECT[$i][1],$GUI_OBJECT[$i][2])
            ExitLoop
        EndIf
    Next
    
    Return $GUI_RUNDEFMSG
EndFunc



Func close()
    If @GUI_WinHandle<>$hwnd THen
        For $i=0 To UBound($GUI_OBJECT)-1
            If @GUI_WinHandle=$GUI_OBJECT[$i][0] Then
                _GDIPlus_GraphicsDispose($GUI_OBJECT[$i][3])
                _GDIPlus_ImageDispose($GUI_OBJECT[$i][4])
                GUIDelete($GUI_OBJECT[$i][0])
                _ArrayDelete($GUI_OBJECT,$i)
                Return
            EndIf
        Next
    EndIf
    
    
    
    For $i=0 To UBound($GUI_OBJECT)-1
        _GDIPlus_GraphicsDispose($GUI_OBJECT[$i][3])
        _GDIPlus_ImageDispose($GUI_OBJECT[$i][4])
    Next
    _GDIPlus_Shutdown()
       Exit
EndFunc
Link to comment
Share on other sites

i figured out i shouldnt call the DismissSplash() but when i take that out in line 9 then the png doesnt appear?

@AustrianOak, I'm sorry I could not respond to your PM's.

Perhaps this is what you are looking for - change the png filename to whatever your png is called.

#NoTrayIcon
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#Include <GDIPlus.au3>
Local $SplashGUI, $hSplashImage
Local $pngSrc, $width, $height, $i

$pngSrc = @WorkingDir & "\Splash.png"
_GDIPlus_Startup()
$hSplashImage = _GDIPlus_ImageLoadFromFile($pngSrc)
; Extract image width and height from PNG
$width = _GDIPlus_ImageGetWidth($hSplashImage)
$height = _GDIPlus_ImageGetHeight($hSplashImage)
; Create layered window
$SplashGUI = GUICreate("", $width, $height, -1, -1, $WS_POPUP, $WS_EX_LAYERED, $DS_MODALFRAME)
SetBitmap($SplashGUI, $hSplashImage, 0)
GUISetState()
WinSetOnTop($SplashGUI, "", 1)
;Sleep(500)
;fade in png background
For $i = 0 To 255 Step 5
      SetBitmap($SplashGUI, $hSplashImage, $i)
Next
;Sleep(1000)
While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE, $GUI_EVENT_PRIMARYDOWN, $GUI_EVENT_PRIMARYUP
           ExitLoop
  EndSwitch
Wend

; Below commented out, necessary only if in a separate function
;If Not IsHWnd($SplashGUI) Then
;   SetError(1)
;   Exit; <- Should be 'Return' if in a Fn.
;EndIf

;fade out png background
For $i = 255 To 0 Step - 5
    SetBitmap($SplashGUI, $hSplashImage, $i)
Next
; Release resources
_GDIPlus_ImageDispose($hSplashImage)
_GDIPlus_Shutdown()
GUIDelete($SplashGUI)

Func SetBitmap($hGUI, $hImage, $iOpacity)
Const $AC_SRC_ALPHA = 1
;Const $ULW_ALPHA = 2
Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
local $hBrush, $hFormat, $hFamily, $hFont, $tLayout,$hGraphic
    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $hGraphic = _GDIPlus_GraphicsCreateFromHDC ($hMemDC)

    $hBrush  = _GDIPlus_BrushCreateSolid (0xffFA0C0A)
    $hFormat = _GDIPlus_StringFormatCreate ()
    $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    $hFont   = _GDIPlus_FontCreate ($hFamily, 32, 1)
    $tLayout = _GDIPlus_RectFCreate ( 100, 200, 350, 50 )

    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", $AC_SRC_ALPHA)
    _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)
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
   _GDIPlus_StringFormatDispose ($hFormat)
   _GDIPlus_BrushDispose ($hBrush)
EndFunc ;==>SetBitmap
Edited by DaRam
Link to comment
Share on other sites

  • Moderators

DaRam,

Ran without problems (once I changed the Return in line 35 to Exit) and produced the result below (shown against my Vista blue desktop).

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba23. Did you have an issue like the OP w/o the change in line 35? I think the return was left over from when this code was in a separate function, in any case I've commented it out (since it is not necessary) in Post #8. AustrianOak could you check? If you have modified the code or incorporated it in your own code and are having issues, please do mention this and post relevant code.

DaRam,

Ran without problems (once I changed the Return in line 35 to Exit) and produced the result below (shown against my Vista blue desktop).

M23

Link to comment
Share on other sites

  • Moderators

DaRam,

SciTE would not let me run the script without changing that line, so I cannot say if any problems resulted from leaving it in.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 2 weeks later...

Thanks Melba23. Did you have an issue like the OP w/o the change in line 35? I think the return was left over from when this code was in a separate function, in any case I've commented it out (since it is not necessary) in Post #8. AustrianOak could you check? If you have modified the code or incorporated it in your own code and are having issues, please do mention this and post relevant code.

DaRam, sorry I've been away so long. Concerning the script, strangely when I started it up today the script worked perfectly and the png appeared and stayed until I clicked on it. I hadn't even changed any lines yet but since you said the Return on line 35 wasn't necessary I commented it out and the script remained working. One question though, does the script close once you click on the png? If so what part of the script is responsible for this?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...