Function Reference


_WinAPI_AlphaBlend

Displays bitmaps that have transparent or semitransparent pixels

#include <WinAPIGdi.au3>
_WinAPI_AlphaBlend ( $hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iAlpha [, $bAlpha = False] )

Parameters

$hDestDC Handle to the destination device context.
$iXDest The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
$iYDest The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
$iWidthDest The width, in logical units, of the destination rectangle.
$iHeightDest The height, in logical units, of the destination rectangle.
$hSrcDC Handle to the source device context.
$iXSrc The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
$iYSrc The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
$iWidthSrc The width, in logical units, of the source rectangle.
$iHeightSrc The height, in logical units, of the source rectangle.
$iAlpha The alpha transparency value to be used on the entire source bitmap.
This value is combined with any per-pixel alpha values in the source bitmap.
If you set $iAlpha to 0, it is assumed that your image is transparent.
Set $iAlpha value to 255 (opaque) when you only want to use per-pixel alpha values.
$bAlpha [optional] Specifies whether uses an alpha channel from the source bitmap, valid values:
    True    - Use the alpha channel (that is, per-pixel alpha).
        Note that the APIs use premultiplied alpha, which means that the red, green and blue channel values in the bitmap must be premultiplied with the alpha channel value.
        For example, if the alpha channel value is x, the red, green and blue channels must be multiplied by x and divided by 255 prior to the call.
    False - Do not use the alpha channel (Default).

Return Value

Success: True.
Failure: False.

Remarks

If the source rectangle and destination rectangle are not the same size, the source bitmap is stretched to match the destination rectangle.
If the _WinAPI_SetStretchBltMode() function is used, the stretching mode value is automatically converted to $COLORONCOLOR for this function (that is, $BLACKONWHITE, $WHITEONBLACK, and $HALFTONE are changed to $COLORONCOLOR).

If destination and source bitmaps do not have the same color format, _WinAPI_AlphaBlend() function converts the source bitmap to match the destination bitmap.

See Also

Search GdiAlphaBlend in MSDN Library.

Example

#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPIRes.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

Opt('TrayAutoPause', 0)

; Load image
Global $g_hBitmap = _WinAPI_LoadImage(0, @ScriptDir & '\Extras\AutoIt.bmp', $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
Local $tSIZE = _WinAPI_GetBitmapDimension($g_hBitmap)
Local $W = DllStructGetData($tSIZE, 'X')
Local $H = DllStructGetData($tSIZE, 'Y')

; Create GUI
Global $g_hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), $W, $H + 26)
Global $g_idPic = GUICtrlCreatePic('', 0, 0, $W, $H)
GUICtrlCreateGraphic(0, $H, $W, 1)
GUICtrlSetBkColor(-1, 0xDFDFDF)
Global $g_idSlider = GUICtrlCreateSlider(0, $H + 1, $W, 25, BitOR($TBS_BOTH, $TBS_NOTICKS))
Global $g_hSlider = GUICtrlGetHandle(-1)
GUICtrlSetLimit(-1, 255, 0)
GUICtrlSetData(-1, 255)

; Set bitmap to control with alpha
_SetBitmapAlpha($g_idPic, $g_hBitmap, 255)

; Register WM_HSCROLL message for live scrolling and show GUI
GUIRegisterMsg($WM_HSCROLL, 'WM_HSCROLL')
GUISetState(@SW_SHOW)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _SetBitmapAlpha($hWnd, $hBitmap, $iAlpha)
    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
        If Not $hWnd Then
            Return 0
        EndIf
    EndIf

    Local $aW[2], $aH[2]
    Local $tRECT = _WinAPI_GetClientRect($hWnd)
    $aW[0] = DllStructGetData($tRECT, 3) - DllStructGetData($tRECT, 1)
    $aH[0] = DllStructGetData($tRECT, 4) - DllStructGetData($tRECT, 2)
    Local $tSIZE = _WinAPI_GetBitmapDimension($hBitmap)
    $aW[1] = DllStructGetData($tSIZE, 1)
    $aH[1] = DllStructGetData($tSIZE, 2)
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateCompatibleBitmapEx($hDC, $aW[0], $aH[0], 0xFFFFFF)
    Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBmp)
    Local $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBitmap)
    _WinAPI_AlphaBlend($hDestDC, 0, 0, $aW[0], $aH[0], $hSrcDC, 0, 0, $aW[1], $aH[1], $iAlpha, 0)
    _WinAPI_SelectObject($hDestDC, $hDestSv)
    _WinAPI_DeleteDC($hDestDC)
    _WinAPI_SelectObject($hSrcDC, $hSrcSv)
    _WinAPI_DeleteDC($hSrcDC)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Local $hObj = _SendMessage($hWnd, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp)
    If $hObj Then
        _WinAPI_DeleteObject($hObj)
    EndIf
    $hObj = _SendMessage($hWnd, $STM_GETIMAGE)
    If $hObj <> $hBmp Then
        _WinAPI_DeleteObject($hBmp)
    EndIf
    Return 1
EndFunc   ;==>_SetBitmapAlpha

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

    Switch $hWnd
        Case $g_hForm
            Switch $lParam
                Case $g_hSlider
                    _SetBitmapAlpha($g_idPic, $g_hBitmap, GUICtrlRead($g_idSlider))
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_HSCROLL