Sign in to follow this  
Followers 0
taz742

_GDIPlus_PicSetGradient()

3 posts in this topic

#1 ·  Posted (edited)

I've play with GDIPlus User fonction and finally I wrote this one from other.

It fill a Pic Control with a gradient color linear or not.

Capture:

Posted Image

#include <GDIPlus.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>

$hGui = GUICreate("Pic Gradients", 670, 450)
GUISetState()

Global $picW = 100, $picH = 100

Dim $aBlends3[3] = [0xFFFFFFFF, 0xFFAA0000, 0xFF000000];3 colors gradient set
Dim $aBlends4[4] = [0xFF00FF00, 0xFFFFFF00, 0xFF00FFFF, 0xFFFF0000];4 colors gradient set
Dim $aPositions3[3] = [0, 0.8, 1];custom positions array for 3 colors
Dim $aPositions4[4] = [0, 0.2, 0.4, 1]; custom positions array for 4 colors

$Pic1 = GUICtrlCreatePic("", 10+($picW+10)*0, 10+($picH+10)*0, $picW, $picH)
$Pic2 = GUICtrlCreatePic("", 10+($picW+10)*1, 10+($picH+10)*0, $picW, $picH)
$Pic3 = GUICtrlCreatePic("", 10+($picW+10)*0, 10+($picH+10)*1, $picW, $picH)
$Pic4 = GUICtrlCreatePic("", 10+($picW+10)*1, 10+($picH+10)*1, $picW, $picH)
_GDIPlus_PicSetGradient($Pic1, $aBlends3, 0x00000000);Linear Gradient Horizontal 3 colors
_GDIPlus_PicSetGradient($Pic2, $aBlends3, 0x00000001);Linear Gradient Vertical 3 colors
_GDIPlus_PicSetGradient($Pic3, $aBlends3, 0x00000002);Linear Gradient ForwardDiagonal 3 colors
_GDIPlus_PicSetGradient($Pic4, $aBlends3, 0x00000003);Linear Gradient BackwardDiagonal 3 colors

$Pic5 = GUICtrlCreatePic("", 10+($picW+10)*3, 10+($picH+10)*0, $picW, $picH)
$Pic6 = GUICtrlCreatePic("", 10+($picW+10)*4, 10+($picH+10)*0, $picW, $picH)
$Pic7 = GUICtrlCreatePic("", 10+($picW+10)*3, 10+($picH+10)*1, $picW, $picH)
$Pic8 = GUICtrlCreatePic("", 10+($picW+10)*4, 10+($picH+10)*1, $picW, $picH)
_GDIPlus_PicSetGradient($Pic5, $aBlends3, 0x00000000, $aPositions3);Custom Gradient Horizontal 3 colors
_GDIPlus_PicSetGradient($Pic6, $aBlends3, 0x00000001, $aPositions3);Custom Gradient Vertical 3 colors
_GDIPlus_PicSetGradient($Pic7, $aBlends3, 0x00000002, $aPositions3);Custom Gradient ForwardDiagonal 3 colors
_GDIPlus_PicSetGradient($Pic8, $aBlends3, 0x00000003, $aPositions3);Custom Gradient BackwardDiagonal 3 colors

$Pic9 = GUICtrlCreatePic("", 10+($picW+10)*0, 10+($picH+10)*2, $picW, $picH)
$Pic10 = GUICtrlCreatePic("", 10+($picW+10)*1, 10+($picH+10)*2, $picW, $picH)
$Pic11 = GUICtrlCreatePic("", 10+($picW+10)*0, 10+($picH+10)*3, $picW, $picH)
$Pic12 = GUICtrlCreatePic("", 10+($picW+10)*1, 10+($picH+10)*3, $picW, $picH)
_GDIPlus_PicSetGradient($Pic9, $aBlends4, 0x00000000);Linear Gradient Horizontal 4 colors
_GDIPlus_PicSetGradient($Pic10, $aBlends4, 0x00000001);Linear Gradient Vertical 4 colors
_GDIPlus_PicSetGradient($Pic11, $aBlends4, 0x00000002);Linear Gradient ForwardDiagonal 4 colors
_GDIPlus_PicSetGradient($Pic12, $aBlends4, 0x00000003);Linear Gradient BackwardDiagonal 4 colors

$Pic13 = GUICtrlCreatePic("", 10+($picW+10)*3, 10+($picH+10)*2, $picW, $picH)
$Pic14 = GUICtrlCreatePic("", 10+($picW+10)*4, 10+($picH+10)*2, $picW, $picH)
$Pic15 = GUICtrlCreatePic("", 10+($picW+10)*3, 10+($picH+10)*3, $picW, $picH)
$Pic16 = GUICtrlCreatePic("", 10+($picW+10)*4, 10+($picH+10)*3, $picW, $picH)
_GDIPlus_PicSetGradient($Pic13, $aBlends4, 0x00000000, $aPositions4);Custom Gradient Horizontal 4 colors
_GDIPlus_PicSetGradient($Pic14, $aBlends4, 0x00000001, $aPositions4);Custom Gradient Vertical 4 colors
_GDIPlus_PicSetGradient($Pic15, $aBlends4, 0x00000002, $aPositions4);Custom Gradient ForwardDiagonal 4 colors
_GDIPlus_PicSetGradient($Pic16, $aBlends4, 0x00000003, $aPositions4);Custom Gradient BackwardDiagonal 4 colors
Do
    Sleep(25)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;==== _GDIPlus_PicSetGradient === Taz742 function
; Description  - Fill a Pic control with linear or custom gradient from a set of boundary points and boundary colors.
; ===========
; $cID         - Pic ControlID
;
; $aBlend      - Pointer to an array of ARGB colors that specify the colors to be interpolated
;                for this linear gradient brush. A color of a given index in the blend array
;                corresponds to the blend position of that same index in the positions array.
;
; $iDirection  - {Opt} GradientHorizontal       = 0x00000000 <Default>
;                      GradientVertical         = 0x00000001
;                      GradientForwardDiagonal  = 0x00000002
;                      GradientBackwardDiagonal = 0x00000003
;
; $aPositions  - {Opt} If non-array or non specified, a linear array will be internaly created.
;                Pointer to an array of real numbers that specify blend factors' positions. Each number in the array
;                indicates a percentage of the distance between the starting boundary and the ending boundary
;                and is in the range from 0.0 through 1.0, where 0.0 indicates the starting boundary of the
;                gradient and 1.0 indicates the ending boundary. There must be at least two positions
;                specified: the first position, which is always 0.0, and the last position, which is always
;                1.0. Otherwise, the behavior is undefined. A blend position between 0.0 and 1.0 indicates a
;                line, parallel to the boundary lines, that is a certain fraction of the distance from the
;                starting boundary to the ending boundary. For example, a blend position of 0.7 indicates
;                the line that is 70 percent of the distance from the starting boundary to the ending boundary.
;
; Requirement:  #include <GDIPlus.au3>
;=============  _GDIPlus_CreateLineBrushFromRect()
;               _GDIPlus_SetLinePresetBlend()
;=================================================
Func _GDIPlus_PicSetGradient($cID, $aBlends, $iDirection = 0x00000000, $aPositions = "")
    If Not IsArray($aBlends) Then Dim $aBlends[2] = [0xFFFFFFFF, 0xFF000000]
    If Not IsArray($aPositions) Then
        Local $BlendsDim = UBound($aBlends)
        Dim $aPositions[$BlendsDim]
        For $i=0 To $BlendsDim -1
            $aPositions[$i] = 1/($BlendsDim-1)*$i
        Next
    EndIf
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local $hWnd, $cID_Pos, $hBitmap, $hImage, $hGraphic, $hBrushLin, $hbmp, $aBmp
    $hWnd = GUICtrlGetHandle($cID)
    $cID_Pos = ControlGetPos("", "", $hWnd)
    $hBitmap = _WinAPI_CreateBitmap($cID_Pos[2], $cID_Pos[3], 1, 32)
    _GDIPlus_Startup()
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hBrushLin = _GDIPlus_CreateLineBrushFromRect(0, 0, $cID_Pos[2], $cID_Pos[3], $aPositions, $aPositions, $aBlends[0], $aBlends[UBound($aBlends)-1], $iDirection)
    _GDIPlus_SetLinePresetBlend($hBrushLin, $aBlends, $aPositions)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $cID_Pos[2], $cID_Pos[3], $hBrushLin)
    $hbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $aBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hbmp)
    If $aBmp[0] <> 0 Then _WinAPI_DeleteObject($aBmp[0])
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_BrushDispose($hBrushLin)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hbmp)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>_GDIPlus_PicSetGradient

;====_GDIPlus_CreateLineBrushFromRect === Malkey's function
;Description - Creates a LinearGradientBrush object from a set of boundary points and boundary colors.
; $aFactors - If non-array, default array will be used.
;           Pointer to an array of real numbers that specify blend factors. Each number in the array
;           specifies a percentage of the ending color and should be in the range from 0.0 through 1.0.
;$aPositions - If non-array, default array will be used.
;            Pointer to an array of real numbers that specify blend factors' positions. Each number in the array
;            indicates a percentage of the distance between the starting boundary and the ending boundary
;            and is in the range from 0.0 through 1.0, where 0.0 indicates the starting boundary of the
;            gradient and 1.0 indicates the ending boundary. There must be at least two positions
;            specified: the first position, which is always 0.0, and the last position, which is always
;            1.0. Otherwise, the behavior is undefined. A blend position between 0.0 and 1.0 indicates a
;            line, parallel to the boundary lines, that is a certain fraction of the distance from the
;            starting boundary to the ending boundary. For example, a blend position of 0.7 indicates
;            the line that is 70 percent of the distance from the starting boundary to the ending boundary.
;            The color is constant on lines that are parallel to the boundary lines.
; $iArgb1    - First Top color in 0xAARRGGBB format
; $iArgb2    - Second color in 0xAARRGGBB format
; $LinearGradientMode -  LinearGradientModeHorizontal       = 0x00000000,
;                        LinearGradientModeVertical         = 0x00000001,
;                        LinearGradientModeForwardDiagonal  = 0x00000002,
;                        LinearGradientModeBackwardDiagonal = 0x00000003
; $WrapMode  - WrapModeTile       = 0,
;              WrapModeTileFlipX  = 1,
;              WrapModeTileFlipY  = 2,
;              WrapModeTileFlipXY = 3,
;              WrapModeClamp      = 4
; GdipCreateLineBrushFromRect(GDIPCONST GpRectF* rect, ARGB color1, ARGB color2,
;             LinearGradientMode mode, GpWrapMode wrapMode, GpLineGradient **lineGradient)
; Reference:  http://msdn.microsoft.com/en-us/library/ms534043(VS.85).aspx

Func _GDIPlus_CreateLineBrushFromRect($iX, $iY, $iWidth, $iHeight, $aFactors, $aPositions, _
        $iArgb1 = 0xFF0000FF, $iArgb2 = 0xFFFF0000, $LinearGradientMode = 0x00000001, $WrapMode = 0)

    Local $tRect, $pRect, $aRet, $tFactors, $pFactors, $tPositions, $pPositions, $iCount

    If $iArgb1 = Default Then $iArgb1 = 0xFF0000FF
    If $iArgb2 = Default Then $iArgb2 = 0xFFFF0000
    If $LinearGradientMode = -1 Or $LinearGradientMode = Default Then $LinearGradientMode = 0x00000001
    If $WrapMode = -1 Or $LinearGradientMode = Default Then $WrapMode = 1

    $tRect = DllStructCreate("float X;float Y;float Width;float Height")
    $pRect = DllStructGetPtr($tRect)
    DllStructSetData($tRect, "X", $iX)
    DllStructSetData($tRect, "Y", $iY)
    DllStructSetData($tRect, "Width", $iWidth)
    DllStructSetData($tRect, "Height", $iHeight)

    ;Note: Withn _GDIPlus_Startup(), $ghGDIPDll is defined
    $aRet = DllCall($ghGDIPDll, "int", "GdipCreateLineBrushFromRect", "ptr", $pRect, "int", $iArgb1, _
            "int", $iArgb2, "int", $LinearGradientMode, "int", $WrapMode, "int*", 0)

    If IsArray($aFactors) = 0 Then Dim $aFactors[4] = [0.0, 0.4, 0.6, 1.0]
    If IsArray($aPositions) = 0 Then Dim $aPositions[4] = [0.0, 0.3, 0.7, 1.0]

    $iCount = UBound($aPositions)
    $tFactors = DllStructCreate("float[" & $iCount & "]")
    $pFactors = DllStructGetPtr($tFactors)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tFactors, 1, $aFactors[$iI], $iI + 1)
    Next
    $tPositions = DllStructCreate("float[" & $iCount & "]")
    $pPositions = DllStructGetPtr($tPositions)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tPositions, 1, $aPositions[$iI], $iI + 1)
    Next

    $hStatus = DllCall($ghGDIPDll, "int", "GdipSetLineBlend", "hwnd", $aRet[6], _
            "ptr", $pFactors, "ptr", $pPositions, "int", $iCount)
    Return $aRet[6] ; Handle of Line Brush
EndFunc   ;==>_GDIPlus_CreateLineBrushFromRect

;===========================================================
; Description:  Sets the colors to be interpolated for this linear gradient brush and their
;            corresponding blend positions.
; Parameters
; $hBrush    [in] Pointer to the LinearGradientBrush object.
; $aBlend   [in] Pointer to an array of ARGB colors that specify the colors to be interpolated
;            for this linear gradient brush. A color of a given index in the blend array
;            corresponds to the blend position of that same index in the positions array.
; $aPositions [in] Pointer to an array of real numbers that specify the blend positions.
;               Each number in the array specifies a percentage of the distance between
;               the starting boundary and the ending boundary and is in the range from
;               0.0 through 1.0, where 0.0 indicates the starting boundary of the gradient
;               and 1.0 indicates the ending boundary. There must be at least two positions
;               specified: the first position, which is always 0.0f, and the last position,
;               which is always 1.0f. Otherwise, the behavior is undefined. A blend position
;               between 0.0 and 1.0 indicates the line, parallel to the boundary lines,
;               that is a certain fraction of the distance from the starting boundary to the
;               ending boundary. For example, a blend position of 0.7 indicates the line that
;               is 70 percent of the distance from the starting boundary to the ending boundary.
;               The color is constant on lines that are parallel to the boundary lines.
; GdipSetLinePresetBlend(GpLineGradient *brush, GDIPCONST ARGB *blend, GDIPCONST REAL* positions, INT count)
; Reference:  http://msdn.microsoft.com/en-us/library/ms534043(VS.85).aspx

Func _GDIPlus_SetLinePresetBlend($hBrush, $aBlend = 0, $aPositions = 0)
    Local $iCount, $tBlend, $pBlend, $tPositions, $pPositions, $hStatus, $res
   
    ;                                      Red,      Yellow,      Green,      Blue
    If IsArray($aBlend) = 0 Then Dim $aBlend[4] = [0xFFFF0000, 0xffffff00, 0xFF00FF00, 0xFF0000FF]
    If IsArray($aPositions) = 0 Then Dim $aPositions[4] = [0.0, 0.3, 0.7, 1.0]

    $iCount = UBound($aPositions)
    $tBlend = DllStructCreate("int[" & $iCount & "]")
    $pBlend = DllStructGetPtr($tBlend)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tBlend, 1, $aBlend[$iI], $iI + 1)
    Next
    $tPositions = DllStructCreate("float[" & $iCount & "]")
    $pPositions = DllStructGetPtr($tPositions)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tPositions, 1, $aPositions[$iI], $iI + 1)
    Next

    $hStatus = DllCall($ghGDIPDll, "int", "GdipSetLinePresetBlend", "hwnd", $hBrush, _
            "int", $pBlend, "ptr", $pPositions, "int", $iCount)
    Return
EndFunc   ;==>_GDIPlus_SetLinePresetBlend
Edited by taz742

Share this post


Link to post
Share on other sites



very well done!


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

very well done!

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