Disegnare un Button contenente un'icona e un testo mediante la DrawingArea

Da Gambas-it.org - Wikipedia.

Molte volte si ha la necessità di posizionare immagine e testo al centro - o in altre posizioni - in un Button. Non è possibile, però, ottenere questo effetto con le risorse della Classe Button. Gambas fornisce almeno un paio di possibilità, che mostriamo di seguito, utilizzando come base una DrawingArea, per ottenere quel risultato.


Uso del Metodo .Style.Button( ) della Classe Draw

Questa modalità prevede l'uso dell Metodo .Style.Button( ) della Classe Draw [ Nota 1 ].

Mostriamo un semplice esempio:

Private da As DrawingArea
Private bo As Boolean


Public Sub Form_Open()
 
 With da = New DrawingArea(Me) As "DrawingArea1"
   .X = 200
   .Y = 200
   .W = 100
   .H = 80
 End With
  
End


Public Sub DrawingArea1_Draw()
 
 Dim pc As Picture
 Dim testo As String
 
  pc = Stock["32/monitor"]
  testo = "Testo qualsiasi"
  
  With Draw
    .Style.Button(0, 0, da.W, da.H, bo, 0, False)
    .Picture(pc, (.W - pc.W) / 2, 10)
    .Text(testo, (.W - .TextWidth(testo)) / 2, 50)
  End With
 
  bo = Not bo
  
End


Public Sub DrawingArea1_MouseDown()
  
  Print "Tasto premuto";
   
  da.Refresh
  
End


Public Sub DrawingArea1_MouseUp()
   
  Print " e rilasciato"
   
  da.Refresh
  
End


Uso del Metodo .PaintButton( ) della Classe Style

Questa modalità [ Nota 2 ], più complessa della precedente, fa uso del Metodo .PaintButton( ) della Classe Style.


In questo esempio partico verrà fra l'altro utilizzata un'apposita Classe specifica, chiamata SquareButton.class, per creare alcuni tasti sul Form, che sarà la seguente:

Inherits UserControl

Public Const _Properties As String = "*,Text,Picture"
Public Const _DefaultEvent As String = "Click"
Public Const _DefaultSize As String = "8,8"
Public Const _Similar As String = "Button"

Event Click

Property Picture As Picture
Property Text As String

Private $hPicture As Picture
Private $sText As String
Private $hDA As DrawingArea
Private $bState As Boolean
 
 
Public Sub _new()
 
 $hDA = New DrawingArea(Me) As "da"  
 Me.Proxy = $hDA  
 
End


Private Function Picture_Read() As Picture
 
 Return $hPicture
  
End


Private Sub Picture_Write(Value As Picture)
 
 $hPicture = Value
 
End


Private Function Text_Read() As String
 
 Return $sText
 
End


Private Sub Text_Write(Value As String)
 
 $sText = Value
 
End


Public Sub da_Draw()
 
 Dim hImage As New Image
 Dim iFlag As Integer = Style.StateOf(Me)
 
  If Me.W > Me.H Then
    Me.H = Me.W
  Else
    Me.W = Me.H
  Endif
  
  Style.PaintButton(0, 0, $hDA.W, $hDA.H, $bState, iFlag, False)
   
  If $hPicture Then
    hImage = $hPicture.Image.Stretch(Me.W / 2, Me.H / 2)
    If $sText Then      
      Paint.DrawImage(hImage, (Me.W - hImage.W) / 2, (Me.H / 2) - (hImage.H - (Desktop.Scale)))
      Draw.RichText(Html($sText), 0, 0, Me.W, Me.H + (Me.H / 2), Align.Center)
    Else      
      Paint.DrawImage(hImage, (Me.W - hImage.W) / 2, (Me.H - hImage.H) / 2)
    Endif  
  Else
    Draw.RichText(Html($sText), 0, 0, Me.W, Me.H, Align.Center)
  Endif
  
End


Public Sub da_MouseDown()
   
  $bState = True
  $hDA.Refresh    
  Raise Click
  
End


Public Sub da_MouseUp()
   
  $bState = False
  $hDA.Refresh
  
End


Public Sub da_Enter()
 
  $hDA.Refresh
  
End
 
 
Public Sub da_Leave()
 
  $hDA.Refresh
  
End


Public Sub da_GotFocus()
 
  $hDA.Refresh
  
End


Public Sub da_LostFocus()
 
  $hDA.Refresh
   
End

Tale codice genera nella casella degli oggetti dell'IDE un nuovo widget, chiamato SquareButton. Sarà necessario posizionare, nelle modalità consuete, quattro oggetti del tipo SquareButton sul Form principale.


Il codice presente nella Classe principale FMain.class sarà, invece, il seguente:

Public Sub SquareButton1_Click()
 
 Print "Il pulsante uno è stato premuto"
 
End


Public Sub SquareButton2_Click()
 
 Print "Il pulsante due è stato premuto"
  
End


Public Sub SquareButton3_Click()
 
 Print "Il pulsante tre è stato premuto"
  
End


Public Sub SquareButton4_Click()
 
 Print "Il pulsante quattro è stato premuto"
  
End



Note

[1] La Classe Draw, anche se ancora funzionante, è considerata obsoleta.

[2] Il codice è stato scritto dal membro Gianluigi del forum www.gambas-it.org .