Buttons

Just starting out? Need help? Post your questions and find answers here.
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Buttons

Post by doctorized »

I used to use Thorsten1977's ButtonEx (viewtopic.php?f=27&t=72520&hilit=buttonex) to have buttons, 120 width, 100 height, to present some data about people. Every button has a name in the center (1 or 2 lines according to name width) and some numbers and letters on the 4 corners of the button (my addition). I am experiencing some issues with right mouse click (to be honest I knew that Thorsten's module had code for showing menu with right click but the latest version that he provides, seems not to include such code, I even see it in my prelast post there). So, I need to find a new button (or at least something like that, which can be clicked) that will be able to show the data I refer above, set background color and control right mouse click to show a custom menu that I made. I do not care about rounded corners. Is there anything that I could use?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Buttons

Post by jacdelad »

On Windows:

Code: Select all

Define clickpoint.POINT
#Button = 1
#Window = 2

OpenWindow(#Window,0,0,200,200,"Test",#PB_Window_ScreenCentered)
ButtonGadget(#Button,10,10,180,180,"Push me")

Repeat
  Select WaitWindowEvent()
    Case #WM_RBUTTONUP
      GetCursorPos_(@clickpoint)
      If GetDlgCtrlID_(WindowFromPoint_(clickpoint\y << 32 + clickpoint\x))=#Button
        Debug "Do my stuff on rightclick"
      EndIf
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
...or I got the question wrong...
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: Buttons

Post by AZJIO »

Code: Select all

EnableExplicit

Define clickpoint.POINT
#Button = 1
#Window = 2
#Menu = 0

OpenWindow(#Window, 0, 0, 200, 200, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#Button, 4, 4, 33, 33, Chr($25BC))

If CreatePopupMenu(#Menu)
	MenuItem(0, "qwer")
	MenuItem(1, "asdf")
EndIf
Define MenuID = MenuID(#Menu)

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_Menu
			Select EventMenu()
				Case 0
					MessageRequester("", "qwer")
				Case 1
					MessageRequester("", "asdf")
			EndSelect
		Case #PB_Event_Gadget
			Select EventGadget()
				Case #Button
					MessageRequester("", "#Button")
			EndSelect
		Case #WM_RBUTTONUP
			GetCursorPos_(@clickpoint)
			If GetDlgCtrlID_(WindowFromPoint_(clickpoint\y << 32 + clickpoint\x))=#Button
				clickpoint\x = 4
				clickpoint\y = 4 + 33
				ClientToScreen_(WindowID(#Window), @clickpoint)
				TrackPopupMenu_(MenuID, #TPM_RIGHTBUTTON, clickpoint\x, clickpoint\y, 0, WindowID(#Window), #NUL)
			EndIf
		Case #PB_Event_CloseWindow
			Break
	EndSelect
ForEver
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Buttons

Post by doctorized »

I think I was not clear enough. See what I want to have. I want to be able to set 5 values, not just one, and set back color. That's why I can't use a simple button gadget.
Image
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Buttons

Post by jacdelad »

Then you need to custom draw them on a canvas.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Buttons

Post by doctorized »

I was wondering if there is any code ready to use.
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Buttons

Post by mk-soft »

Sadly, however, as an OOP example
Link: Own Flat Gadgets
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Buttons

Post by infratec »

Code: Select all

#ButtonColor = $E8DBA8


Structure DoctorizedButtonStructure
  Gadget.i
  TextGadget.i
  TextGadgetUL.i
  TextGadgetUR.i
  TextGadgetLL.i
  TextGadgetLR.i
  LeftClick.i
EndStructure


Procedure DoctorizedButtonGadgetCallBack()
  
  Protected *Parameter.DoctorizedButtonStructure
  
  
  *Parameter = GetGadgetData(EventGadget())
  If *Parameter
    If IsGadget(*Parameter\Gadget)
      Select EventType()
        Case #PB_EventType_MouseEnter
          SetGadgetAttribute(*Parameter\Gadget, #PB_Canvas_Cursor, #PB_Cursor_Hand)
        Case #PB_EventType_MouseLeave
          SetGadgetAttribute(*Parameter\Gadget, #PB_Canvas_Cursor, #PB_Cursor_Default)
;         Case #PB_EventType_LeftClick
;           If *Parameter\LeftClick
;             *Parameter\LeftClick = #False
;           Else
;             *Parameter\LeftClick = #True
;             PostEvent(#PB_Event_Gadget, EventWindow(), *Parameter\Gadget, #PB_EventType_LeftClick)
;           EndIf 
      EndSelect
    EndIf
  EndIf
  
EndProcedure




Procedure.i DoctorizedButtonGadget(Gadget.i, x, y, w, h, Text$, TextUL$="", TextUR$="", TextLL$="", TextLR$="")
  
  Protected Result.i
  Protected *Parameter.DoctorizedButtonStructure
  
  
  If Gadget = #PB_Any
    Gadget = CanvasGadget(#PB_Any, x, y, w, h, #PB_Canvas_Container)
    If Gadget
      Result = #True
    EndIf
  Else
    Result = CanvasGadget(Gadget, x, y, w, h, #PB_Canvas_Container)
  EndIf
  
  If Result
    
    If StartDrawing(CanvasOutput(Gadget))
      Box(0, 0, w, h, #ButtonColor)
      StopDrawing()
    EndIf
    
    *Parameter = AllocateStructure(DoctorizedButtonStructure)
    If *Parameter
      *Parameter\Gadget = Gadget
      SetGadgetData(Gadget, *Parameter)
      *Parameter\TextGadgetUL = TextGadget(#PB_Any, 5, 5, GadgetWidth(Gadget) / 2, 20, TextUL$)
      SetGadgetColor(*Parameter\TextGadgetUL, #PB_Gadget_BackColor, #ButtonColor)
      *Parameter\TextGadgetUR = TextGadget(#PB_Any, GadgetWidth(Gadget) / 2, 5, GadgetWidth(Gadget) / 2 - 5, 20, TextUR$, #PB_Text_Right)
      SetGadgetColor(*Parameter\TextGadgetUR, #PB_Gadget_BackColor, #ButtonColor)
      *Parameter\TextGadgetLL = TextGadget(#PB_Any, 5, GadgetHeight(Gadget) - 20 - 5, GadgetWidth(Gadget) / 2, 20, TextLL$)
      SetGadgetColor(*Parameter\TextGadgetLL, #PB_Gadget_BackColor, #ButtonColor)
      *Parameter\TextGadgetLR = TextGadget(#PB_Any, GadgetWidth(Gadget) / 2, GadgetHeight(Gadget) - 20 - 5, GadgetWidth(Gadget) / 2 - 5, 20, TextLR$, #PB_Text_Right)
      SetGadgetColor(*Parameter\TextGadgetLR, #PB_Gadget_BackColor, #ButtonColor)
      
      *Parameter\TextGadget = TextGadget(#PB_Any, 0, GadgetHeight(Gadget) / 2 - 10, GadgetWidth(Gadget), 20, Text$, #PB_Text_Center)
      SetGadgetColor(*Parameter\TextGadget, #PB_Gadget_BackColor, #ButtonColor)
    EndIf
    
    CloseGadgetList()
    
    BindGadgetEvent(Gadget, @DoctorizedButtonGadgetCallBack(), #PB_All)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure






OpenWindow(0, 0, 0, 400, 300, "Test", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)

DoctorizedButtonGadget(1, 10, 10, 200, 200, "Press", "Paid", "32", "156", "GF")

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          If EventType() = #PB_EventType_LeftClick
            Debug "Pressed"
          EndIf
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow
Last edited by infratec on Thu Sep 16, 2021 5:04 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Buttons

Post by infratec »

Simpler Variant:

Code: Select all

#ButtonColor = $E8DBA8


Procedure DoctorizedButtonGadgetCallBack()
  
  Select EventType()
    Case #PB_EventType_MouseEnter
      SetGadgetAttribute(EventGadget(), #PB_Canvas_Cursor, #PB_Cursor_Hand)
    Case #PB_EventType_MouseLeave
      SetGadgetAttribute(EventGadget(), #PB_Canvas_Cursor, #PB_Cursor_Default)
  EndSelect
  
EndProcedure




Procedure.i DoctorizedButtonGadget(Gadget.i, x, y, w, h, Text$, TextUL$="", TextUR$="", TextLL$="", TextLR$="")
  
  Protected Result.i
  
  
  If Gadget = #PB_Any
    Gadget = CanvasGadget(#PB_Any, x, y, w, h)
    If Gadget
      Result = #True
    EndIf
  Else
    Result = CanvasGadget(Gadget, x, y, w, h)
  EndIf
  
  If Result
    
    If StartDrawing(CanvasOutput(Gadget))
      Box(0, 0, w, h, #ButtonColor)
      
      DrawText(5, 5, TextUL$, #Black, #ButtonColor)
      DrawText(GadgetWidth(Gadget) - TextWidth(TextUR$) - 5, 5, TextUR$, #Black, #ButtonColor)
      DrawText(5, GadgetWidth(Gadget) - TextHeight("A") - 5, TextLL$, #Black, #ButtonColor)
      DrawText(GadgetWidth(Gadget) - TextWidth(TextLR$) - 5, GadgetHeight(Gadget) - TextHeight("A") - 5, TextLR$, #Black, #ButtonColor)
      
      DrawText((GadgetWidth(Gadget) - TextWidth(Text$)) / 2, GadgetHeight(Gadget) / 2 - TextHeight("A") / 2, Text$, #Black, #ButtonColor)
      
      StopDrawing()
    EndIf
    
    BindGadgetEvent(Gadget, @DoctorizedButtonGadgetCallBack(), #PB_EventType_MouseEnter)
    BindGadgetEvent(Gadget, @DoctorizedButtonGadgetCallBack(), #PB_EventType_MouseLeave)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure






OpenWindow(0, 0, 0, 400, 300, "Test", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)

DoctorizedButtonGadget(1, 10, 10, 200, 200, "Press", "Paid", "32", "156", "GF")

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          If EventType() = #PB_EventType_LeftClick
            Debug "Pressed"
          EndIf
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Buttons

Post by infratec »

And at least, a 'mixed' version:

Code: Select all

#ButtonColor = $E8DBA8


Structure DoctorizedButtonStructure
  Gadget.i
  TextUL$
  TextUR$
  TextLL$
  TextLR$
  TextC$
  FrontColor.i
  BackColor.i
EndStructure


Procedure DoctorizedButtonGadgetCallBack()
  
  Protected *Parameter.DoctorizedButtonStructure
  
  
  *Parameter = GetGadgetData(EventGadget())
  If *Parameter
    If IsGadget(*Parameter\Gadget)
      Select EventType()
        Case #PB_EventType_MouseEnter
          SetGadgetAttribute(EventGadget(), #PB_Canvas_Cursor, #PB_Cursor_Hand)
        Case #PB_EventType_MouseLeave
          SetGadgetAttribute(EventGadget(), #PB_Canvas_Cursor, #PB_Cursor_Default)
      EndSelect
    EndIf
  EndIf 
  
EndProcedure




Procedure.i SetDoctorizedButtonGadgetColor(Gadget.i, ColorType.i, Color.i)
  
  Protected *Parameter.DoctorizedButtonStructure, w.i, h.i
  
  
  *Parameter = GetGadgetData(Gadget)
  If *Parameter
    
    If ColorType = #PB_Gadget_FrontColor
      If Color <> #PB_Default
        *Parameter\FrontColor = Color
      EndIf
    EndIf
    
    If ColorType = #PB_Gadget_BackColor
      If Color <> #PB_Default
        *Parameter\BackColor = Color
      EndIf
    EndIf
    
    If StartDrawing(CanvasOutput(Gadget))
      
      h = GadgetHeight(Gadget)
      w = GadgetWidth(Gadget)
      
      Box(0, 0, w, h, *Parameter\BackColor)
      
      DrawText(5, 5, *Parameter\TextUL$, *Parameter\FrontColor, *Parameter\BackColor)
      DrawText(w - TextWidth(*Parameter\TextUR$) - 5, 5, *Parameter\TextUR$, *Parameter\FrontColor, *Parameter\BackColor)
      DrawText(5, h - TextHeight("A") - 5, *Parameter\TextLL$, *Parameter\FrontColor, *Parameter\BackColor)
      DrawText(w - TextWidth(*Parameter\TextLR$) - 5, h - TextHeight("A") - 5, *Parameter\TextLR$, *Parameter\FrontColor, *Parameter\BackColor)
      
      DrawText((GadgetWidth(Gadget) - TextWidth(*Parameter\TextC$)) / 2, GadgetHeight(Gadget) / 2 - TextHeight("A") / 2, *Parameter\TextC$, *Parameter\FrontColor, *Parameter\BackColor)
      
      StopDrawing()
    EndIf
  EndIf
  
EndProcedure




Procedure.i DoctorizedButtonGadget(Gadget.i, x, y, w, h, Text$, TextUL$="", TextUR$="", TextLL$="", TextLR$="")
  
  Protected Result.i
  Protected *Parameter.DoctorizedButtonStructure
  
  
  If Gadget = #PB_Any
    Gadget = CanvasGadget(#PB_Any, x, y, w, h)
    If Gadget
      Result = #True
    EndIf
  Else
    Result = CanvasGadget(Gadget, x, y, w, h)
  EndIf
  
  If Result
    
    *Parameter = AllocateStructure(DoctorizedButtonStructure)
    If *Parameter
      SetGadgetData(Gadget, *Parameter)
      *Parameter\Gadget = Gadget      
      *Parameter\TextUL$ = TextUL$
      *Parameter\TextUR$ = TextUR$
      *Parameter\TextLL$ = TextLL$
      *Parameter\TextLR$ = TextLR$
      *Parameter\TextC$ = Text$
      *Parameter\FrontColor = $444444
      *Parameter\BackColor = $E8DBA8
    EndIf
    
    SetDoctorizedButtonGadgetColor(Gadget, #PB_Gadget_FrontColor, #PB_Default)
    
    BindGadgetEvent(Gadget, @DoctorizedButtonGadgetCallBack(), #PB_EventType_MouseEnter)
    BindGadgetEvent(Gadget, @DoctorizedButtonGadgetCallBack(), #PB_EventType_MouseLeave)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure






OpenWindow(0, 0, 0, 440, 300, "Test", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)

DoctorizedButtonGadget(1, 10, 10, 200, 200, "Press", "Paid", "32", "156", "GF")

DoctorizedButtonGadget(2, 220, 10, 200, 200, "Press", "Paid", "32", "156", "GF")
SetDoctorizedButtonGadgetColor(2, #PB_Gadget_BackColor, $0000A0)
SetDoctorizedButtonGadgetColor(2, #PB_Gadget_FrontColor, $FFFFFF)

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          If EventType() = #PB_EventType_LeftClick
            Debug "1 Pressed"
          EndIf
          
        Case 2
          If EventType() = #PB_EventType_LeftClick
            Debug "2 Pressed"
          EndIf
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Buttons

Post by RASHAD »

Straight forward with Right mouse click
# 1:

Code: Select all

Procedure PBbutton(id,x,y,w,h,bcolor,fcolor,txt1.s,txt2.s,txt3.s,txt4.s,txt5.s)
  img = CreateImage(#PB_Any,w,h,24,bcolor)
  StartDrawing(ImageOutput(img))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawingFont(#PB_Default)
  DrawText(5,5,txt1,fcolor)
  length = TextWidth(txt2)
  DrawText(w-5-length,5,txt2,fcolor)
  height = TextHeight(txt3)
  DrawText(5,h-5-height,txt3,fcolor)
  length = TextWidth(txt4)
  DrawText(w-5-length,h-5-height,txt4,fcolor)
  length = TextWidth(txt5)
  DrawText(w/2-length/2,h/2-height/2,txt5,fcolor)
  StopDrawing()
  ButtonImageGadget(id, x,y,w,h, ImageID(img))  
EndProcedure


If OpenWindow(0, 0, 0, 200, 200, "ButtonImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  PBbutton(0,10,10,100,100,$00FFFF,$0000FF,"Paid","32","156","GF","Push me")
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_RBUTTONDOWN,#WM_RBUTTONUP
        GetCursorPos_ (@p.POINT) 
        ScreenToClient_ (WindowID(0), @p)     
        Select ChildWindowFromPoint_ (WindowID(0), p\y<< 32+p\x) 
          Case GadgetID(0) 
            Debug "PBbutton got the Focus"
        EndSelect 
        
    EndSelect
  Until Quit = 1
EndIf
# 2:

Code: Select all

Procedure PBbutton(id,x,y,w,h,bcolor,fcolor,txt1.s,txt2.s,txt3.s,txt4.s,txt5.s)
  img = CreateImage(#PB_Any,w,h,24,bcolor)
  StartDrawing(ImageOutput(img))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawingFont(#PB_Default)
  DrawText(5,5,txt1,fcolor)
  length = TextWidth(txt2)
  DrawText(w-5-length,5,txt2,fcolor)
  height = TextHeight(txt3)
  DrawText(5,h-5-height,txt3,fcolor)
  length = TextWidth(txt4)
  DrawText(w-5-length,h-5-height,txt4,fcolor)
  length = TextWidth(txt5)
  DrawText(w/2-length/2,h/2-height/2,txt5,fcolor)
  StopDrawing()
  ButtonImageGadget(id, x,y,w,h, ImageID(img))  
EndProcedure


If OpenWindow(0, 0, 0, 200, 200, "ButtonImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  PBbutton(0,10,10,100,100,$00FFFF,$0000FF,"Paid","32","156","GF","Push me")
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_RBUTTONDOWN,#WM_RBUTTONUP
        GetCursorPos_ (@p.POINT) 
        ScreenToClient_ (WindowID(0), @p)     
        Select ChildWindowFromPoint_ (WindowID(0), p\y<< 32+p\x) 
          Case GadgetID(0) 
            FreeGadget(0)
            PBbutton(0,10,10,100,100,$F0F0F0,$FF0000,"Paid","3200","156","GFFF","Push me #2")
        EndSelect 
        
    EndSelect
  Until Quit = 1
EndIf

# 3:
Using CanvasGadget()

Code: Select all

Procedure PBbutton(id,x,y,w,h,bcolor,fcolor,txt1.s,txt2.s,txt3.s,txt4.s,txt5.s)
  img = CreateImage(#PB_Any,w,h,24,bcolor)  
  StartDrawing(ImageOutput(img))
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(0,0,w,h,$0) 
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(#PB_Default)
    DrawText(5,5,txt1,fcolor)
    length = TextWidth(txt2)
    DrawText(w-5-length,5,txt2,fcolor)
    height = TextHeight(txt3)
    DrawText(5,h-5-height,txt3,fcolor)
    length = TextWidth(txt4)
    DrawText(w-5-length,h-5-height,txt4,fcolor)
    length = TextWidth(txt5)
    DrawText(w/2-length/2,h/2-height/2,txt5,fcolor)
  StopDrawing()
  CanvasGadget(id, x,y,w,h)
  SetGadgetAttribute(id,#PB_Canvas_Image ,ImageID(img))  
EndProcedure


If OpenWindow(0, 0, 0, 200, 200, "ButtonImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  PBbutton(0,10,10,100,100,$00FFFF,$0000FF,"Paid","32","156","GF","Push me")
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_RBUTTONDOWN,#WM_RBUTTONUP
        GetCursorPos_ (@p.POINT) 
        ScreenToClient_ (WindowID(0), @p)     
        Select ChildWindowFromPoint_ (WindowID(0), p\y<< 32+p\x) 
          Case GadgetID(0) 
            FreeGadget(0)
            PBbutton(0,10,10,100,100,$F0F0F0,$FF0000,"Paid","3200","156","GFFF","Push me #2")
        EndSelect 
        
    EndSelect
  Until Quit = 1
EndIf

Egypt my love
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Buttons

Post by doctorized »

I really do appreciate your codes. To be honest, I like all of them! Thank you! Thank you!! Thank you!!!
Post Reply