Jump to content
Sign in to follow this  

Gradient Background

Recommended Posts


i was just wondering how exactly i could make the backgrounf of a GUI be gradient from one color to while, so for example if th euser selected blue it woudl be blue into white, or red and have red into white, like in windows live messenger.


Admin Of:http://notmyspace.info [Under Development, looking for volunteers to help improve]http://PSNetCards.co.ukhttp://ZacnAndLindsey.com [Under development, not quite sure what to do with it yet]http://revelm.com------------------------------------Radio Streams: [128kb/s 44kHz] [22kb/s 22kHz](works on mobile phones)-----------------------------------My Server:Owned By: http://jumpline.comIP: Space:1TBNetwork Connection: 1GB/S[up and down]Operating System: Red Hat LinuxInstalled Apps:Webmail, phpBB, Majordomo, phpMyAdmin, MySQL, Active Server Pages, FrontPage Extensions 2002, GraphicsMagick, Mod Perl, Perl, PHP: Hypertext Preprocessor, Python(want cheap good webhosting, or need a place to park your domain? contact me)-----------------------------------


Share this post

Link to post
Share on other sites

Share this post

Link to post
Share on other sites

Hi, I had a play with one of Malkey's GDI+ gradient line function and it works quite well

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#include <WinAPI.au3>

Global $ApW = 600, $ApH = 400
Global $iColor[2] = ["0xFFFF00","0x0000FF"], $Label[2], $Button[2]
Global $Direction = 0

$hGui = GUICreate("Gradient", $ApW, $ApH)
$Pic = GUICtrlCreatePic("", 0, 0, $ApW, $ApH)
GUICtrlSetState(-1, $GUI_DISABLE)
$Combo = GUICtrlCreateCombo("", 5, 70, 120, 20, 0x0003)
GUICtrlSetData(-1, "Horizontal|Vertical|Forward Diagonal|Backward Diagonal", "Horizontal")
$Button[0] = GUICtrlCreateButton("Select 1st Color", 5, 5, 100, 25)
$Label[0] = GUICtrlCreateLabel($iColor[0], 110, 5, 60, 25, 0x301)
GUICtrlSetBkColor(-1, $iColor[0])
GUICtrlSetColor(-1, $iColor[1])
$Button[1] = GUICtrlCreateButton("Select 2nd Color", 5, 35, 100, 25)
$Label[1] = GUICtrlCreateLabel($iColor[1], 110, 35, 60, 25, 0x301)
GUICtrlSetBkColor(-1, $iColor[1])
GUICtrlSetColor(-1, $iColor[0])
GUISetState(@SW_SHOW, $hGui)

PicSetGradient($Pic, $ApW, $ApH, $iColor[0], $iColor[1], $Direction)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
        Case $Button[0]
            Local $SC = SetColor(0)
            If $SC Then PicSetGradient($Pic, $ApW, $ApH, $iColor[0], $iColor[1], $Direction)
        Case $Button[1]
            Local $SC = SetColor(1)
            If $SC Then PicSetGradient($Pic, $ApW, $ApH, $iColor[0], $iColor[1], $Direction)
        Case $Combo
            Switch GUICtrlRead($Combo)
                Case "Horizontal"
                    $Direction = 0x00000000
                Case "Vertical"
                    $Direction = 0x00000001
                Case "Forward Diagonal"
                    $Direction = 0x00000002
                Case "Backward Diagonal"
                    $Direction = 0x00000003
            PicSetGradient($Pic, $ApW, $ApH, $iColor[0], $iColor[1], $Direction)

Func SetColor($iD)
    Local $Clr
    $Clr = _ChooseColor(2, 0, 2, $hGui)
    If $Clr <> -1 Then
        $iColor[$iD] = $Clr
        If $iD Then 
            GUICtrlSetColor($Label[$iD], $iColor[0])
            GUICtrlSetColor($Label[0], $iColor[1])
            GUICtrlSetColor($Label[$iD], $iColor[1])
            GUICtrlSetColor($Label[1], $iColor[0])
        GUICtrlSetData($Label[$iD], $iColor[$iD])
        GUICtrlSetBkColor($Label[$iD], $iColor[$iD])
        Return 1
    Return 0

Func PicSetGradient($cID, $iW, $iH, $iClr1, $iClr2, $iDirection)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0    
    Local $hWnd, $iC1, $iC2, $hBitmap, $hImage, $hGraphic, $hBrushLin, $hbmp, $aBmp
    $hWnd = GUICtrlGetHandle($cID)
    $iC1 = StringReplace($iClr1, "0x", "0xFF")
    $iC2 = StringReplace($iClr2, "0x", "0xFF")  
    $hBitmap = _WinAPI_CreateBitmap($iW, $iH, 1, 32)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hBrushLin = _GDIPlus_CreateLineBrushFromRect(0, 0, $iW, $iH, -1, -1, $iC1, $iC2, $iDirection)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iW, $iH, $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])
    For $i = $Combo To $Label[1]
        GUICtrlSetState($i, $GUI_SHOW)

Func _Quit()
;==== 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 = -1 Then $iArgb1 = 0xFF0000FF
    If $iArgb2 = -1 Then $iArgb2 = 0xFFFF0000
    If $LinearGradientMode = -1 Then $LinearGradientMode = 0x00000001
    If $WrapMode = -1 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)
    $tPositions = DllStructCreate("float[" & $iCount & "]")
    $pPositions = DllStructGetPtr($tPositions)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tPositions, 1, $aPositions[$iI], $iI + 1)

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


PS: I agree with BrettF in regards to the sig is bullshit.

If anyone want to know your servers, assets, intrests or the color of your nickers then they'd look at your profile.

You got a user profile About Me page.. Why not use it!

Share this post

Link to post
Share on other sites

I really like your example.

You inspired me to add the ability to change the Colour Factors and Colour Positions arrays.

Also added Gamma Correction as an after thought. It sometimes improves graphics.

Decided to post in Example Scripts here:-


It make a good utility for selecting parameters for that perfect gradient.

Share this post

Link to post
Share on other sites

Hi, I had a play with one of Malkey's GDI+ gradient line function and it works quite well

Very nice.

Maybe you could add


in case someone wants the $WS_GUISIZEBOX for their gui.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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