No announcement yet.

Button Color

  • Filter
  • Time
  • Show
Clear All
new posts

  • Button Color

    This may sound silly, but I've noticed that
    sendmessage hbutton&,%tvm_setbkcolor,0,byval %blue
    does not change the color of a button.
    I've never had reason to change a button color before...

    Any ideas on how to change the color of a button right after creation?

    Jim Seekamp

  • #2


    is a treeview message and not a button message. It is used to set the background color of a treeview control. Definitely wrong message to use with a button control.


    You can not set the color of a button control.

    The button class does send the WM_CTLCOLORBTN message, but it only works for specific window styles which means only when the button class is a checkbox, radio button or frame.

    When the button class uses the styles to be a normal Button, then you can't set its colors. Thats defined by Windows.

    You can though create your own custom looking button controls, but this requires using ownerdraw, which means you have to draw the button yourself (which means you can define its colors).
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"


    • #3
      Or, you can use a static control to emulate a button but this takes quite a bit more code if you emulate a button press, etc. Or, if using ddt, you can use
      imgbutton/imgbuttonx and use small bmps with the text wanted in different colors.
      Client Writeup for the CPA

      Links Page


      • #4
        Thanks for the info!
        I've done ownerdraw buttons before, but that explains why I never colorized a button before!! LOL
        Jim Seekamp


        • #5
          After reading this thread (I've always wanted to be able to color buttons too), jff I thought I'd try to do it with Labels. And you'll never guess what I found. When I tried to use the %Etch styles to emulate a button, it won't print text.

          Kinda cool though. Can make an "invisible" button in that the Callback still detects a click in the area where the faux button (Label) is drawn.

          'PBWIN 9.01 - WinApi 05/2008 - XP Pro SP3
          #Dim All 
          #Compile Exe  
          #Optimize SPEED
          #Debug Display On'off for production code
          #Include "WIN32API.INC"
          #Include "COMDLG32.INC"
          #Include ""
          Global hdlg As Dword                
          %Id_Exit_Btn = 1000
          %Id_Label01 = 1001
          %Id_Show_Result_Btn = 1002
          Macro Common_Locals 'Macro easier than retyping and maintains coding consistency
            Global Dlg_hght, Dlg_Wd As Long 'Global in case want to use in Controls
            Local Stile, Row, col, hght, wd, Longest,ctr, ln, ln1, i As Long
            Local  l, s As String
          End Macro  
          CallBack Function Dialog_Processor              
            Select Case CbMsg     'This is TO determine the message TYPE 
               Case %WM_INITDIALOG'<- Initialiaton when the program loads 
               Case %WM_SYSCOMMAND 'Traps Any Alt key but only F4 closes              
               Case %WM_COMMAND  'This processes command messages
                 Select Case CbCtl
                   Case %Id_Label01 
                        ? "Label01 clicked", , FuncName$
                   Case %Id_Exit_Btn
                     Select Case CbCtlMsg        
                        Case 0
                          Dialog End CbHndl 'Applikation beenden
                     End Select
                 End Select
            End Select
          End Function
          Function PBMain
             Stile = Stile Or %WS_CAPTION
             Stile = Stile Or %WS_SYSMENU
             Stile = Stile Or %WS_THICKFRAME 
             Stile = Stile Or %WM_HELP 
             Stile = Stile Or %WS_Border  'doesn't do anything
            Dlg_hght = 400
            Dlg_Wd = 400
            Dialog New Pixels, hdlg, "Demo", , , Dlg_Wd, Dlg_Hght, Stile To hdlg 'centered
            Row = 10
            col = 10
            Wd = 100
            Hght = 25                              
            Reset stile
            Stile = stile Or %ss_Notify
            Stile = Stile Or %ss_Center
          '      [COLOR=red]To see the different effects, (un)rem the Etch stiles.[/COLOR]
          '  Stile = Stile Or %ss_EtchedHorz [COLOR=red]'only shws a line[/COLOR]
          '  Stile = Stile Or %ss_EtchedVert '[COLOR=red] ""[/COLOR]
            Stile = Stile Or %ss_EtchedFrame  [COLOR=red]'shows nothing?[/COLOR]               
            s$ = " I am a button. "
            Control Add Label, hdlg, %Id_Label01, s$ & "z", Col, Row, Wd, Hght, stile
            Control Set Text hdlg, %Id_Label01, s$ & "Y" [COLOR=red]'overwrite Etching? - Nope[/COLOR]
            Control Set Color hdlg, %Id_Label01, -1, %Yellow
             Col = col + wd + 10 
               Control Add Button, hdlg, %Id_Show_Result_Btn, s$ & "x", col, row, Wd, Hght
             Row = Dlg_hght - Hght - 2 'Just off bottom
               Control Add Button, hdlg, %Id_Exit_Btn, "Abandon Ship", col, row, Wd, Hght
               Control Set Focus hdlg, %Id_Exit_Btn
               Dialog Show Modal hDlg   Call Dialog_Processor
          End Function  'Applikation beenden
          "The man who goes alone can start today;
          but he who travels with another
          must wait till that other is ready."
          Henry David Thoreau (1817-1862)
          Last edited by Gösta H. Lovgren-2; 29 Apr 2009, 08:49 PM. Reason: In living color.
          It's a pretty day. I hope you enjoy it.


          JWAM: (Quit Smoking):
          LDN - A Miracle Drug: