# Button Colour and Style

## Recommended Posts

Hi All,

I'm sure this is a simple one to answer and I have searched and experimented before I asked (well enough hopefully!)

I'm trying to set a button background to be white, the font colour red, centred vertically and horizontally and multi-line. Can it be done?

Here is my example code:

#include <Constants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ColorConstantS.au3>
#include <FontConstants.au3>
#include "ColorConstants.au3"

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("GUI", 250, 300) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$Button = GUICtrlCreateButton("Button" & @CRLF & "One", 160, 185, 85, 110, BitOR($BS_MULTILINE, $BS_CENTER,$BS_VCENTER, $WS_EX_WINDOWEDGE)) GUICtrlSetBkColor(-1,$COLOR_White)
GUICtrlSetFont($Button, 14, 800, 0, "MV Boli", 5) GUICtrlSetColor($Button, 0xFF0000)
GUICtrlSetOnEvent($Button, "_Exit") GUISetState() While 1 Sleep(100) WEnd Func _Exit() Exit EndFunc ;==>_Exit With everything I have tried, this is the closest I get, button white, font red, correct font, but no centring and sometimes no multi-line (depending on button text and font size). I saw I might have to add the default button forced style back in so I did that too$WS_EX_WindowEdge.

Thanks for any help and apologies if I've missed an obvious answer to this elsewhere.

##### Share on other sites

This works for me.

#include <Constants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ColorConstantS.au3>
#include <FontConstants.au3>
#include "ColorConstants.au3"

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("GUI", 250, 300) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$Button = GUICtrlCreateButton("Button" & @CRLF & "One", 160, 185, 85, 110, BitOR($SS_CENTER, $SS_CENTERIMAGE)) ;$BS_MULTILINE, $BS_CENTER,$BS_VCENTER, $WS_EX_WINDOWEDGE)) GUICtrlSetBkColor(-1, 0xFFFFFF) GUICtrlSetFont($Button, 14, 800, 0, "MV Boli", 5)
GUICtrlSetColor($Button, 0xFF0000) GUICtrlSetOnEvent($Button, "_Exit")

GUISetState()

While 1
Sleep(100)
WEnd

Func _Exit()
Exit
EndFunc   ;==>_Exit

MEASURE TWICE - CUT ONCE

##### Share on other sites

Thanks Reb, tried your code and it centres it but no multi-line, and if I put multi-line on the end it centres it at the top instead and it ignores the @CRLF in the text. Did I do something wrong?

##### Share on other sites

I guess you could always fake a button with a label.

include <Constants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ColorConstantS.au3>
#include <FontConstants.au3>
#include "ColorConstants.au3"

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("GUI", 250, 300) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$bBm = "Button" & @CRLF & "One"$Button = GUICtrlCreateLabel(@CRLF & $bBm, 160, 185, 85, 110, BitOR($SS_CENTER, $BS_VCENTER),$WS_EX_CLIENTEDGE) ;, $SS_CENTERIMAGE)) ;$BS_MULTILINE, $BS_CENTER,$BS_VCENTER, $WS_EX_WINDOWEDGE)) GUICtrlSetBkColor(-1, 0xFFFFFF) GUICtrlSetFont($Button, 14, 800, 0, "MV Boli", 5)
GUICtrlSetColor($Button, 0xFF0000) GUICtrlSetOnEvent($Button, "_Exit")

GUISetState()

While 1
Sleep(100)
WEnd

Func _Exit()
Exit
EndFunc   ;==>_Exit

_WinAPI_DrawText

Edited by reb
Removed unused #include

MEASURE TWICE - CUT ONCE

##### Share on other sites

reb, you're using $ss_* styles in a button control,$ss_* styles are for static controls, labels/icons/images. There is no button control constant that has the same value as $ss_center or$ss_centerimage (although there is a constant with the same value as _centerimage, it's used in a checkbox control), so there's no telling what either of those settings might produce as a result if anything.

I've tried every combination of button style setting and I have not been able to produce a button with multiline and vertically centered text. The best I could come up with was to add a @CRLF in front of the button text to get it to add another line above the text.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

I revised my post just above yours using label constants.  I see your point.

Thank you.

#include <Constants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ColorConstantS.au3>
#include <FontConstants.au3>
#include "ColorConstants.au3"

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("GUI", 250, 300) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$bBm = "Button" & @CRLF & "One"$Button = GUICtrlCreateLabel(@CRLF & $bBm, 160, 185, 85, 110,$SS_CENTER,$WS_EX_CLIENTEDGE) ;,$SS_CENTERIMAGE)) ; $BS_MULTILINE,$BS_CENTER, $BS_VCENTER,$WS_EX_WINDOWEDGE))
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetFont($Button, 14, 800, 0, "MV Boli", 5) GUICtrlSetColor($Button, 0xFF0000)
GUICtrlSetOnEvent($Button, "_Exit") GUISetState() While 1 Sleep(100) WEnd Func _Exit() Exit EndFunc ;==>_Exit . MEASURE TWICE - CUT ONCE #### Share this post ##### Link to post ##### Share on other sites Thank you both for your answers, I didn't even think of adding a @CRLF before the text! Unless anyone else can tell me (us) where we're going wrong I guess it's just one of those things I'll have to work around. Anyone else? #### 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 • ### Recently Browsing 0 members No registered users viewing this page. • ### Similar Content • By Zobu Hey Guys, I want to add a new checkbox with its own variable every time the add button is clicked. The added checkboxes should remain when I close the window or exit the script and when I reopen I should be able to add new checkboxes aswell. here is what I have so far.. #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <EditConstants.au3> #include <MsgBoxConstants.au3> #include <GuiButton.au3>$test = GUICreate("adding test", 475, 345, 500, 175) $Check1 = GUICtrlCreateCheckbox("Checkbox 1", 15, 25, 300, 25)$Button = GUICtrlCreateButton("Add", 365, 25, 90, 20) $Check2 = GUICtrlCreateCheckbox("Checkbox 2", 15, 50, 300, 25)$Check3 = GUICtrlCreateCheckbox("Checkbox 3", 15, 75, 300, 25) GUICtrlSetState($Check2,$GUI_HIDE) GUICtrlSetState($Check3,$GUI_HIDE) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ; Exit Case$Button GUICtrlSetPos($Check2, 15, 50, 300, 25) GUICtrlSetState($Check2, $GUI_SHOW) Case$Button GUICtrlSetPos($Check3, 15, 75, 300, 25) GUICtrlSetState($Check3, $GUI_SHOW) EndSwitch WEnd • Hello Everyone, Hoping someone can help me, I am totally stuck on this problem, I cannot reach the textarea and the button in a maps page that I get by clicking on a button in a previus page, the only way I can interact with this is using tab and mouseclick but this is a danger method as you know. I get the handle by using:$Posizione = _IEAttach("Ricerca Indirizzo su mappa")

then I tried a lot of things but nothing works, can someone give me a hint?
--------------------
WinWait("Ricerca Indirizzo su mappa")
sleep(1000)
$Posizione = _IEAttach("Ricerca Indirizzo su mappa") WinSetState ("Ricerca Indirizzo su mappa", "", @SW_MAXIMIZE ) WinActivate("Ricerca Indirizzo su mappa") sleep(500) ;~$oForm = _IEFormGetObjByName($Posizione, "form1");Punta il Form$oText = _IEFormElementGetObjByName($Posizione,"indirizzo")$oText = _IEFormElementGetObjByName($Posizione,"writeAddress") ;~$LenteTestoMess = _IEFormElementGetValue($oText);Inserisce il PrimoRepertorio nella variabile ;~ _IELinkClickByText($Posizione, $oForm);Clicca su Aggiorna ed invia il form _IEFormElementSetValue ($oText,"via Ravenna 12" );Completamento campo testo
----------------------------------------------------
Mappa.odt Ricerca Indirizzo su mappa.htm

• Good morning,

I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
Can someone tell me what I do wrong ?
Here is my code:
$btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
I tried to follow the help, made a lot of different parameter combinations (here it's $BS_BOTTOM, but I tried $BS_VCENTER and BS_TOP), still get the same result : vertical alignement is a no go for me. Can someone help ? TIA Gilles • By Pike Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG! To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1" Case 1 LocalsRead = IniRead("config.ini", "Config", "One", "Default")
SoundPlay($sRead, @HotKeyPressed) <------------------------------- Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.

Much Respect,
Pike

Func _HandleButton() $iClickCount += 1$iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or$iTimer = 0 Then Return Switch $iClickCount Case 1 Local$sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)",$FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One",$sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch$iClickCount = 0 AdlibUnRegister() EndFunc

• By misioooo
Hi!

I am writing some GUI and i need to use images as buttons, but with text on them (text is from INI file).
Part with button looks like this:
$ankieta1 = GUICtrlCreateButton($txtAnk1, 100,350,765,164) GUICtrlSetFont(-1, 22, 800, -1, "Arial") ;GUICtrlSetColor(-1,0xffffff) $hImageBtn_1 = _GUIImageList_Create(765, 164) _GUIImageList_AddBitmap($hImageBtn_1, "c:\path\to\ankieta_clean.bmp") _GUICtrlButton_SetImageList($ankieta1,$hImageBtn_1, 4) GUICtrlSetOnEvent(-1, "On_Ankieta1") The problem - if i uncomment GUICtrlSetColor, color of text changes, but button is just plain gray (no image). How i can change text color not touching image on a button here?
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...