Jump to content

How do I make a gradient on a label?


Recommended Posts

Not that I know of. You can easily make a graphic control with a gradient if you don't want to use an image.

To make a graphic, do something like this:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

GUICreate("Gradient", 200, 200)
GUISetState()
; The color at the beginning, 0xFFFFFF
$startRed = 0xFF
$startGreen = 0xFF
$startBlue = 0xFF
; The color at the end, 0x000000
$endRed = 0x00
$endGreen = 0x00
$endBlue = 0x00

; Number of steps in the gradient
$steps = 100

; Width/Height of gradient
$width = 100
$height = 50

$gradient = GUICtrlCreateGraphic(0, 0, $width, $height)

For $x = 1 To $width
    For $y = 1 To $height
        GUICtrlSetGraphic($gradient, $GUI_GR_MOVE, $x, $y)
        $currentStep = ($x / $width) * $steps
        $stepRed = Hex(($endRed - $startRed) + $currentStep * ($endRed - $startRed / $steps), 2)
        $stepGreen = Hex(($endGreen - $startGreen) + $currentStep * ($endGreen - $startGreen / $steps),2)
        $stepBlue = Hex(($endBlue - $startBlue) + $currentStep * ($endBlue - $startBlue / $steps),2)
        
        $color = "0x" & $stepRed & $stepGreen & $stepBlue
        
        GUICtrlSetGraphic($gradient, $GUI_GR_COLOR, $color)
        GUICtrlSetGraphic($gradient, $GUI_GR_PIXEL, $x, $y)
    Next
;MsgBox(1, "", $color)
Next

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
WEnd

Basically you just use a percent complete ($currentstep) to find out how much color to add to each value, red green and blue.

Only downside is that graphic controls suck muttley

Regards,Josh

Link to comment
Share on other sites

Haha, my bad... forgot to have it refresh the graphic (hence why graphics suck)... use this code:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

GUICreate("Gradient", 200, 200)
GUISetState()
; The color at the beginning, 0xFFFFFF
$startRed = 0xFF
$startGreen = 0xFF
$startBlue = 0xFF
; The color at the end, 0x000000
$endRed = 0x00
$endGreen = 0x00
$endBlue = 0x00

; Number of steps in the gradient
$steps = 10

; Width/Height of gradient
$width = 100
$height = 50

$gradient = GUICtrlCreateGraphic(0, 0, $width, $height)

For $x = 1 To $width
    For $y = 1 To $height
        GUICtrlSetGraphic($gradient, $GUI_GR_MOVE, $x, $y)
        $currentStep = ($x / $width) * $steps
        $stepRed = Hex(($endRed - $startRed) + $currentStep * (($endRed - $startRed) / $steps), 2)
        $stepGreen = Hex(($endGreen - $startGreen) + $currentStep * (($endGreen - $startGreen) / $steps),2)
        $stepBlue = Hex(($endBlue - $startBlue) + $currentStep * (($endBlue - $startBlue) / $steps),2)
        
        $color = "0x" & $stepRed & $stepGreen & $stepBlue
        
        GUICtrlSetGraphic($gradient, $GUI_GR_COLOR, $color)
        GUICtrlSetGraphic($gradient, $GUI_GR_PIXEL, $x, $y)
    Next
;MsgBox(1, "", $color)
Next

GUICtrlSetGraphic($gradient, $GUI_GR_REFRESH)

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
WEnd

For some reason the number of steps aren't working... not a big deal though... you should always want best resolution if unless you don't want a smooth gradient

Regards,Josh

Link to comment
Share on other sites

I'm sure there are other ways. I just use that when I need to, but I think you can do a similar process with GDIPlus.

What other programs do you speak of? They could be using an image...

Regards,Josh

Link to comment
Share on other sites

Or take JFee's cool idea and turn it into a single UDF for creating a gradient label...

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

GUICreate("Gradient", 200, 200)

$label1 = _GuiCtrlCreateGradientLabel("Test text on label",0,0,100,50,0,0,0,255,255,255)
$label2 = _GuiCtrlCreateGradientLabel("Test text on label2",0,50,100,50,0,255,0,255,200,0)
$Label3 = _GuiCtrlCreateGradientLabel("Test text on label2",0,100,100,50,255,0,0,0,0,255)

GUISetState()


While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
WEnd

Func _GuiCtrlCreateGradientLabel($vText,$x,$y,$w,$h,$RStart,$GStart,$BStart,$REnd,$GEnd,$BEnd,$iSteps = 10)
    
    $gradient = GUICtrlCreateGraphic($x, $y, $w, $h)

    For $iX = 1 To $w
        For $iY = 1 To $h
            GUICtrlSetGraphic($gradient, $GUI_GR_MOVE, $iX, $iY)
            $currentStep = ($iX / $w) * $iSteps
            $stepRed = Hex(($REnd - $RStart) + $currentStep * (($REnd - $RStart) / $iSteps), 2)
            $stepGreen = Hex(($GEnd - $GStart) + $currentStep * (($GEnd - $GStart) / $iSteps),2)
            $stepBlue = Hex(($BEnd - $BStart) + $currentStep * (($BEnd - $BStart) / $iSteps),2)
            
            $color = "0x" & $stepRed & $stepGreen & $stepBlue
            
            GUICtrlSetGraphic($gradient, $GUI_GR_COLOR, $color)
            GUICtrlSetGraphic($gradient, $GUI_GR_PIXEL, $iX, $iY)
        Next

    Next

    GUICtrlSetGraphic($gradient, $GUI_GR_REFRESH)

    $hLabel = GuiCtrlCreateLabel($vText,$x,$y,$w,$h)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

EndFunc
Link to comment
Share on other sites

  • 5 years later...

Hi

 using labels you can create a gradient band .

you cant use text , but is a nice touch

$intervalos=6
    $lblwidth=$popupwidth-10
    $clrs=StringSplit("0x990066,0x990077,0x990088,0x990099,0x9900aa,0x9900bb",",")
    $lbltop=0
    $lblhigh=0
    ;--
    For $i=1 to UBound($clrs)-1
        $lbltop=$lblhigh
        $lblhigh=$lblhigh+$popupheight/$intervalos
        GUICtrlCreateLabel("", $lblwidth, $lbltop, 10, $popupheight)
        GUICtrlSetBkColor(-1,$clr[$i])
    next
Link to comment
Share on other sites

  • Moderators

Marcelos,

You do realise this thread was last active 5 1/2 years ago? Please do not necro-post like this in future. :naughty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

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