Page 1 of 1

'Borderless' buttons on dark backgrounds

Posted: 21 Jul 2014, 09:09
by just me
Playing around with the WM_CTLCOLOR... messages I stumbled over an interesting feature. At least on Win 7 and 8 all push buttons seem to send the WM_CTLCOLORBTN message to there parent window before they are redrawn. Most of what Microsoft states on MSDN seems to be pure nonsense. But, after some testing it seems, that the brush returned by the parent will be actually used to draw the button's background. This seems to solve the problem of the somewhat ugly appearance of themed push buttons on dark coloured backgrounds.

So, how to do it?
  1. If you use e.g. Gui, Color, 808080 or a picture for the Gui background:
  2. Create a variable (e.g. CtrlColorBtnsBrush) containing a brush using a proper color:

    Code: Select all

    CtlColorBtnsBrush := DllCall("Gdi32.dll\CreateSolidBrush", "UInt", 0x808080, "UPtr")
    Note: The color has to be specified in BGR (reversed order).
  3. Create a function (e.g. CtrlColorBtns()) which returnes right this brush:

    Code: Select all

    CtlColorBtns() {
       Global CtlColorBtnsBrush
       Return CtlColorBtnsBrush
    }
  4. Register the function as handler for WM_CTLCOLORBTN messages:

    Code: Select all

    OnMessage(0x0135, "CtlColorBtns") ; WM_CTLCOLORBTN = 0x0135
  5. Create the Gui and the buttons and redraw the Gui once after the first Gui, Show, ... with WinSet, Redraw, ....
  6. Done!
Because OnMessage() functions aren't Gui related as yet, multiple Guis will need some adjustment, but that should be feasible.

There's one disadvantage you will notice, the buttons don't seem to be proper aligned with other controls any more. As a workaround, you can adjust the position and size.

Code: Select all

#NoEnv
; MsgBox, PSPad Workaround!
CtlColorBtnsBrush := DllCall("Gdi32.dll\CreateSolidBrush", "UInt", 0x808080, "UPtr") ; BGR!!!
OnMessage(0x0135, "CtlColorBtns") ; WM_CTLCOLORBTN = 0x0135
Gui, +LastFound ; sets the last found window
Gui, Margin, 100, 50
Gui, Color, 808080
Gui, Add, Radio, w100 vRB1, Radio1
Gui, Add, Radio, xm y+10 w100 vRB2, Radio2
Gui, Add, CheckBox, w100 vCB, CheckBox
Gui, Add, Edit, xm w100 h30
Gui, Add, Button, xm y+2 w100 vPB, Button
Gui, Show, , Test
WinSet, Redraw ; uses th last found window
Return

GuiClose:
GuiEscape:
ExitApp

CtlColorBtns() {
   Global CtlColorBtnsBrush
   Return CtlColorBtnsBrush
}
*Tested on Win 7 and 8*

Re: 'Borderless' buttons on dark backgrounds

Posted: 22 Jul 2014, 12:00
by fischgeek
Sweet! Thank you!