Sign in to follow this  
Followers 0
A-L-I-E-N

Transparent child window

2 posts in this topic

Hello. I am trying to make GUI with transparent child window. I need it to place PNG on top of bitmap from parent window.
 I need to move and rotate PNG image that is why I create it in child window. But I can't make it transparent. Here is my code:

#include <windowsconstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <MsgBoxConstants.au3>
#include <ScreenCapture.au3>
#include <winapi.au3>
Global Const $AC_SRC_ALPHA = 1
Global $g_hGUI, $g_hGraphics, $g_hBmp_Buffer, $g_hGfx_Buffer, $g_hImage, $controlGui
Global $g_iMouseX, $g_iMouseY
AutoItSetOption("GUIOnEventMode", 1)

    Local $sFile = @ScriptDir & "\2.png"
    If Not FileExists($sFile) Then
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", $sFile & " not found!", 30)
        Exit
    EndIf
    Local $sFile2 = @ScriptDir & "\1.bmp"
        If Not FileExists($sFile2) Then
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", $sFile2 & " not found!", 30)
        Exit
    EndIf
$g_hGUI = GUICreate("Main", 701, 671, -1, -1)
GUISetBkColor(0xFFFFFF)
$Graphic1 = GUICtrlCreatePic($sFile2, 8, 8, 409, 657)
$Button1 = GUICtrlCreateButton("Change", 424, 8, 161, 33)

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "_ResetGraphicsTransform")
GUISetState()

$C1 = GUICreate("Child",409,657,0,0,$WS_POPUP,bitor($WS_EX_MDICHILD, $WS_EX_LAYERED),$g_hGUI)
GUISetBkColor(0xacbdef);set the child background to some colour which we can set as the transparent colour
$graphic = GUICtrlCreateGraphic(0, 0, 409, 657, $GUI_WS_EX_PARENTDRAG)
_WINAPI_SetLayeredWindowAttributes($C1, 0xacbdef, 255)

_GDIPlus_Startup()

    $g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($C1)
    $g_hBmp_Buffer = _GDIPlus_BitmapCreateFromGraphics(409, 657, $g_hGraphics)
    $g_hGfx_Buffer = _GDIPlus_ImageGetGraphicsContext($g_hBmp_Buffer)

    $g_hImage = _GDIPlus_ImageLoadFromFile($sFile)


    GUIRegisterMsg($WM_LBUTTONDOWN, "WM_LBUTTONDOWN")
    GUIRegisterMsg($WM_MOUSEWHEEL, "WM_MOUSEWHEEL")
    GUIRegisterMsg($WM_MOUSEMOVE, "WM_MOUSEMOVE")
    GUISetState(@SW_SHOW)

    _Draw()

    While Sleep(10)
    WEnd

Func _ResetGraphicsTransform()
    _GDIPlus_GraphicsResetTransform($g_hGfx_Buffer)
    _Draw()
EndFunc   ;==>_ResetGraphicsTransform

Func WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam

    $g_iMouseX = BitAND($lParam, 0x0000FFFF)
    $g_iMouseY = BitShift($lParam, 16)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_LBUTTONDOWN

Func WM_MOUSEMOVE($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg

    Switch BitAND($wParam, 0x0000FFFF)
        Case 1
            Local $iX = BitAND($lParam, 0x0000FFFF)
            Local $iY = BitShift($lParam, 16)

            _GDIPlus_GraphicsTranslateTransform($g_hGfx_Buffer, $iX - $g_iMouseX, $iY - $g_iMouseY, True)

            $g_iMouseX = $iX
            $g_iMouseY = $iY

            _Draw()
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MOUSEMOVE

Func WM_MOUSEWHEEL($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam

    Switch BitAND($wParam, 0x0000FFFF)
        Case 1
            Local $iAngle = -3
            If BitShift($wParam, 16) < 0 Then $iAngle = 3

            Local $aMousePos[2][2] = [[1]]
            $aMousePos[1][0] = $g_iMouseX
            $aMousePos[1][1] = $g_iMouseY
            _GDIPlus_GraphicsTransformPoints($g_hGfx_Buffer, $aMousePos)

            _GDIPlus_GraphicsTranslateTransform($g_hGfx_Buffer, $aMousePos[1][0], $aMousePos[1][1])
            _GDIPlus_GraphicsRotateTransform($g_hGfx_Buffer, $iAngle)
            _GDIPlus_GraphicsTranslateTransform($g_hGfx_Buffer, -$aMousePos[1][0], -$aMousePos[1][1])

        Case Else
            Local $aInfo = GUIGetCursorInfo($g_hGUI)
            Local $iScale = 1.1
            If BitShift($wParam, 16) < 0 Then $iScale = 0.9

            _GDIPlus_GraphicsTranslateTransform($g_hGfx_Buffer, -$aInfo[0], -$aInfo[1], True)
            _GDIPlus_GraphicsScaleTransform($g_hGfx_Buffer, $iScale, $iScale, True)
            _GDIPlus_GraphicsTranslateTransform($g_hGfx_Buffer, $aInfo[0], $aInfo[1], True)
    EndSwitch

    _Draw()
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MOUSEWHEEL

Func _Draw()
    _GDIPlus_GraphicsClear($g_hGfx_Buffer, "0xFFFFFFFF")

    _GDIPlus_GraphicsDrawImage($g_hGfx_Buffer, $g_hImage, 60, 70)

    _GDIPlus_GraphicsDrawImage($g_hGraphics, $g_hBmp_Buffer, 0, 0)

EndFunc   ;==>_Draw


Func _Exit()
    _GDIPlus_ImageDispose($g_hImage)
    _GDIPlus_GraphicsDispose($g_hGfx_Buffer)
    _GDIPlus_BitmapDispose($g_hBmp_Buffer)
    _GDIPlus_GraphicsDispose($g_hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($g_hGUI)
    Exit
EndFunc   ;==>_Exit

Child window is not transparent. But if I change _GDIPlus_GraphicsClear($g_hGfx_Buffer, "alpha") in "Draw" function, then child window becomes transparen but with noise and it leaves tracks when moving. How can I place a PNG on top of bitmap?

Share this post


Link to post
Share on other sites



i did it this way:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUICtrlPic.au3>
#include <Array.au3>


Global $aHoverBtn[5][7]
$aHoverBtn[0][1] = @ScriptDir & "\Pictures\Up.png"
$aHoverBtn[0][2] = @ScriptDir & "\Pictures\UpHovered.png"
$aHoverBtn[0][3] = @ScriptDir & "\Pictures\UpPressed.png"
$aHoverBtn[0][4] = @ScriptDir & "\Pictures\UpDisabled.png"
$aHoverBtn[0][5] = 0

$aHoverBtn[1][1] = @ScriptDir & "\Pictures\Down.png"
$aHoverBtn[1][2] = @ScriptDir & "\Pictures\DownHovered.png"
$aHoverBtn[1][3] = @ScriptDir & "\Pictures\DownPressed.png"
$aHoverBtn[1][4] = @ScriptDir & "\Pictures\DownDisabled.png"
$aHoverBtn[1][5] = 0

$aHoverBtn[2][1] = @ScriptDir & "\Pictures\Left.png"
$aHoverBtn[2][2] = @ScriptDir & "\Pictures\LeftHovered.png"
$aHoverBtn[2][3] = @ScriptDir & "\Pictures\LeftPressed.png"
$aHoverBtn[2][4] = @ScriptDir & "\Pictures\LeftDisabled.png"
$aHoverBtn[2][5] = 0

$aHoverBtn[3][1] = @ScriptDir & "\Pictures\Right.png"
$aHoverBtn[3][2] = @ScriptDir & "\Pictures\RightHovered.png"
$aHoverBtn[3][3] = @ScriptDir & "\Pictures\RightPressed.png"
$aHoverBtn[3][4] = @ScriptDir & "\Pictures\RightDisabled.png"
$aHoverBtn[3][5] = 0

$aHoverBtn[4][1] = @ScriptDir & "\Pictures\Exit.png"
$aHoverBtn[4][2] = @ScriptDir & "\Pictures\ExitHovered.png"
$aHoverBtn[4][3] = @ScriptDir & "\Pictures\ExitPressed.png"
$aHoverBtn[4][4] = @ScriptDir & "\Pictures\ExitDisabled.png"
$aHoverBtn[4][5] = 0

Global $nMsg, $aPos, $aInfo
Global $hGui = GUICreate("Hovered Ping-Buttons", 612, 369, 271, 235)
Global $sAutoItPath
If @Compiled Then
    $sAutoItPath=RegRead64('HKLM\Software\AutoIt v3\AutoIt', 'InstallDir')&'\'
Else
    $sAutoItPath= StringReplace(@AutoItExe, "autoit3.exe", "")&'\'
    $sAutoItPath= StringReplace(@AutoItExe, "autoit3_x64.exe", "")
EndIf

Global $Background = _GUICtrlPic_Create($sAutoItPath & "Examples\GUI\msoobe.jpg", 0, 0, 612, 369, $SS_CENTERIMAGE, Default)
GUICtrlSetState(-1, $Gui_DISABLE)
Global $idTorus = _GUICtrlPic_Create($sAutoItPath & "Examples\GUI\Torus.png", 88, 8, 68, 71, $SS_CENTERIMAGE, Default)

$aHoverBtn[0][0] = _GUICtrlPic_Create($aHoverBtn[0][1], 460, 8, 48, 48, BitOR($SS_CENTERIMAGE, $SS_NOTIFY), Default) ;Up
$aHoverBtn[1][0] = _GUICtrlPic_Create($aHoverBtn[1][1], 460, 138, 48, 48, BitOR($SS_CENTERIMAGE, $SS_NOTIFY), Default) ;Down
$aHoverBtn[2][0] = _GUICtrlPic_Create($aHoverBtn[2][1], 400, 73, 48, 48, BitOR($SS_CENTERIMAGE, $SS_NOTIFY), Default) ;Left
$aHoverBtn[3][0] = _GUICtrlPic_Create($aHoverBtn[3][1], 520, 73, 48, 48, BitOR($SS_CENTERIMAGE, $SS_NOTIFY), Default) ;Right
$aHoverBtn[4][0] = _GUICtrlPic_Create($aHoverBtn[4][1], 540, 266, 48, 48, BitOR($SS_CENTERIMAGE, $SS_NOTIFY), Default) ;Exit
GUISetState()

While 1
    _GuiCtrlPic_CheckHoverDisabled($hGui, $aHoverBtn)   ;checken ob ein anderes Pic angezeigt werden muß und bei Bedarf anzeigen
    $nMsg = GUIGetMsg()
    $aPos = ControlGetPos($hGui, "", $idTorus)
    Switch $nMsg
        Case $Gui_EVENT_CLOSE
            Exit

        ;animiert die PicButtons wie ein Button
        ;MouseDown wechselt zum Status Button gedrückt
        ;MouseUp wechselt zum vorherigen Status zurück
        Case $aHoverBtn[0][0]
            If _GuiCtrlPic_AnimButton($hGui, $aHoverBtn[0][0], $aHoverBtn[0][1],$aHoverBtn[0][2], $aHoverBtn[0][3]) Then $aPos[1] -= 5 ;nach oben
        Case $aHoverBtn[1][0]
            If _GuiCtrlPic_AnimButton($hGui, $aHoverBtn[1][0], $aHoverBtn[1][1],$aHoverBtn[1][2], $aHoverBtn[1][3]) Then $aPos[1] += 5 ;nach unten
        Case $aHoverBtn[2][0]
            If _GuiCtrlPic_AnimButton($hGui, $aHoverBtn[2][0], $aHoverBtn[2][1],$aHoverBtn[2][2], $aHoverBtn[2][3]) Then $aPos[0] -= 5 ;nach links
        Case $aHoverBtn[3][0]
            If _GuiCtrlPic_AnimButton($hGui, $aHoverBtn[3][0], $aHoverBtn[3][1],$aHoverBtn[3][2], $aHoverBtn[3][3]) Then $aPos[0] += 5 ;nach rechts
        Case $aHoverBtn[4][0]
            If _GuiCtrlPic_AnimButton($hGui, $aHoverBtn[4][0], $aHoverBtn[4][1],$aHoverBtn[4][2], $aHoverBtn[4][3]) Then Exit
        Case $GUI_EVENT_PRIMARYDOWN
            ;hier noch DRAGen einfügen
    EndSwitch
    ControlMove($hGui, "", $idTorus, $aPos[0], $aPos[1])
WEnd

Func RegRead64($sKeyname, $sValue)
    ;Aus AutoUpdateIt.au3
    Local $sRes = RegRead($sKeyname, $sValue)
    If @error And @AutoItX64 Then
        $sKeyname = StringReplace($sKeyname, "HKEY_LOCAL_MACHINE", "HKLM")
        $sKeyname = StringReplace($sKeyname, "HKLM\SOFTWARE\", "HKLM\SOFTWARE\Wow6432Node\")
        $sRes = RegRead($sKeyname, $sValue)
        If @error Then
            SetError(1)
            Return ""
        EndIf
    EndIf
    SetError(0)
    Return $sRes
EndFunc   ;==>RegRead64

using GUICtrlPic.au3 written by Großvater and a little extended by me.

pictures.zip

Share this post


Link to post
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
Sign in to follow this  
Followers 0