Page 1 of 2

ButtonGadget modify color by CallBack [Resolved]

Posted: Fri Sep 02, 2011 9:22 am
by Kwai chang caine
Hello at all

Is it possible to modify the FontColor and BackGroundColor with Callback of a button ???

I have try the same API than TextBox but obviously that not works :(

Code: Select all

Procedure WinProc(hWnd,Msg,wParam,lParam)
      
  If Msg = #WM_CTLCOLORSTATIC
   
   If IsGadget(#Button) 
    If lParam = GadgetID(#Button)
     SetBkMode_(wParam, RGB(255,255,255)) 
     SetTextColor_(wParam, RGB(66, 103, 46)) 
     ProcedureReturn TextGadgetBackground 
    EndIf 
   EndIf
   
  EndIf 
   
EndProcedure  
Have a good day

Re: ButtonGadget modify color by CallBack

Posted: Fri Sep 02, 2011 9:49 am
by RASHAD
Hi KCC

Code: Select all


Procedure WCB(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  Select Message
   Case #WM_CTLCOLORSTATIC
      Select lparam
        Case GadgetID(1)
           ;SetBkMode_(wparam,#TRANSPARENT)
           SetBkColor_(wparam,$88FCFD)
           SetTextColor_(wParam,$0002FF)
           ProcedureReturn GetStockObject_(#NULL_BRUSH	)
        EndSelect
  EndSelect
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,0,0,250,250,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  TextGadget(1,10,10,200,25," TextGadget Test ",0)
  LoadFont(0, "Arial", 16)
  SetGadgetFont(1, FontID(0))
  SetWindowColor(0,$F5F5F5)
  SetWindowCallback(@WCB(),0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: ButtonGadget modify color by CallBack

Posted: Fri Sep 02, 2011 9:53 am
by netmaestro
KCC, Your code has mistakes. For one, you are returning TextGadgetBackground and this must be a valid hBrush. It's not anything. Also, SetBkMode_() requires a device context and an integer, valid values of which are #TRANSPARENT and #OPAQUE. A COLORREF won't do. Also, if you're using a window callback, if you don't find the message you are handling, you must return #PB_ProcessPureBasicEvents. Also, a ButtonGadget is not a static control and so #WM_CTRLCOLORSTATIC doesn't apply. It would be #WM_CTRLCOLORBTN.

Now that all that is out of the way, Windows is jealous of how buttons are colored and it so will ignore a hBrush you return for coloring them. If you want to color buttons, you should look into the joys and wonders of #BS_OWNERDRAW. Or, if that doesn't appeal to you, you can make an ImageButton and create an image your buttonsize, use a Box() command to draw the background and a DrawText() command to draw the text any color you want. Set this image as parameter to the ImageButton and you're done.

Re: ButtonGadget modify color by CallBack

Posted: Fri Sep 02, 2011 9:55 am
by netmaestro
@RASHAD: Did you notice this:

Code: Select all

If lParam = GadgetID(#Button)
Makes all the difference.

Re: ButtonGadget modify color by CallBack

Posted: Fri Sep 02, 2011 10:54 am
by Kwai chang caine
Thanks at you two 8)

NETMAESTRO as right 8)
RASHAD it's a button i want to modify color and your code not works for a button :wink:
Also, a ButtonGadget is not a static control and so #WM_CTRLCOLORSTATIC doesn't apply. It would be #WM_CTRLCOLORBTN.
I have try the #WM_CTRLCOLORBTN constant and PB don't know her :(
If you want to color buttons, you should look into the joys and wonders of #BS_OWNERDRAW. Or, if that doesn't appeal to you, you can make an ImageButton and create an image your buttonsize, use a Box() command to draw the background and a DrawText() command to draw the text any color you want. Set this image as parameter to the ImageButton and you're done.
Yes i have see several code with this method :D
But like i have already a callback i have thinking it's more easier to use her for also coloring my button.

If you say it's not a good way, KCC don't do it :oops:
Windows is jealous of how buttons are colored
KCC don't want to unnerve bil gates, even if it is a retirement :mrgreen:

Image

So KCC create a box and use it like button

Again thanks at you two, and have a very good day 8)

Re: ButtonGadget modify color by CallBack

Posted: Fri Sep 02, 2011 11:48 am
by RASHAD
Sorry KCC
Very Sorry NM (Speed Reader) :)

Code: Select all

Global BBrush,TBrush

BBrush = CreateSolidBrush_($FF3028)
TBrush = CreateSolidBrush_($D9FEFE)


Procedure WCB(hWnd,uMsg,wParam,lParam)
    Select uMsg
        Case #WM_DRAWITEM
       *cb.DRAWITEMSTRUCT = lParam
       
       SetBkMode_(*cb\hDC,#TRANSPARENT)
       
       If *cb\itemState & #ODS_SELECTED
          SelectObject_(*cb\hDC,BBrush)         
          SetTextColor_(*cb\hDC,$D9FEFE)
       Else
          SelectObject_(*cb\hDC,TBrush)         
          SetTextColor_(*cb\hDC,$FF3028)     
       EndIf
       
       RoundRect_(*cb\hDC,0,0,*cb\rcItem\right,*cb\rcItem\bottom,10,10)
       RoundRect_(*cb\hDC,1,1,*cb\rcItem\right-1,*cb\rcItem\bottom-1,10,10)
       DrawText_(*cb\hDC,GetGadgetText(0),-1,*cb\rcItem,#DT_CENTER | #DT_VCENTER | #DT_SINGLELINE)
       
       ProcedureReturn 0
    EndSelect
     
    ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0,0,0,400,200,"TEST",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0,10,165,100,25,"COLOR BUTTON",#BS_OWNERDRAW)

SetClassLongPtr_(GadgetID(0),#GCL_STYLE,GetClassLongPtr_(GadgetID(0),#GCL_STYLE) | #CS_DBLCLKS)

SetWindowCallback(@WCB())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend


Re: ButtonGadget modify color by CallBack

Posted: Fri Sep 02, 2011 1:02 pm
by Kwai chang caine
Image
Splendid !!!! :shock:
Very Sorry NM (Speed Reader)
You are obviously all excused 8)

Thanks Thanks Thanks again a lot 8)

Just another thing....i have searched what is the function of this line, and not found :oops:
Because if i commented her, that also works :shock:

Code: Select all

SetClassLongPtr_(GadgetID(0),#GCL_STYLE,GetClassLongPtr_(GadgetID(0),#GCL_STYLE) | #CS_DBLCLKS)

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sat Sep 03, 2011 5:23 am
by electrochrisso
I like it RASHAD. 8)
I love the Egyptian KCC, reminds me of that song, you know: Walks.... :lol:

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sat Sep 03, 2011 12:49 pm
by Fluid Byte
I like ...
ImageImageImageImage

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sat Sep 03, 2011 2:06 pm
by RASHAD
@FB
Very very glad by your post :P

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sun Sep 04, 2011 6:40 pm
by Kwai chang caine
@electrochrisso and Fluid Byte
Thanks :D

@Rashad
It's the minimum for give you a little present 8)
You give always so much for me, so i have the idea to put this nice egyptian for you :D
Thanks again RASHAD for your big help and also your great knowledge 8)

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sun Jun 25, 2023 5:58 am
by Blue
Hello Rashad.
and thank you for a wonderful piece of code.

This may be very old code, but it is still very up-to-date and useful.
It is exactly what I was hoping to find. It works flawlessly (with PB6.03, under Windows 11 X64 ) , no matter what fonts get thrown at it.

I just replaced GetGadgetText(0) with GetGadgetText(wParam) so it can work with any button that wants to be coloured.

I now wish you'd find the time to explain why this line

Code: Select all

SetClassLongPtr_(GadgetID(2),#GCL_STYLE,GetClassLongPtr_(GadgetID(2),#GCL_STYLE) | #CS_DBLCLKS)
is required, since commenting it out seems to change nothing at all...

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sun Jun 25, 2023 7:41 am
by BarryG
Blue wrote: Sun Jun 25, 2023 5:58 amI now wish you'd find the time to explain why this line

Code: Select all

SetClassLongPtr_(GadgetID(2),#GCL_STYLE,GetClassLongPtr_(GadgetID(2),#GCL_STYLE) | #CS_DBLCLKS)
is required, since commenting it out seems to change nothing at all...
Apparently #CS_DBLCLKS is needed for double-click support -> https://learn.microsoft.com/en-us/windo ... ble-clicks

But I don't know why double-clicking works if you comment that line out. Maybe because the window itself is getting the double-clicks?

Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sun Jun 25, 2023 9:32 am
by mk-soft
Set Window Long is probably not necessary.
It works with wParm because PB also sets its PB ID internally at CrlID.

But I would check the PB ID and the gadget type.

Code: Select all

;-TOP

; Base Rashad : https://www.purebasic.fr/english/viewtopic.php?p=360438#p360438

Global BBrush,TBrush

BBrush = CreateSolidBrush_($FF3028)
TBrush = CreateSolidBrush_($D9FEFE)


Procedure DrawButtonCB(wParam, *lParam.DRAWITEMSTRUCT)
  SetBkMode_(*lParam\hDC, #TRANSPARENT)
  If *lParam\itemState & #ODS_SELECTED
    SelectObject_(*lParam\hDC, BBrush)         
    SetTextColor_(*lParam\hDC, $D9FEFE)
  Else
    SelectObject_(*lParam\hDC, TBrush)         
    SetTextColor_(*lParam\hDC, $FF3028)     
  EndIf
  RoundRect_(*lParam\hDC, 0, 0, *lParam\rcItem\right,*lParam\rcItem\bottom, 8, 8)
  DrawText_(*lParam\hDC, GetGadgetText(wParam), -1, *lParam\rcItem,#DT_CENTER | #DT_VCENTER | #DT_SINGLELINE)
EndProcedure


Procedure WCB(hWnd,uMsg,wParam,lParam)
  Select uMsg
    Case #WM_DRAWITEM
      If IsGadget(wParam) And GadgetType(wParam) = #PB_GadgetType_Button
        DrawButtonCB(wParam, lParam)
        ProcedureReturn 0
      EndIf
      
  EndSelect
  
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0,0,0,400,200,"TEST",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0,10,165,120,25,"COLOR BUTTON 0",#BS_OWNERDRAW)
ButtonGadget(1,140,165,120,25,"COLOR BUTTON 1",#BS_OWNERDRAW)

;SetClassLongPtr_(GadgetID(1),#GCL_STYLE,GetClassLongPtr_(GadgetID(1),#GCL_STYLE) | #CS_DBLCLKS)

SetWindowCallback(@WCB())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend


Re: ButtonGadget modify color by CallBack [Resolved]

Posted: Sun Jun 25, 2023 12:22 pm
by AZJIO
Perhaps you will be interested in my experiments. I did them when I wrote the launcher.

Code: Select all

; Base Rashad : https://www.purebasic.fr/english/viewtopic.php?p=360438#p360438

EnableExplicit

Global BBrush,TBrush, hBrush, RBrush, hPen, hFont
Global hIcon
; ExtractIconEx_("C:\WINDOWS\System32\Shell32.dll", 3, @hIcon, 0, 1)
ExtractIconEx_("C:\WINDOWS\System32\Shell32.dll", 3, 0, @hIcon, 1)
hPen = CreatePen_(#PS_SOLID, 4, $8080ff)
hFont = CreateFont_(18, 0, 0, 0, 400, 0, 0, 0,
                    #DEFAULT_CHARSET, #OUT_DEFAULT_PRECIS, #CLIP_DEFAULT_PRECIS, #DEFAULT_QUALITY, 0, "Arial")

BBrush = CreateSolidBrush_($222222)
TBrush = CreateSolidBrush_($eeeeee)
hBrush = CreateSolidBrush_($666600)
RBrush = CreateSolidBrush_($8080ff)


Procedure WCB(hWnd,uMsg,wParam,lParam)
	Protected *cb.DRAWITEMSTRUCT, circle
	Select uMsg
		Case #WM_DRAWITEM
			*cb.DRAWITEMSTRUCT = lParam
			
			SetBkMode_(*cb\hDC,#TRANSPARENT)
			
			If *cb\itemState & #ODS_SELECTED
				SelectObject_(*cb\hDC,BBrush)         
				SetTextColor_(*cb\hDC,$8080ff)
			Else
				SelectObject_(*cb\hDC,TBrush)         
				SetTextColor_(*cb\hDC,$0)     
			EndIf
			
			SelectObject_(*cb\hDC, hFont)
			FillRect_(*cb\hDC, *cb\rcItem, TBrush)
;			SelectObject_(*cb\hDC,RBrush)
			FrameRect_(*cb\hDC, *cb\rcItem, RBrush)
; 			DrawFrameControl_(*cb\hDC, *cb\rcItem, #DFC_BUTTON, #DFCS_BUTTONPUSH)
; 			DrawEdge_(*cb\hDC, *cb\rcItem, #EDGE_BUMP, #BF_RECT)
			circle = 0 ; если 0 то квадратная
; 			RoundRect_(*cb\hDC,0,0,*cb\rcItem\right,*cb\rcItem\bottom,circle,circle)
; 			RoundRect_(*cb\hDC,1,1,*cb\rcItem\right-1,*cb\rcItem\bottom-1,circle,circle)
			DrawText_(*cb\hDC,GetGadgetText(*cb\CtlID),-1,*cb\rcItem,#DT_CENTER | #DT_VCENTER | #DT_SINGLELINE)
			DrawIconEx_(*cb\hDC, 9, 9, hicon,16,16,0,0,#DI_NORMAL)
			
			ProcedureReturn 0
	EndSelect
	
	ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0,0,0,400,200,"Test",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
; SetWindowColor(0, $666600)
ButtonGadget(0,10,45,130,35,"Button 1",#BS_OWNERDRAW)
ButtonGadget(1,10,85,130,35,"Button 2",#BS_OWNERDRAW)
; ButtonGadget(2,10,125,130,35,"Button 3")


; SetClassLongPtr_(GadgetID(0),#GCL_STYLE,GetClassLongPtr_(GadgetID(0),#GCL_STYLE) | #CS_DBLCLKS)
; SetClassLongPtr_(GadgetID(1),#GCL_STYLE,GetClassLongPtr_(GadgetID(1),#GCL_STYLE) | #CS_DBLCLKS)

SetWindowCallback(@WCB())

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_Gadget
			Select EventGadget()
				Case 0
; 					MessageRequester("", GetGadgetText(0))
					SetWindowTitle(0 , GetGadgetText(0))
				Case 1
; 					MessageRequester("", GetGadgetText(1))
					SetWindowTitle(0 , GetGadgetText(1))
			EndSelect
		Case #PB_Event_CloseWindow
			DeleteObject_(BBrush)
			DeleteObject_(TBrush)
			DeleteObject_(hBrush)
			DeleteObject_(hFont)
			DeleteObject_(hPen)
			DeleteObject_(RBrush)
			CloseWindow(0)
			DestroyIcon_(hicon)
			End
	EndSelect
ForEver