Sign in to follow this  
Followers 0
Yashied

Using gradients in AutoIt

5 posts in this topic

#1 ·  Posted (edited)

This function will be included in WinAPIEx.au3 UDF in the next update (v1.77). I now would like to hear your opinion about the usefulness (or not) of this function.

#Include <WinAPI.au3>

Global $aVertex[6][3] = [[0, 0, 0xFF0000], [400, 400, 0x00FF00], [0, 400, 0x0000FF], [0, 0, 0xFF0000], [400, 0, 0xFFFF00], [400, 400, 0x00FF00]]

$hForm = GUICreate('MyGUI', 400, 400)
$hDC = _WinAPI_GetDC($hForm)
GUISetState()

_WinAPI_GradientFill($hDC, $aVertex, 0, 2)
_WinAPI_GradientFill($hDC, $aVertex, 3, 5)
_WinAPI_ReleaseDC($hForm, $hDC)

Do
Until GUIGetMsg() = -3

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GradientFill
; Description....: Fills rectangle or triangle gradient.
; Syntax.........: _WinAPI_GradientFill ($hDC, $aVertex [, $iStart [, $iEnd [, $fRotate]]] )
; Parameters.....: $hDC     - Handle to the destination device context.
;                  $aVertex - The 2D array ([x1, y1, $rgb1], [x2, y2, $rgb2], ... [xN, yN, $rgbN]) that contains the necessary
;                             gradient vertices. Each vertex in this array contains the following parameters.
;
;                             x   - The x-coordinate, in logical units.
;                             y   - The y-coordinate, in logical units
;                             rgb - The color information at the point of x, y.
;
;                  $iStart  - The index of array to start filling at.
;                  $iEnd    - The index of array to stop filling at.
;                  $fRotate - Specifies whether fills a rectangle from left to right edge (horizontal gradient). $fRotate used
;                             only for the rectangular gradients, for the triangular gradients this parameter will be ignored,
;                             valid values:
;                  |TRUE  - Fills from left to right edge.
;                  |FALSE - Fills from top to bottom edge. (Default)
; Return values..: Success  - 1.
;                  Failure  - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: If the number of vertices defined by using $iStart and $iEnd parameters is 2, _WinAPI_GradientFill() function
;                  fills a rectangle. If the number of vertices is 3, fills a triangle. For the rectangle, the vertices must
;                  specify its upper left and lower right corners. Note that $aVertex array may contain any number of vertices
;                  of the gradient, but only 2 or 3 vertices may be used at the same time from this array.
;                  Otherwise, the function is fails.
;
;                  _WinAPI_GradientFill() function can only fill the rectangle or triangle at one call. Use multiple calls this
;                  function to fill a complex gradients.
; Related........:
; Link...........: @@MsdnLink@@ GdiGradientFill
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_GradientFill($hDC, $aVertex, $iStart = 0, $iEnd = -1, $fRotate = 0)

    If UBound($aVertex, 2) < 3  Then
        Return SetError(2, 0, 0)
    EndIf

    Local $Count, $Fill, $Point, $tVertex, $Struct = ''

    If $iStart < 0 Then
        $iStart = 0
    EndIf
    If ($iEnd < 0) Or ($iEnd > UBound($aVertex) - 1) Then
        $iEnd = UBound($aVertex) - 1
    EndIf
    $Point = $iEnd - $iStart + 1
    If $Point > 3 Then
        $iEnd = $iStart + 2
        $Point = 3
    EndIf
    Switch $Point
        Case 2
            $Mode = Number($fRotate = 0)
        Case 3
            $Mode = 2
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    For $i = $iStart To $iEnd
        $Struct &= 'short[8];'
    Next
    $tVertex = DllStructCreate(StringTrimRight($Struct, 1))
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    $Count = 1
    $tGradient = DllStructCreate('ulong[' & $Point & ']')
    For $i = $iStart To $iEnd
        DllStructSetData($tGradient, 1, $Count - 1, $Count)
        DllStructSetData($tVertex, $Count, _WinAPI_LoWord($aVertex[$i][0]), 1)
        DllStructSetData($tVertex, $Count, _WinAPI_HiWord($aVertex[$i][0]), 2)
        DllStructSetData($tVertex, $Count, _WinAPI_LoWord($aVertex[$i][1]), 3)
        DllStructSetData($tVertex, $Count, _WinAPI_HiWord($aVertex[$i][1]), 4)
        DllStructSetData($tVertex, $Count, BitShift(BitAND(($aVertex[$i][2]), 0xFF0000), 8), 5)
        DllStructSetData($tVertex, $Count, BitAND($aVertex[$i][2], 0x00FF00), 6)
        DllStructSetData($tVertex, $Count, BitShift(BitAND(($aVertex[$i][2]), 0x0000FF), -8), 7)
        DllStructSetData($tVertex, $Count, 0, 8)
        $Count += 1
    Next

    Local $Ret = DllCall('gdi32.dll', 'int', 'GdiGradientFill', 'hwnd', $hDC, 'ptr', DllStructGetPtr($tVertex), 'ulong', $Point, 'ptr', DllStructGetPtr($tGradient), 'ulong', 1, 'ulong', $Mode)

    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    Return 1
EndFunc   ;==>_WinAPI_GradientFill
Edited by Yashied

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

It looks beautiful!

Imho you should add it also to the very usefull WinAPIEx.au3 include!

Thanks,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Well thats pretty cool. Could you give a few more examples? (one with 2 colors, 1 color)

Would you be able to do gradient transparency?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Well thats pretty cool. Could you give a few more examples? (one with 2 colors, 1 color)

Would you be able to do gradient transparency?

#Include <WinAPIEx.au3>

Global $aVertex[7][3] = [[0, 0, 0x81F70F], [200, 400, 0x002F2F], [200, 0, 0xFD7E00], [400, 400, 0x90008E], [50, 50, 0x0000FF], [350, 100, 0xFFFF00], [150, 350, 0xF7970F]]

$hForm = GUICreate('MyGUI', 400, 400)
$hDC = _WinAPI_GetDC($hForm)
GUISetState()

_WinAPI_GradientFill($hDC, $aVertex, 0, 1)
_WinAPI_GradientFill($hDC, $aVertex, 2, 3, 1)
_WinAPI_GradientFill($hDC, $aVertex, 4, 6)
_WinAPI_ReleaseDC($hForm, $hDC)

Do
Until GUIGetMsg() = -3

_WinAPI_GradientFill() function does not support transparency.

Edited by Yashied

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