代码之家  ›  专栏  ›  技术社区  ›  Ralph Thomas Hopper

PNG背景透明度

  •  1
  • Ralph Thomas Hopper  · 技术社区  · 7 年前

    #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
    

    但是背景会着色(没有透明度)。任何指点都很好。

    1 回复  |  直到 7 年前
        1
  •  1
  •   user4157124    7 年前

    我使用 a snippet from AutoIt's forums (如果你没有,我会先去那里寻求支持)。它工作得很好。有几个小问题,但没有什么是在个案基础上无法解决的。试试看!

    #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