Jump to content
Sign in to follow this  
kurtykurtyboy

GuiFlatButton UDF : Change Colors of Regular Buttons

Recommended Posts

GuiFlatButton is a UDF to easily create regular buttons with different colors for background, foreground, border, hover, focus, etc..

This started as an effort to change the background color of a button and eventually grew into a full UDF.

If you've looked around forums for changing button background colors, you have probably noticed that each proposed workaround has its own set of issues/side-effects. The answers usually circle back to 'use ownerdrawn buttons' and 'not worth it'. Well, now it is possible for anyone to easily create ownerdrawn buttons - totally worth it!

Some issues with other workarounds such as drawing with GDI+ or using a colored label as a 'button':

  • Not 'real' buttons so you lose built-in functionality that windows gives to buttons
  • Messy / inefficient code in the main while loop to check for mouse position
  • Slow to respond to click, paint, etc...
  • Having to deal with GUIRegisterMsg messages
  • Not straight-forward to implement

GuiFlatButton is not a workaround; it is a technique to respond to Windows' built-in owner-drawn button events.
With minimal effort, we can now create true simple colored buttons.

The idea is to create an owner-drawn button using GUICtrlCreateButton then subclass the GUI and controls to handle the button-specific events to paint it however we want.
This UDF magically does all of this for us! No need to worry about event handling or main while loop logic.

 

How to use

It couldn't be any easier! Simply create a new button using the familiar syntax. This creates an ownerdrawn button with default colors.

$mybutton1 = GuiFlatButton_Create("Button 1", 78, 20, 120, 40)

If you want to change the background and text colors:

GuiFlatButton_SetBkColor(-1, 0x5555FF)
GuiFlatButton_SetColor(-1, 0xFFFFFF)

Advanced Usage

Set background/text/border all at once

GuiFlatButton_SetColors(-1, 0x0000FF, 0xFFFFFF, 0x9999FF)

Set ALL colors for ALL button states! (normal, focus, hover, selected)

Local $aColorsEx = [0x0000FF, 0xFFFFFF, -2, 0x4444FF, 0xFFFFFF, 0xAAAAFF, 0x6666FF, 0xFFFFFF, 0xCCCCFF, 0x0000EE, 0xFFFFFF, 0x7777EE]
GuiFlatButton_SetColorsEx(-1, $aColorsEx)

Set default colors to apply to any future buttons

;set colors
GuiFlatButton_SetDefaultColors(0x0000FF, 0xFFFFFF, 0x9999FF)
;create buttons
$mybutton1 = GuiFlatButton_Create("Button 1", 12, 20, 120, 40)
$mybutton2 = GuiFlatButton_Create("Button 2", 143, 20, 120, 40)

Set ALL color defaults

;set colors
Local $aColorsEx = [0x0000FF, 0xFFFFFF, -2, 0x4444FF, 0xFFFFFF, 0xAAAAFF, 0x6666FF, 0xFFFFFF, 0xCCCCFF, 0x0000EE, 0xFFFFFF, 0x7777EE]
GuiFlatButton_SetDefaultColorsEx($aColorsEx)
;create buttons
$mybutton1 = GuiFlatButton_Create("Button 1", 12, 20, 120, 40)
$mybutton2 = GuiFlatButton_Create("Button 2", 143, 20, 120, 40)

 

Available Functions

Spoiler

GuiFlatButton_Create
GuiFlatButton_Read
GuiFlatButton_SetData
GuiFlatButton_SetBkColor  :  Set background color
GuiFlatButton_SetColor  :  Set text color
GuiFlatButton_SetColors  :  Set background, foreground, border colors
GuiFlatButton_SetColorsEx  :  Set background, foreground, border colors for all states
GuiFlatButton_SetDefaultColors  :  Set default colors for all future buttons
GuiFlatButton_SetDefaultColorsEx  :  Set default extended colors for all future buttons
GuiFlatButton_GetState
GuiFlatButton_SetState
GuiFlatButton_Delete
** GuiFlatButton_DrawButton  :  For those brave souls who would like to do more fancy coloring, the possibilities are endless!

 

Simple Example
image.png.7d6038f62647449282e67cdc1fa4a8b2.png

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include "GuiFlatButton.au3"

Example()

;GUI with one button
Func Example()

    Local $hGUI, $mybutton1

    $hGUI = GUICreate("GuiFlatButton Ex0", 275, 120)
    GUISetBkColor(0x333333)

    $idLabel = GUICtrlCreateLabel("Click the button", 10, 100, 150, 30)
    GUICtrlSetColor(-1, 0xFFFFFF)

    ;create new button then set the background and foreground colors
    $mybutton1 = GuiFlatButton_Create("Button 1", 78, 20, 120, 40)
    GuiFlatButton_SetBkColor(-1, 0x5555FF)
    GuiFlatButton_SetColor(-1, 0xFFFFFF)

    GUISetState(@SW_SHOW, $hGUI)

    Local $i = 0
    Local $iMsg
    While 1
        $iMsg = GUIGetMsg()

        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $mybutton1
                $i += 1
                GUICtrlSetData($idLabel, $i)
                ConsoleWrite($i & @CRLF)
        EndSwitch

        Sleep(10)
    WEnd

    GUIDelete()
EndFunc   ;==>Example


Menu/Toolbar Example
image.png.a039ca4a859fa1f2691d743ca0f0cf57.png

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include "GuiFlatButton.au3"

Example()

;Example GUI with toolbar
Func Example()

    Local $hGUI, $idLabel, $aButtons, $iTbSize

    $hGUI = GUICreate("GuiFlatButton Ex2", 300, 200)
    GUISetBkColor(0x444444)

    $idLabel = GUICtrlCreateLabel("Click a button", 10, 180, 150, 30)
    GUICtrlSetColor(-1, 0xFFFFFF)

    $aButtons = createToolbar()
    $iTbSize = UBound($aButtons)

    GUISetState(@SW_SHOW, $hGUI)

    Local $i = 0
    Local $iMsg
    While 1
        $iMsg = GUIGetMsg()

        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $aButtons[0] To $aButtons[$iTbSize - 1]
                ConsoleWrite("1")
                GUICtrlSetData($idLabel, GuiFlatButton_Read($iMsg))

        EndSwitch

        Sleep(10)
    WEnd

    GUIDelete()
EndFunc   ;==>Example


Func createToolbar()
    Local $aButtons[6]
    Local $bkColor = 0x777777
    Local $textColor = 0xFFFFFF
    Local $borderColor = 0x999999
    Local $aBtnClrs[12] = [0x777777, 0xFFFFFF, $GUI_BKCOLOR_TRANSPARENT, 0x888888, 0xFFFFFF, $GUI_BKCOLOR_TRANSPARENT, 0x999999, 0xFFFFFF, $GUI_BKCOLOR_TRANSPARENT, 0x666666, 0xFFFFFF, $GUI_BKCOLOR_TRANSPARENT]

    For $i = 0 To UBound($aButtons) - 1
        $aButtons[$i] = GuiFlatButton_Create("B" & $i, $i * 50, 0, 50, 17)
        GuiFlatButton_SetColorsEx($aButtons[$i], $aBtnClrs)
    Next

    Return $aButtons
EndFunc   ;==>createToolbar


I'm sure there are some use-cases I've forgotten, so feedback is welcome!


Update 2019-02-09
Added 2 new functions to set the button colors globally for all future buttons.
GuiFlatButton_SetDefaultColors 
GuiFlatButton_SetDefaultColorsEx

Download the UDF and several examples: GuiFlatButton.zip


Credits to:
Melba23 (UDF template)
LarsJ (general subclassing code)
4ggr35510n (TrackMouseEvent example)
binhnx (disable dragging with $WS_EX_CONTROLPARENT)
GUIRegisterMsg in AutoIt Help (owner-draw button example)

Edited by kurtykurtyboy
Fixed color 0x000000

Share this post


Link to post
Share on other sites

Looks great! Should be part of the default button creation. :P


Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites

Impressive!   Thanks for this.

The only "improvement" that comes to mind is to add a two-button example that makes clear (by direct comparison) the capabilities of GuiFlatButton_SetColorsEx ... and maybe include a couple of commented-out alternative lines that show some of the more effective uses you've tested.

 

Share this post


Link to post
Share on other sites

In working with the UDF, I've settled on a default (my default) look for buttons that will work on many screens. As a test, I've edited the Button_Create function as follows:

;fill the array with the default values
    $aGuiFlatButton[$i][0] = $i
    $aGuiFlatButton[$i][4] = False
    $aGuiFlatButton[$i][5] = 13357056               ; bk color
    $aGuiFlatButton[$i][6] = 0x00000000             ; color of text
    $aGuiFlatButton[$i][17] = $sText
    For $j = 7 To 16
        $aGuiFlatButton[$i][$j] = -1
    Next

Should I be looking at a different method for this? Have you thought of adding some sort of SetTheme function that can predefine several of the settings prior to any Button_Create calls?

5c5bb8c5e31e1_DefaultButton.PNG.10d6eb4d7966c633e51c86473886a6ce.PNG

Edited by qwert

Share this post


Link to post
Share on other sites

@qwert Your wish is my command!

Added functions to set default colors:
GuiFlatButton_SetDefaultColors
GuiFlatButton_SetDefaultColorsEx

See first post for how to use. A new example was added to the zip as well.

Share this post


Link to post
Share on other sites

Thanks for adding those.  I haven't had time to delve into the code, so can you spot the reason these two buttons come up with different colors?

In my simple way of thinking, the two create calls would use the same colors.

5c60b3b00b30c_Defaultcolortest.thumb.PNG.4a5da39e5ff2a19926665599390d7000.PNG

Share this post


Link to post
Share on other sites

Well, yes, I can see that now.

While you're still looking at this, can you tell me where the white border is coming from (on a hover), when I've set all the border colors to 0x000000?

5c619ca18a409_BorderDifference.PNG.e952a02078ff5abfcd8f430428482f5d.PNG

Share this post


Link to post
Share on other sites
Quote

I hope you don't mind

Not at all.  Thanks for keeping at it.  This is going to be a very useful UDF!  Styles and such on websites have led users to expect this sort of thing ... which Autoit scripts are now going to be able to deliver.

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  

  • Similar Content

    • By MFrancisca
      Hello! 
      I've been checking the logging UDFs in the wiki, mostly Log4a and Loga and I wanted to ask which one would you recommend for a script that will be executed remotely through PSExec. My main question is where the logs are located in that situation and if I can change that location to a custom one., because I need to retrieve them at the end of execution.
      So in a rough description the process is
      Open PSExec
      Send compiled AutoIT script to remote machine
      Execute script
      Copy logs from the remote to the local machine.
       
      Any opinions? 
    • By caramen
      I watched _OL_ItemSend&_OL_ItemFind&_OL_ItemCreate in OutlookEx UDF but hmmm
       
      Since it use the test environnement i cant get the orders of the mail creation
       
      Can someone make me win some time of reading all exemples script and lead me how to do one ?
      That whould be lovely.
      Gnight
    • By Tersion
      Here the this wiki page with list of available UDFs for data compression. For my tasks I only need ZIP support, so I started looking at pure AutoIt UDFs without any 3rd party dlls. And found out that most of available realizations uses standard ("native method") Windows dll - "zipfldr.dll". So for now I chose ZIP UDF by wraithdu. I've tested it on Windows 7 (x64) and it seem works fine. But here the comment from another topic where user says that Windows 10 discontinued support of "zipfldr.dll". Now I confused. I don't have around any Windows 10 machine to tested it. So maybe someone could confirm or deny that? Or maybe would better to switch to UDF with 7zip dll?
      I need an advice...
    • By TheSaint
      Here is the bare bones of a UDF I have started work on.
      Mostly just a proof of concept at this stage, and still need to add some functions and dress the UDF up a bit ... to look like a UDF ... though it has my own distinct styling, especially as I have never really developed a UDF before now .... used plenty and modified plenty though. I've even invented my own UDF variable naming convention, which I am sure some of you will be aghast at. I work with what feels best for me, but others are free to adapt if they wish.
      The idea is to emulate the simplicity of INI files, but gain the benefits of SQL.
      Two scripts are provided.
      (1) The UDF, a work in progress - SimpleSQL_UDF.au3
      (2) An example or testing script - UDF_Test.au3
      Another first for me, is creating a 2D array from scratch, never done that before, that I can recall ... never had a need, and even for 1 dimension arrays, for a long time now, I have just used _StringSplit to create them. So I needed a bit of a refresher course, which my good buddy @TheDcoder assisted me with ... not without some angst I might add. LOL
      SimpleSQL_UDF.zip  (12 downloads previously)
      (I have now completed all the functions I intended to. My next update will be a big improvement, bringing things more inline with my latest INItoSQL DB program changes.)
      Program requires the sqlite3.dll, not included, but easily enough obtained.
      Hopefully the usage is self-evident ... just change the Job number variable in the UDF_Test.au3 file to check the existing functions out.
      Enjoy!
      P.S. This is also related to a new program I have just finished and uploaded - INItoSQL DB
    • By xtcislove
      Hello,
      as a start in Autoit i tried something i was missing since im using Autoit. 

      I build a custom MessageBox which has a large amount of custom options and which scales its size on the parameters you set. 

      Aviable Settings:
      -Title
      -Unlimited Buttons
      -Text Color (Buttons, Text)

      -Background Color (Msgbox, Buttons, Label) 

      -Button Timeout
      -Autoclose Timeout
      -Icon (Default, No Icon, Custom)

      -Label/ Button Style. 
      -Transparency

      I tried to keep this as close as i could to a Msgbox i was used too on my batch times.

      After i was ready i realised, @Melba23 probably build a way better msgbox which would have suit my needs enterly, anyway thanks to @Melba23 because i use his Stringsize UDF. 

       
       
      local $Message = _sMsgBox("Test", 6, "Continue?") if @extended <> -1 Then MsgBox(0, @extended, $Message&" Button pressed")  
      ScalingMessageBox.au3
×
×
  • Create New...