Flat button with border when hovered ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Flat button with border when hovered ?

Post by charvista »

Hello,

I would like to create flat buttons with a different background color when the mouse cursor is hovered on the item. So far, no problem. But I would like that he has a color border around it, like this picture:
Image

So far the code:

Code: Select all

Procedure  HoverGad(Gad)
    Protected Pt.Point,Rc.Rect
    GetCursorPos_(Pt)
    GetWindowRect_(GadgetID(Gad),Rc)
    ProcedureReturn PtInRect_(@Rc,Pt\X|Pt\Y<<32)
EndProcedure
;==================================================================================================
Macro GadRGB(Gad,Front=#White,Back=#Black)
    SetGadgetColor(Gad,#PB_Gadget_FrontColor,Front)
    SetGadgetColor(Gad,#PB_Gadget_BackColor,Back)
EndMacro
;==================================================================================================
Procedure CheckOver(Ev,Gad,HoverTextRGB=#Red,HoverBackRGB=#Green,TextRGB=#Blue,BackRGB=#White)
    Static Hover
    If MMk=0 And Ev=#WM_MOUSEMOVE
        If HoverGad(Gad)
            If Hover=0
                GadRGB(Gad,HoverTextRGB,HoverBackRGB)
                Hover=Gad 
            EndIf 
        ElseIf Hover=Gad
            GadRGB(Gad,TextRGB,BackRGB) 
            Hover=0 
        EndIf
    EndIf
EndProcedure
;==================================================================================================
TextRGB=#Blue
BackRGB=$F0F0F0
BorderColor=#Black; unused at this time
;==================================================================================================
HoverTextRGB=#Red
HoverBackRGB=$FFE0C2
HoverBorderColor=$FF9933; unused at this time
SelectTextRGB=#Green
;==================================================================================================
OpenWindow(0, 0, 0, 300, 500, "TextGadget as Button", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    SetWindowColor(0,$f0f0f0)
    MyfontId=FontID(LoadFont(#PB_Any,"Segoe UI",9))
    TextGadget(1, 10, 100, 190,20,"Borderless Button", #PB_Text_Center|#SS_NOTIFY)
    SetGadgetFont(1,MyfontId)
    GadRGB(1,TextRGB,BackRGB)
    
    TextGadget(2, 10, 150, 190,18,"Button 2", #PB_Text_Center|#WS_BORDER|#SS_NOTIFY)
    SetGadgetFont(2,MyFontID)
    GadRGB(2,TextRGB,BackRGB)
    Repeat
        If GetAsyncKeyState_(27)&$8000 :  End : EndIf
        Ev = WaitWindowEvent()
        If Ev = #PB_Event_Gadget
            Select EventGadget()
                Case 1
                    Debug "Clicked on button #1"
                    GadRGB(EventGadget(),SelectTextRGB,BackRGB)
                Case 2
                    Debug "Clicked on button #2"
                    GadRGB(EventGadget(),SelectTextRGB,BackRGB)
            EndSelect
        Else
            CheckOver(Ev,1,HoverTextRGB,HoverBackRGB,TextRGB,BackRGB)
            CheckOver(Ev,2,HoverTextRGB,HoverBackRGB,TextRGB,BackRGB)
        EndIf
    Until Ev = #PB_Event_CloseWindow
;==================================================================================================
It is the borderless button that must have a colored border (variable HoverBorderColor/BorderColor).

The Button2 has a fixed black color, using the flag #WS_BORDER, should normally not be changed.

Thanks in advance.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Flat button with border when hovered ?

Post by RASHAD »

@charvista Hi
How about that :mrgreen:
Now go to sleep

Code: Select all

Procedure  HoverGad(Gad)
    Protected Pt.Point,Rc.Rect
    GetCursorPos_(Pt)
    GetWindowRect_(GadgetID(Gad),Rc)
    ProcedureReturn PtInRect_(@Rc,Pt\X|Pt\Y<<32)
EndProcedure
;==================================================================================================
Macro GadRGB(Gad,Front=#White,Back=#Black)
    SetGadgetColor(Gad,#PB_Gadget_FrontColor,Front)
    SetGadgetColor(Gad,#PB_Gadget_BackColor,Back)
EndMacro
;==================================================================================================
Procedure CheckOver(Ev,Gad,HoverTextRGB=#Red,HoverBackRGB=#Green,TextRGB=#Blue,BackRGB=#White)
    Static Hover
    If MMk=0 And Ev=#WM_MOUSEMOVE
        If HoverGad(Gad)
            If Hover=0 And Gad = 1
                GadRGB(Gad,HoverTextRGB,HoverBackRGB)
                SetGadgetColor(0,#PB_Gadget_BackColor,#Red)
            Else
                GadRGB(Gad,HoverTextRGB,HoverBackRGB)
            EndIf
                Hover=Gad       
        ElseIf Hover=Gad
            SetGadgetColor(0,#PB_Gadget_BackColor,$F0F0F0)
            GadRGB(Gad,TextRGB,BackRGB)
            Hover=0
        EndIf
    EndIf
EndProcedure
;==================================================================================================
TextRGB=#Blue
BackRGB=$F0F0F0
BorderColor=#Black; unused at this time
;==================================================================================================
HoverTextRGB=#Red
HoverBackRGB=$FFE0C2
HoverBorderColor=$FF9933; unused at this time
SelectTextRGB=#Green
;==================================================================================================
OpenWindow(0, 0, 0, 300, 500, "TextGadget as Button", #PB_Window_SystemMenu| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered)
    SetWindowColor(0,$f0f0f0)
    MyfontId=FontID(LoadFont(#PB_Any,"Segoe UI",9))
    ContainerGadget(0,10,100,190,22,#PB_Container_BorderLess)
    TextGadget(1, 1, 1, 188,20,"Borderless Button",  #SS_CENTERIMAGE | #SS_CENTER| #SS_NOTIFY)
    CloseGadgetList()
    SetGadgetFont(1,MyfontId)
    GadRGB(1,TextRGB,BackRGB)
   
    TextGadget(2, 10, 150, 190,22,"Button 2", #SS_CENTERIMAGE | #SS_CENTER| #WS_BORDER| #SS_NOTIFY)
    SetGadgetFont(2,MyFontID)
    GadRGB(2,TextRGB,BackRGB)
    Repeat
        If GetAsyncKeyState_(27)&$8000 :  End : EndIf
        Ev = WaitWindowEvent()
        If Ev = #PB_Event_Gadget
            Select EventGadget()
                Case 1
                    Debug "Clicked on button #1"
                    GadRGB(EventGadget(),SelectTextRGB,BackRGB)
                Case 2
                    Debug "Clicked on button #2"
                    GadRGB(EventGadget(),SelectTextRGB,BackRGB)
            EndSelect
        Else
            CheckOver(Ev,1,HoverTextRGB,HoverBackRGB,TextRGB,BackRGB)
            CheckOver(Ev,2,HoverTextRGB,HoverBackRGB,TextRGB,BackRGB)
        EndIf
    Until Ev = #PB_Event_CloseWindow
;==================================================================================================
Last edited by RASHAD on Fri Mar 01, 2013 11:20 pm, edited 1 time in total.
Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Flat button with border when hovered ?

Post by charvista »

Marvellous!!!!
Thank you Doctor RASHAD :mrgreen:
You are the best doctor on this planet :mrgreen:
where is my bed?
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Flat button with border when hovered ?

Post by RASHAD »

:mrgreen:
You will recover soon as long as you listen to your Doctor :P
Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Flat button with border when hovered ?

Post by charvista »

:D Not only a Doctor, but a good friend as well :D
I could spring and dance like KCC :mrgreen:
but then without the GIF files :mrgreen:

I was wondering if I could set the frame thickness to 1 pixel, as I see it is 2 pixels wide..... :?:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Flat button with border when hovered ?

Post by RASHAD »

I did that already when I noticed it is ugly
Previous post updated

charvista you still awaken :evil:
You will take some more time to recover :P
Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Flat button with border when hovered ?

Post by charvista »

This is perfect! I noticed the addition of the ContainerGadget
I already tried to correct it and before it was finished, it gives a nice shadow effet :D

Code: Select all

Procedure  HoverGad(Gad)
    Protected Pt.Point,Rc.Rect
    GetCursorPos_(Pt)
    GetWindowRect_(GadgetID(Gad),Rc)
    ProcedureReturn PtInRect_(@Rc,Pt\X|Pt\Y<<32)
EndProcedure
;==================================================================================================
Macro GadRGB(Gad,Front=#White,Back=#Black)
    SetGadgetColor(Gad,#PB_Gadget_FrontColor,Front)
    SetGadgetColor(Gad,#PB_Gadget_BackColor,Back)
EndMacro
;==================================================================================================
Procedure CheckOver(Ev,Gad,HoverTextRGB=#Red,HoverBackRGB=#Green,TextRGB=#Blue,BackRGB=#White)
    Static Hover
    If MMk=0 And Ev=#WM_MOUSEMOVE
        If HoverGad(Gad)
            If Hover=0 And Gad = 1
                GadRGB(Gad,HoverTextRGB,HoverBackRGB)
                SetGadgetColor(0,#PB_Gadget_BackColor,#Red)
            Else
                GadRGB(Gad,HoverTextRGB,HoverBackRGB)
            EndIf
            Hover=Gad       
        ElseIf Hover=Gad
            SetGadgetColor(0,#PB_Gadget_BackColor,$F0F0F0)
            GadRGB(Gad,TextRGB,BackRGB)
            Hover=0
        EndIf
    EndIf
EndProcedure
;==================================================================================================
TextRGB=#Blue
BackRGB=$F0F0F0
BorderColor=#Black; unused at this time
;==================================================================================================
HoverTextRGB=#Red
HoverBackRGB=$FFE0C2
HoverBorderColor=$FF9933; unused at this time
SelectTextRGB=#Green
;==================================================================================================
OpenWindow(0, 0, 0, 300, 500, "TextGadget as Button", #PB_Window_SystemMenu| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered)
    SetWindowColor(0,$f0f0f0)
    MyfontId=FontID(LoadFont(#PB_Any,"Segoe UI",9))
    ContainerGadget(0,10,100,190,24,#PB_Container_BorderLess)
    TextGadget(1, 1,1, 186,20,"Borderless Button",  #SS_CENTERIMAGE | #SS_CENTER| #SS_NOTIFY)
    CloseGadgetList()
    SetGadgetFont(1,MyfontId)
    GadRGB(1,TextRGB,BackRGB)
    
    TextGadget(2, 10, 150, 190,22,"Button 2", #SS_CENTERIMAGE | #SS_CENTER| #WS_BORDER| #SS_NOTIFY)
    SetGadgetFont(2,MyFontID)
    GadRGB(2,TextRGB,BackRGB)
    Repeat
        If GetAsyncKeyState_(27)&$8000 :  End : EndIf
        Ev = WaitWindowEvent()
        If Ev = #PB_Event_Gadget
            Select EventGadget()
                Case 1
                    Debug "Clicked on button #1"
                    GadRGB(EventGadget(),SelectTextRGB,BackRGB)
                Case 2
                    Debug "Clicked on button #2"
                    GadRGB(EventGadget(),SelectTextRGB,BackRGB)
            EndSelect
        Else
            CheckOver(Ev,1,HoverTextRGB,HoverBackRGB,TextRGB,BackRGB)
            CheckOver(Ev,2,HoverTextRGB,HoverBackRGB,TextRGB,BackRGB)
        EndIf
    Until Ev = #PB_Event_CloseWindow
    ;==================================================================================================
    
Thank you and Good night Sir Doctor Rashad :mrgreen: Otherwise my eyes will be like this: :shock:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Flat button with border when hovered ?

Post by Andre »

Even if I couldn't test this for now (I'm on MacOS at the moment...), it seems like a useful addition to PB. So I made a feature-request for a cross-platform HoverGadget just now... :mrgreen:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Flat button with border when hovered ?

Post by charvista »

Thanks to the excellent work from RASHAD, I could not sleep at all :mrgreen:
Instead, I have created a PullDownGadget :mrgreen:
Fred will be happy :P

Perhaps is this what André would like to have....... :wink:
And yes, perhaps it is a good idea to add this function to PB.

The PullDownGadget works well, but only when we are slow with the mouse. If we go too fast, we seem to get double selections, because the previous was not cleared yet. But I leave it to the experts to see what is wrong.
Sure, the Select EventGadget() is not elegant when checking the array, and I have not found a correct way to make it a single check (a For-Next cannot be used in a Select-Case loop).

Code: Select all

Procedure zHoverGad(Gadget.i)
    GetCursorPos_(Mouse.Point)
    GetWindowRect_(GadgetID(Gadget.i),Rc.Rect)
    ProcedureReturn PtInRect_(@Rc,Mouse\X|Mouse\Y<<32)
EndProcedure
;==================================================================================================
Procedure zCheckHover(Array Pull.s(2),TextColor=$000000,BackColor=$F0F0F0,BorderColor=$F0F0F0,HoverTextColor=$000000,HoverBackColor=$FFE0C2,HoverBorderColor=$FF9933)
    Static Hover.i
    For i=1 To ArraySize(Pull.s(),1)
        If zHoverGad(Val(Pull.s(i,2)))
            If Hover=0
                SetGadgetColor(Val(Pull.s(i,2)),#PB_Gadget_FrontColor,HoverTextColor)
                SetGadgetColor(Val(Pull.s(i,2)),#PB_Gadget_BackColor,HoverBackColor)
                SetGadgetColor(Val(Pull.s(i,1)),#PB_Gadget_BackColor,HoverBorderColor)
            EndIf
            Hover=Val(Pull.s(i,2))
        ElseIf Hover=Val(Pull.s(i,2))
            SetGadgetColor(Val(Pull.s(i,2)),#PB_Gadget_FrontColor,TextColor)
            SetGadgetColor(Val(Pull.s(i,2)),#PB_Gadget_BackColor,BackColor)
            SetGadgetColor(Val(Pull.s(i,1)),#PB_Gadget_BackColor,BorderColor)
            Hover=0
        EndIf
    Next
EndProcedure
;==================================================================================================
TextColor=$000000
BackColor=$F0F0F0
BorderColor=$F0F0F0
;==================================================================================================
HoverTextColor=$000000
HoverBackColor=$FFE0C2
HoverBorderColor=$FF9933
;==================================================================================================
SelectTextColor=$000000
SelectBackColor=$FFE0C2
SelectBorderColor=$FF9933
;==================================================================================================
Procedure zPullDownGadget(X.i,Y.i,W.i,H.i,Array Pull.s(2));,FontNumber.i=151)
    MyFontId=FontID(LoadFont(#PB_Any,"Segoe UI",10))
    
    NbElements=ArraySize(Pull.s(),1)
    If NbElements
        For i=1 To NbElements
            Pull.s(i,1)=Str(ContainerGadget(#PB_Any,X,Y+H*(i-1),W,H,#PB_Container_BorderLess))
                Pull.s(i,2)=Str(TextGadget(#PB_Any,1,1,W-2,H-2,Pull.s(i,0),#SS_CENTERIMAGE|#SS_CENTER|#SS_NOTIFY))
            CloseGadgetList()
            SetGadgetFont(Val(Pull.s(i,2)),MyFontId);  _Font(FontNumber.i,0))
            SetGadgetColor(Val(Pull.s(i,1)),#PB_Gadget_FrontColor,$000000)
            SetGadgetColor(Val(Pull.s(i,1)),#PB_Gadget_BackColor,$F0F0F0)
        Next
    EndIf
EndProcedure
;==================================================================================================
Win=OpenWindow(#PB_Any,0,0,300,500,"PullDown Gadget",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    SetWindowColor(Win,$F0F0F0)
    
    Dim Pull.s(5,2)
    Pull(1,0)="Printer"
    Pull(2,0)="Computer"
    Pull(3,0)="Mouse"
    Pull(4,0)="Keyboard"
    Pull(5,0)="Hard Disk"
    
    zPullDownGadget(10,20,200,22,Pull.s())
    
    Repeat
        If GetAsyncKeyState_(27)&$8000 : End : EndIf
        Event=WaitWindowEvent()
        Select Event
            Case #PB_Event_CloseWindow
                ExitEventLoop=#True
            Case #PB_Event_Gadget
                Select EventGadget()
                    Case Val(Pull.s(1,2))
                        Debug "Clicked on "+Pull.s(1,0)
                        SetGadgetColor(Val(Pull.s(1,2)),#PB_Gadget_FrontColor,SelectTextColor)
                        SetGadgetColor(Val(Pull.s(1,2)),#PB_Gadget_BackColor,SelectBackColor)
                        SetGadgetColor(Val(Pull.s(1,1)),#PB_Gadget_BackColor,SelectBorderColor)
                    Case Val(Pull.s(2,2))
                        Debug "Clicked on "+Pull.s(2,0)
                        SetGadgetColor(Val(Pull.s(2,2)),#PB_Gadget_FrontColor,SelectTextColor)
                        SetGadgetColor(Val(Pull.s(2,2)),#PB_Gadget_BackColor,SelectBackColor)
                        SetGadgetColor(Val(Pull.s(2,1)),#PB_Gadget_BackColor,SelectBorderColor)
                    Case Val(Pull.s(3,2))
                        Debug "Clicked on "+Pull.s(3,0)
                        SetGadgetColor(Val(Pull.s(3,2)),#PB_Gadget_FrontColor,SelectTextColor)
                        SetGadgetColor(Val(Pull.s(3,2)),#PB_Gadget_BackColor,SelectBackColor)
                        SetGadgetColor(Val(Pull.s(3,1)),#PB_Gadget_BackColor,SelectBorderColor)
                    Case Val(Pull.s(4,2))
                        Debug "Clicked on "+Pull.s(4,0)
                        SetGadgetColor(Val(Pull.s(4,2)),#PB_Gadget_FrontColor,SelectTextColor)
                        SetGadgetColor(Val(Pull.s(4,2)),#PB_Gadget_BackColor,SelectBackColor)
                        SetGadgetColor(Val(Pull.s(5,1)),#PB_Gadget_BackColor,SelectBorderColor)
                    Case Val(Pull.s(5,2))
                        Debug "Clicked on "+Pull.s(5,0)
                        SetGadgetColor(Val(Pull.s(5,2)),#PB_Gadget_FrontColor,SelectTextColor)
                        SetGadgetColor(Val(Pull.s(5,2)),#PB_Gadget_BackColor,SelectBackColor)
                        SetGadgetColor(Val(Pull.s(5,1)),#PB_Gadget_BackColor,SelectBorderColor)
                EndSelect
            Case #WM_MOUSEMOVE
                zCheckHover(Pull.s())
        EndSelect
    Until ExitEventLoop
    ;==================================================================================================
Enjoy!!!!
Now I must obey Doctor Rashad: go to bed and have a lot of time to recover :twisted:
See you back tomorrow :wink:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Flat button with border when hovered ?

Post by Bisonte »

And here the ordinary CanvasVersion ... crossplatform, I think.

Code: Select all

; »»» FlatBorderButton
; »»» -------------------------------------------------------------------------------------------------

EnableExplicit
;
Prototype flatprototype(Pointer, Event = 0)
;
Structure flat_border_button_datas
  
  Gadget.i
  Drawing.flatprototype
  Text.s
  TextColor.i
  InnerColor.i
  BorderColor.i
  BackGroundColor.i
  Font.i
  ExtraData.i
  
EndStructure
;
Procedure FlatBorderButtonDraw(*p.flat_border_button_datas, Event = 0)
  
  Protected x, y, th
  
  If *p
    If IsGadget(*p\Gadget)
      If StartDrawing(CanvasOutput(*p\Gadget))
        Select Event
          Case 0
            DrawingMode(#PB_2DDrawing_Default)
            Box(0,0,OutputWidth(),OutputHeight(),*p\BackGroundColor)
          Case 1
            DrawingMode(#PB_2DDrawing_Default)
            Box(0,0,OutputWidth(),OutputHeight(),*p\BorderColor)  
            Box(1,1,OutputWidth()-2,OutputHeight()-2,*p\InnerColor)            
        EndSelect
                    
        If IsFont(*p\Font)
          DrawingFont(FontID(*p\Font))
        EndIf
        x = (OutputWidth()/2) - (TextWidth(*p\Text)/2) 
        y = (OutputHeight()/2) - (TextHeight(*p\Text)/2)
        DrawingMode(#PB_2DDrawing_Default|#PB_2DDrawing_Transparent)
        DrawText(x,y,*p\Text,*p\TextColor)
        
        StopDrawing()
      EndIf
    EndIf
  EndIf
  
EndProcedure
;
Procedure FlatBorderButton(Gadget, x, y, Width, Height, Text.s, TextColor, BackGroundColor, InnerColor, BorderColor, Font)
  
  Protected *p.flat_border_button_datas = AllocateMemory(SizeOf(flat_border_button_datas))
  Protected ID
  
  If Not *p : ProcedureReturn #False : EndIf
  InitializeStructure(*p, flat_border_button_datas)
  
  ID = CanvasGadget(Gadget, x, y, Width, Height)
  
  If Gadget = #PB_Any : Gadget = ID : EndIf
  
  SetGadgetData(Gadget, *p)
  
  *p\Gadget = Gadget
  *p\Text   = Text
  *p\BorderColor = BorderColor
  *p\BackGroundColor = BackGroundColor
  *p\InnerColor = InnerColor
  *p\Font = Font
  *p\Drawing = @FlatBorderButtonDraw()
  
  *p\Drawing(*p, 0)
  ProcedureReturn ID
  
EndProcedure
;
Procedure FlatBorderButtonEvents(Gadget, Event, EventType)
  
  Protected *p.flat_border_button_datas
  
  If IsGadget(Gadget)
    *p = GetGadgetData(Gadget)
    If *p
      If Event = #PB_Event_Gadget
        Select EventType
          Case #PB_EventType_MouseEnter, #PB_EventType_MouseMove, #PB_EventType_LeftButtonDown, #PB_EventType_RightButtonDown
            *p\Drawing(*p, 1)
          Default
            *p\Drawing(*p, 0)
        EndSelect            
      EndIf
    EndIf
  EndIf
    
EndProcedure
;
DisableExplicit

OpenWindow(0,200,300,800,600,"TestWindow")

Font = LoadFont(#PB_Any, "Segoe UI", 9)

FlatBorderButton(1, 20, 25, 300, 25, "Printer", 0, $F0F0F0, $FFE0C2, $FF9933, Font)
FlatBorderButton(2, 20, 50, 300, 25, "Computer", 0, $F0F0F0, $FFE0C2, $FF9933, Font)
FlatBorderButton(3, 20, 75, 300, 25, "Mouse", 0, $F0F0F0, $FFE0C2, $FF9933, Font)
FlatBorderButton(4, 20, 100, 300, 25, "Keyboard", 0, $F0F0F0, $FFE0C2, $FF9933, Font)
FlatBorderButton(5, 20, 125, 300, 25, "Hard Disk", 0, $F0F0F0, $FFE0C2, $FF9933, Font)

AddKeyboardShortcut(0,#PB_Shortcut_Escape, 9999)

Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      FlatBorderButtonEvents(EventGadget(), Event, EventType())
      Select EventType()
        Case #PB_EventType_LeftClick
          Select EventGadget()
            Case 1
              Debug "Click on Printer"
            Case 2
              Debug "Click on Computer"
            Case 3
              Debug "Click on Mouse"
            Case 4
              Debug "Click on Keyboard"
            Case 5
              Debug "Click on HardDisk"
          EndSelect         
      EndSelect
      
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect
  
Until Quit > 0
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Flat button with border when hovered ?

Post by charvista »

Bisonte, that's wonderful, it works much better than my version.
I believe I will adopt it... :o
I appreciate your help!! :)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Flat button with border when hovered ?

Post by charvista »

@Bisonte, it is really awesome. :D I could not have done that by myself, as I never used prototypes before, and you use here structures and even a callback.
For our friends here, I have discovered a missing line about TextColor :o :

Code: Select all

    *p\Gadget = Gadget
    *p\Text   = Text
    *p\TextColor = TextColor ; this line was missing <<<<<<<<<<<<<
    *p\BorderColor = BorderColor
    *p\BackGroundColor = BackGroundColor
    *p\InnerColor = InnerColor
    *p\Font = Font
    *p\Drawing = @FlatBorderButtonDraw()
@Rashad: your code is not lost. It will be kept for other purposes :wink:
And I slept very well!!! :mrgreen:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Flat button with border when hovered ?

Post by Bisonte »

charvista wrote:

Code: Select all

*p\TextColor = TextColor ; this line was missing <<<<<<<<<<<<<
:oops: But now I can see that you fully understand this code :wink:
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Flat button with border when hovered ?

Post by Blue »

@Bisonte :
Very impressive code.
I never understood how to use the Prototype keyword. Now i see.
Thank you.

PS : can you tell me how to draw the original button (i. the button BEFORE it's hovered or clicked) with a border in your code ?
I've played with it quite a bit, but still can't see how i could do that.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Flat button with border when hovered ?

Post by Bisonte »

That's really simple.
The procedure FlatBorderButtonEvents() checks if an event like
#PB_EventType_MouseEnter, #PB_EventType_MouseMove, #PB_EventType_LeftButtonDown, #PB_EventType_RightButtonDown
occure. So the mouse must be over the button and I call the drawing procedure with Event = 1

So it is drawing hovered. Otherwise draw it normal.
See after StartDrawing() the Select ... Case block.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply