Jump to content

How to Call AlphaBlend() API From Autoit ?


komalo
 Share

Recommended Posts

hi

i am trying to use AlphaBlend Function ( From WinGDI ) in my autoit Script but it always returns

ERROR_INVALID_PARAMETER which means that on of the paameter is invalid

AlphaBlend : http://msdn.microsoft.com/en-us/library/ms532324(VS.85).aspx

AlphaBlend The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.

BOOL AlphaBlend(

HDC hdcDest, // handle to destination DC

int nXOriginDest, // x-coord of upper-left corner

int nYOriginDest, // y-coord of upper-left corner

int nWidthDest, // destination width

int nHeightDest, // destination height

HDC hdcSrc, // handle to source DC

int nXOriginSrc, // x-coord of upper-left corner

int nYOriginSrc, // y-coord of upper-left corner

int nWidthSrc, // source width

int nHeightSrc, // source height

BLENDFUNCTION blendFunction // alpha-blending function

);

Here's my code

Func _WinAPI_AlphaBlend($hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc)
  
      Local Const $AC_SRC_OVER = 0x00
      Local Const $AC_SRC_ALPHA = 0x01
  
      $tBlendFunction = DllStructCreate("BYTE BlendOp;BYTE BlendFlags;BYTE SourceConstantAlpha;BYTE AlphaFormat");i thought the problem is in $tagBLENDFUNCTION varible so i wrote it myself
      $pBlendFunction = DllStructGetPtr($tBlendFunction)
      DllStructSetData($tBlendFunction, "BlendOp", $AC_SRC_OVER)
      DllStructSetData($tBlendFunction, "BlendFlags", 0)
      DllStructSetData($tBlendFunction, "SourceConstantAlpha", 255)
      DllStructSetData($tBlendFunction, "AlphaFormat", $AC_SRC_ALPHA)
      
      Local $aResult = DllCall("Msimg32.dll", "int", "AlphaBlend", _
                                     "hwnd", $hDestDC, _
                                      "int", $iXDest, _
                                      "int", $iYDest, _
                                      "int", $iWidthDest, _
                                      "int", $iHeightDest, _
                                     "hwnd", $hSrcDC, _
                                      "int", $iXSrc, _
                                      "int", $iYSrc, _
                                      "int", $iWidthSrc, _
                                      "int", $iHeightSrc, _
                                      "ptr", $pBlendFunction)
      MsgBox("","",_WinAPI_GetLastError() & " " & $aResult[0] ); to know what is the result , ERROR_INVALID_PARAMETER = 87
      Return $aResult[3]
  EndFunc
Edited by komalo
[font="Palatino Linotype"][size="3"]AutoIt Script Examples :[/size][/font][font="Palatino Linotype"][size="3"]_CaptureBehindWindowGlass CMD for Windows Vista/Seven[/size][/font][left][/left][font="Palatino Linotype"][size="3"]Non AutoIt Script programs : Border Skin - Aero Glass On XP[/size][/font]
Link to comment
Share on other sites

no , i don't mean how to alphablend a window , there's a WinAPI named

AlphaBlend i am trying to call from autoit but i couldn't call it

[font="Palatino Linotype"][size="3"]AutoIt Script Examples :[/size][/font][font="Palatino Linotype"][size="3"]_CaptureBehindWindowGlass CMD for Windows Vista/Seven[/size][/font][left][/left][font="Palatino Linotype"][size="3"]Non AutoIt Script programs : Border Skin - Aero Glass On XP[/size][/font]
Link to comment
Share on other sites

I figured out how to use it. I will include it in my GDI UDFs and then post the example in this thread.

//Edit: the example: startet by Greenhorn and finished by me :)

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; Authors: Greenhorn, Prog@ndy
#Region                 Alpha Blending a Bitmap

;    The following code sample divides a window into three horizontal areas.
;    Then it draws an alpha-blended bitmap in each of the window areas as follows: 

;       * In the top area, constant alpha = 50% but there is no source alpha. 
;       * In the middle area, constant alpha = 100% (disabled) and source alpha is 0 (transparent) in the middle of the bitmap and 0xff (opaque) elsewhere. 
;       * In the bottom area, constant alpha = 75% and source alpha changes. 

#include <WindowsConstants.au3>
#include <Constants.au3>
#include <WinAPI.au3>
#include "GDI.au3"

Global $NULL = Ptr (0)

; Fensterstile
Global Const $WS_EX_COMPOSITED = 0x2000000   ; Gegen "Flackern"

; Nachricht zum Zeichnen abfangen.
GUIRegisterMsg ($WM_PAINT, 'MY_WM_PAINT')

; Das übliche ...
$hWnd = GUICreate ('Alpha Blending a Bitmap', 400, 350, -1, -1, _
                    $WS_OVERLAPPEDWINDOW, $WS_EX_COMPOSITED)
GUISetBkColor (0xFF0000, $hWnd)

GUISetState (@SW_SHOW)


While True
    
    Switch GUIGetMsg ()
        Case -3
            ExitLoop
    EndSwitch
WEnd


Func MY_WM_PAINT ($hWnd, $msg, $lParam, $wParam)
    
     Local $hDC, $ps, $lpPs

     ; Zeichenstruktur erzeugen
     $ps   = DllStructCreate ($tagPAINTSTRUCT)
     $lpPs = DllStructGetPtr ($ps)
     
     ; Gerätekontext wird in WM_PAINT von BeginPaint zurückgegeben
     $hDC = _GDI_BeginPaint ($hWnd, $lpPs)
     
     DrawAlphaBlend ($hWnd, $hDC)
    
     ; Am Ende von WM_PAINT muss der Gerätekontext
     ; mit EndPaint wieder freigegeben werden !
     _GDI_EndPaint ($hWnd, $lpPs)
     Return

EndFunc

; by Greenhorn, Prog@ndy
; MSDN source: http://msdn.microsoft.com/en-us/library/ms532295(VS.85).aspx
Func DrawAlphaBlend ($hWnd, $hdcwnd)

     Local $hdc;          handle of the DC we will create
     Local $hbitmap;      bitmap handle
     Local $bf     = DllStructCreate ($tagBLENDFUNCTION); structure for alpha blending
     Local $bmi    = DllStructCreate ($tagBITMAPINFO);    bitmap header
     Local $pvBits  ;    pointer to DIB section
     Local $ulWindowWidth, $ulWindowHeight;   window width/height
     Local $ulBitmapWidth, $ulBitmapHeight;   bitmap width/height
     Local $rt;             used for getting window dimensions
     Local $x, $y;          stepping variables
     Local $ubAlpha;        used for doing transparent gradient
     Local $ubRed
     Local $ubGreen
     Local $ubBlue
     Local $fAlphaFactor;   used to do premultiply
    
    
    ; get window dimensions
     $rt = _WinAPI_GetClientRect ($hWnd)
     $rtLeft    = DllStructGetData ($rt, "Left")
     $rtTop     = DllStructGetData ($rt, "Top")
     $rtRight   = DllStructGetData ($rt, "Right")
     $rtBottom  = DllStructGetData ($rt, "Bottom")

    ; calculate window width/height
     $ulWindowWidth  = $rtRight  - $rtLeft
     $ulWindowHeight = $rtBottom - $rtTop

    ; make sure we have at least some window size
     If ((Not $ulWindowWidth) Or (Not $ulWindowHeight)) Then _
         Return
         
    ; divide the window into 3 horizontal areas
     $ulWindowHeight = $ulWindowHeight / 3

    ; create a DC for our bitmap -- the source DC for AlphaBlend

     $hdc = _WinAPI_CreateCompatibleDC ($hdcwnd)

     $ulBitmapWidth  = $ulWindowWidth  - ($ulWindowWidth  / 5) * 2
     $ulBitmapHeight = $ulWindowHeight - ($ulWindowHeight / 5) * 2


    ; setup bitmap info
    ; set the bitmap width and height to 60% of the width and height of each of the three horizontal areas.
    ; Later on, the blending will occur in the center of each of the three areas.
     DllStructSetData ($bmi, 'Size' ,       DllStructGetSize (DllStructCreate($tagBITMAPINFOHEADER,1)))
     DllStructSetData ($bmi, 'Width',       $ulBitmapWidth)
     DllStructSetData ($bmi, 'Height',      $ulBitmapHeight)
     DllStructSetData ($bmi, 'Planes',      1)
     DllStructSetData ($bmi, 'BitCount',    32 );  four 8-bit components
     DllStructSetData ($bmi, 'Compression', $BI_RGB)
     DllStructSetData ($bmi, 'SizeImage',   $ulBitmapWidth * $ulBitmapHeight * 4)

    ; create our DIB section and select the bitmap into the DC
     $hbitmap = _GDI_CreateDIBSection ($hdcwnd, $bmi, $DIB_RGB_COLORS, $pvBits, 0, 0)
    Local $cbSize = DllStructGetData ($bmi, 'SizeImage')
    $pvBits = DllStructCreate ('uint[' & $cbSize / 4 & ']', $pvBits) ; Bitmap Bits
     _WinAPI_SelectObject ($hdc, $hbitmap)


    ; in top window area, constant alpha = 50%, but no source alpha
    ; the color format for each pixel is 0xaarrggbb
    ; set all pixels to blue and set source alpha to zero
     For $y = 0 To $ulBitmapHeight-1
        For $x = 1 To $ulBitmapWidth
             DllStructSetData ($pvBits, 1, 0x000000FF,  $x+($y*$ulBitmapWidth) )
;~           ConsoleWrite(@error)
         Next
;~       ConsoleWrite( @CRLF)
     Next
    
     DllStructSetData ($bf, 1    , $AC_SRC_OVER)
     DllStructSetData ($bf, 2 , 0)
     DllStructSetData ($bf, 3 , 0x7F) ;  half of 0xff = 50% transparency
     DllStructSetData ($bf, 4, 0);   ignore source alpha channel
    $bfp = DllStructGetPtr($bf)
    ConsoleWrite($bfp & @CRLF)
;~      If (Not _WinAPI_AlphaBlend ($hdcwnd, $ulWindowWidth / 5, $ulWindowHeight / 5, _
;~                               $ulBitmapWidth, $ulBitmapHeight, _
;~                               $hdc, 0, 0, $ulBitmapWidth, $ulBitmapHeight, $bfp)) Then 
;~      ConsoleWrite(_WinAPI_GetLastErrorMessage() & @CRLF)
;~          Return ;     // alpha blend failed
;~      EndIf
     If (Not _GDI_AlphaBlend ($hdcwnd, $ulWindowWidth / 5, $ulWindowHeight / 5, _
                                 $ulBitmapWidth, $ulBitmapHeight, _
                                 $hdc, 0, 0, $ulBitmapWidth, $ulBitmapHeight, $bfp)) Then 
        ConsoleWrite(_WinAPI_GetLastErrorMessage() & @CRLF)
         Return ;     // alpha blend failed
     EndIf

    ; in middle window area, constant alpha = 100% (disabled), source
    ; alpha is 0 in middle of bitmap and opaque in rest of bitmap
     For $y = 0 To $ulBitmapHeight - 1
         For $x = 0 To $ulBitmapWidth-1
             If (($x > Int($ulBitmapWidth  / 5)) And ($x < Int($ulBitmapWidth  - ($ulBitmapWidth  / 5))) And _
                 ($y > Int($ulBitmapHeight / 5)) And ($y < Int($ulBitmapHeight - ($ulBitmapHeight / 5)))) Then
                ; in middle of bitmap: source alpha = 0 (transparent).
                ; This means multiply each color component by 0x00.
                ; Thus, after AlphaBlend, we have a, 0x00 * r,
                ; 0x00 * g,and 0x00 * b (which is 0x00000000)
                ; for now, set all pixels to red
                 DllStructSetData ($pvBits, 1, 0x00FF0000, $x+1 + $y* $ulBitmapWidth);
             Else
                ; in the rest of bitmap, source alpha = 0xff (opaque)
                ; and set all pixels to blue
                 DllStructSetData ($pvBits, 1, 0xFF0000FF, $x+1 + $y* $ulBitmapWidth);
             EndIf
         Next
     Next
    
     DllStructSetData ($bf, 'Op'    , $AC_SRC_OVER)
     DllStructSetData ($bf, 'Flags' , 0)
     DllStructSetData ($bf, 'Alpha' , 0xFF);  opaque (disable constant alpha)
     DllStructSetData ($bf, 'Format', $AC_SRC_ALPHA);  use source alpha

     If (Not _GDI_AlphaBlend ($hdcwnd, $ulWindowWidth / 5, $ulWindowHeight / 5 + $ulWindowHeight, _
                                 $ulBitmapWidth, $ulBitmapHeight, _
                                 $hdc, 0, 0, $ulBitmapWidth, $ulBitmapHeight, $bf)) Then _
         Return;

    ; bottom window area, use constant alpha = 75% and a changing
    ; source alpha. Create a gradient effect using source alpha, and
    ; then fade it even more with constant alpha
     $ubRed   = 0x00;
     $ubGreen = 0x00;
     $ubBlue  = 0xff;

     For $y = 0 To $ulBitmapHeight - 1
         For $x = 0 To $ulBitmapWidth -1
            ; for a simple gradient, base the alpha value on the x
            ; value of the pixel
             $ubAlpha = $x / $ulBitmapWidth * 255;
            ;calculate the factor by which we multiply each component 
             $fAlphaFactor = $ubAlpha / 0xff;
            ; multiply each pixel by fAlphaFactor, so each component
            ; is less than or equal to the alpha value.
             DllStructSetData ($pvBits, 1, _
                         BitOR (BitAND(BitShift ($ubAlpha, -24),0xFF000000), _                  ; 0xaa000000
                            BitAND(BitShift (($ubRed   * $fAlphaFactor), -16),0x00FF0000) , _   ; 0x00rr0000
                            BitAND(BitShift (($ubGreen * $fAlphaFactor), - 8) ,0x0000FF00) , _   ; 0x0000gg00
                            BitAND(($ubBlue  * $fAlphaFactor),0x000000FF)), _                   ; 0x000000bb
                            $x+1 + $y* $ulBitmapWidth)
         Next
     Next

     DllStructSetData ($bf, 'Op'    , $AC_SRC_OVER)
     DllStructSetData ($bf, 'Flags' , 0)
     DllStructSetData ($bf, 'Alpha' , 0xBF) ;  use constant alpha, with 75% opaqueness
     DllStructSetData ($bf, 'Format', $AC_SRC_ALPHA);  use source alpha

     _GDI_AlphaBlend ($hdcwnd, $ulWindowWidth / 5, $ulWindowHeight / 5 + 2 * $ulWindowHeight, _
                         $ulBitmapWidth, $ulBitmapHeight, _
                         $hdc, 0, 0, $ulBitmapWidth, $ulBitmapHeight, $bf)

    ; do cleanup
     _WinAPI_DeleteObject ($hbitmap)
     _WinAPI_DeleteDC ($hdc)


EndFunc

#EndRegion
;***************************************************************************
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

wow great example but you forget to post the function (AlphaBlend) in the Post

Edited by komalo
[font="Palatino Linotype"][size="3"]AutoIt Script Examples :[/size][/font][font="Palatino Linotype"][size="3"]_CaptureBehindWindowGlass CMD for Windows Vista/Seven[/size][/font][left][/left][font="Palatino Linotype"][size="3"]Non AutoIt Script programs : Border Skin - Aero Glass On XP[/size][/font]
Link to comment
Share on other sites

The function should be included in my GDI UDF... http://www.autoitscript.com/forum/index.php?showtopic=86598 (or link in my siganture)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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

  • Recently Browsing   0 members

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