Jump to content

Gradient buttons


torels
 Share

Recommended Posts

Hi there

I just wrote this small UDF to create buttons with a gradient on the background :mellow:

what do you think about it ?

Comments are welcome...

Here is the code:

#include <GDIPlus.au3>
#include <WindowsConstants.au3>
Dim $bitmap, $GX, $GY, $GH, $GW

dim $first[3] = [0xCC, 0xCC, 0xCC]
dim $second[3] = [0xFB, 0xFB, 0xFB]

$GUI = GUICreate("Gradient Button", 300,70)
GUISetState()
_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($GUI)

$button = GUICtrlCreateButton("Click Me!",50,20,200,30)
_GradientButton($button, $first, $second,1)

while 1
    Switch GUIGetMsg()
        case -3
            _GDIPlus_Shutdown()
            Exit
        Case $button
            MsgBox(0,"","Hello world! I am a gradient button")
    EndSwitch
WEnd

Func _GradientButton($control, $col1, $col2, $border = 1)
    $pos = ControlGetPos($GUI, "", GUICtrlGetHandle($control))
    _GuiCtrlMakeTrans($control,1)
    GradientFill(ControlGetText($GUI,"",GUICtrlGetHandle($control)), $pos[0], $pos[1], $pos[2], $pos[3], $col1, $col2, $border)
EndFunc



Func GradientFill($sText, $Xpos, $Ypos, $width, $height, $left_color, $right_color, $border = 1)
    global $GW = $width
    global $GH = $height
    global $GX = $Xpos
    global $GY = $Ypos
    $bitmap=_GDIPlus_BitmapCreateFromGraphics($width,$height,$Hgraphics)
    $backbuffer=_GDIPlus_ImageGetGraphicsContext($bitmap)
    ;_GDIPlus_GraphicsClear($backbuffer)
    $color0=($left_color[0]-$right_color[0])/$height
    $color1=($left_color[1]-$right_color[1])/$height
    $color2=($left_color[2]-$right_color[2])/$height
    For $Y=0 to $height
        $red=$left_color[0]-floor($Y*$color0)
        $green=$left_color[1]-floor($Y*$color1)
        $blue=$left_color[2]-floor($Y*$color2)
        $col = Hex($blue,2) & Hex($green,2) & Hex($red,2)
        $col = "0xFF" & $col
        $hPen = _GDIPlus_PenCreate($col)
        _GDIPlus_GraphicsDrawLine($backbuffer, 0, $Y, $width, $Y, $hPen)
        _GDIPlus_PenDispose($hPen)
    Next
    $len = StringLen($sText)*8/2
    _GDIPlus_GraphicsDrawString($backbuffer, $sText, $width/2-$len, $height/2-8)
    if $border = 1 then _GDIPlus_GraphicsDrawRect($backbuffer, 0,0,$width-1,$height-1)
    _GDIPlus_GraphicsDrawImageRect($hGraphics,$bitmap,$Xpos,$Ypos,$width,$height)
    GUIRegisterMsg($WM_PAINT, "_ReDraw")
    ;global $bitmap=_GDIPlus_BitmapCreateFromGraphics($width,$height,$hGraphics)
EndFunc

Func _ReDraw()
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $bitmap, $GX, $GY, $GW, $GH)
EndFunc

Func _GuiCtrlMakeTrans($iCtrlID, $iTrans=255)
    Local $pHwnd, $nHwnd, $aPos, $a
    $hWnd = GUICtrlGetHandle($iCtrlID);Get the control handle
    If $hWnd = 0 then Return SetError(1,1,0) 
    $pHwnd = DllCall("User32.dll", "hwnd", "GetParent", "hwnd", $hWnd);Get the parent Gui Handle
    If $pHwnd[0] = 0 then Return SetError(1,2,0)
    $aPos = ControlGetPos($pHwnd[0],"",$hWnd);Get the current pos of the control
    If @error then Return SetError(1,3,0)
    $nHwnd = GUICreate("", $aPos[2], $aPos[3], $aPos[0], $aPos[1], 0x80000000, 0x00080000 + 0x00000040, $pHwnd[0]);greate a gui in the position of the control
    If $nHwnd = 0 then Return SetError(1,4,0)
    $a = DllCall("User32.dll", "hwnd", "SetParent", "hwnd", $hWnd, "hwnd", $nHwnd);change the parent of the control to the new gui
    If $a[0] = 0 then Return SetError(1,5,0)
    If NOT ControlMove($nHwnd,'',$hWnd,0,0) then Return SetError(1,6,-1);Move the control to 0,0 of the newly created child gui
    GUISetState(@SW_Show,$nHwnd);show the new child gui
    WinSetTrans($nHwnd,"",$iTrans);set the transparency
    If @error then Return SetError(1,7,0)
    GUISwitch($pHwnd[0]);switch back to the parent Gui
    Return $nHwnd;Return the handle for the new Child gui
EndFunc

_GuiCtrlMakeTrans isn't a function I wrote... and gradientfill has been "adapted" so it can be used with gdi+ instead than graphics :(

Edit: Excuse me for the messy code...

Edited by torels

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

Link to comment
Share on other sites

Nice one!

However there is a memory leak in your script, every time you create a button with the script a new bitmap and a new graphics object is created which can never be disposed.

Otherwise it's good, too few people use GDI+ in their GUI's.

:mellow:

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Nice work!!

I actually looked into this when I was working with my XSkin.au3 stuff. What I don't like is when the mouse is over the button you can't see any change and even more, I really don't like when this type of button is clicked, there is no apparent noticeable action on the button... it just sits there

That's why I went for this below. 1st the buttons can be designed in any way, The is a hover action is noticed and the click action is noticed.

Same code essentially

#include <WindowsConstants.au3>
#include <EzSkin.au3>

$GUI = GUICreate("Gradient Button", 300, 70)
$button = EzSkinButton("Click Me!", 50, 20, 200, 30)
GUISetState()

While 1
    EzSkinOver()
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $button
            MsgBox(0, "", "Hello world! I am a gradient button")
    EndSwitch
WEnd

EzSkin.au3 located here

http://www.autoitscript.com/forum/index.ph...st&p=307380

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Ok

I will work on it :mellow:

And see if I can resolve the memory leak problem...

:(

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

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...