TButton / TBtnBmp

Standard push buttons and bitmap buttons for dialogs and toolbars

TButton

Fuente: source/classes/button.prg  |  Parent: TControl

TButton wraps the standard Windows push button control. It is the most basic interactive element -- a labeled rectangle that executes an action when clicked. Buttons can be designated as the default button (activated by Enter) or the cancel button (activated by Escape).

Class Hierarchy

graph TD TWindow --> TControl TControl --> TButton TControl --> TBtnBmp subgraph "Button Family" TButton TBtnBmp end subgraph "Containers" TDialog -- "contains" --> TButton TDialog -- "contains" --> TBtnBmp TWindow -- "contains" --> TButton TButtonBar -- "contains" --> TBtnBmp end style TButton fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3 style TBtnBmp fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3

TButton Properties

PropertyTypeDescription
bActionBlockCode block executed when the button is clicked
lDefaultLogicalIf .T., this is the default button (Enter key activates it)
lCancelLogicalIf .T., this is the cancel button (Escape key activates it)
cCaptionStringText displayed on the button face
lProcessingLogicalInternal reentrancy guard -- prevents double-clicks from firing the action twice

TButton Methods

MethodDescription
New( nRow, nCol, cCaption, oWnd, bAction, nWidth, nHeight, ... )Constructor. Creates a new button at the given position.
ReDefine( nId, bAction, oWnd, ... )Redefine a button from a dialog resource (RC file).
Click()Programmatically trigger the button's action.
SetFocus()Move keyboard focus to this button.

TButton Command Syntax

@ nRow, nCol BUTTON oBtn PROMPT "text" ;
   [ OF oWnd ] ;
   [ SIZE nWidth, nHeight ] ;
   [ ACTION codeblock ] ;
   [ DEFAULT ] ;
   [ CANCEL ] ;
   [ FONT oFont ] ;
   [ WHEN bWhen ]

Button State Machine

stateDiagram-v2 [*] --> Normal Normal --> Focused : Tab / SetFocus() Focused --> Pressed : Click / Space / Enter(default) Pressed --> ActionExecuted : bAction fires ActionExecuted --> Normal : Action complete Focused --> Normal : Tab away note right of Pressed lProcessing = .T. Prevents re-entrancy end note

TBtnBmp

Fuente: source/classes/btnbmp.prg  |  Parent: TControl

TBtnBmp is an owner-drawn bitmap button that supports multiple visual states (normal, pressed, mouse-over, disabled). It is the button type used in toolbars (TButtonBar) and anywhere you need graphical buttons with icons.

TBtnBmp Properties

PropertyTypeDescription
cResNameStringResource name for the button bitmap
cBmpFileStringPath to an external BMP/PNG file for the button image
bActionBlockCode block executed on click
lBtnUpLogicalCurrent pressed state (internal)
lGroupLogicalIf .T., starts a new group of toolbar buttons
lPressedLogicalFor toggle-style buttons, indicates the toggled-on state
cToolTipStringTooltip text shown on mouse hover
lFlatLogicalFlat appearance (border only on hover)

TBtnBmp Command Syntax

@ nRow, nCol BTNBMP oBtn ;
   [ OF oWnd ] ;
   [ SIZE nWidth, nHeight ] ;
   [ ACTION codeblock ] ;
   [ FILENAME cBmpFile | RESOURCE cResName ] ;
   [ TOOLTIP cText ] ;
   [ FLAT ] ;
   [ PROMPT cText ] ;
   [ WHEN bWhen ]

4-State Bitmap System

TBtnBmp supports four distinct visual states, each with its own bitmap. The button automatically switches between them based on user interaction:

StateConstantPositionDescription
NormalBTN_UP1Default appearance when the button is idle and enabled
PressedBTN_DOWN2Appearance while the user clicks or holds the mouse button down
DisabledBTN_DISABLE3Appearance when the button is inactive (lActive := .F.)
HoverBTN_OVER4Appearance when the mouse cursor hovers over the button

For resource-based buttons, bitmap IDs follow Borland conventions: the normal bitmap uses the base ID, the pressed bitmap uses base_id + 10000, the disabled bitmap uses base_id + 20000, and the hover bitmap uses base_id + 30000. File-based buttons use four separate files or a multi-image strip, specified via the FILENAME clause.

Advanced Rendering Options

Additional visual enhancements are available through TBtnBmp DATA members:

BUTTONBAR (Toolbar)

The DEFINE BUTTONBAR command creates a TButtonBar container that hosts TBtnBmp buttons as a toolbar at the top of a window.

DEFINE BUTTONBAR oBar OF oWnd SIZE nBtnW, nBtnH [ 2007 | 2010 | 2013 | 2015 ]

DEFINE BUTTON oBtn1 OF oBar ;
   RESOURCE "NEW" ;
   TOOLTIP "New record" ;
   ACTION NewRecord()

DEFINE BUTTON oBtn2 OF oBar ;
   RESOURCE "EDIT" ;
   TOOLTIP "Edit record" ;
   ACTION EditRecord()

DEFINE BUTTON oBtn3 OF oBar ;
   RESOURCE "PRINT" ;
   TOOLTIP "Print report" ;
   ACTION PrintReport() ;
   GROUP                      // visual separator before this button

Code Examples

Example 1: Standard Dialog Buttons

#include "FiveWin.ch"

FUNCTION TestButtons()
   LOCAL oDlg, oBtn1, oBtn2

   DEFINE DIALOG oDlg TITLE "Button Demo" SIZE 400, 250

   @ 2, 2 BUTTON oBtn1 PROMPT "&Save" ;
      OF oDlg SIZE 80, 25 ;
      ACTION ( MsgInfo( "Saved!" ), oDlg:End() ) ;
      DEFAULT

   @ 2, 12 BUTTON oBtn2 PROMPT "&Cancel" ;
      OF oDlg SIZE 80, 25 ;
      ACTION oDlg:End() ;
      CANCEL

   // A button with a WHEN clause -- disabled until data is valid
   @ 4, 2 BUTTON PROMPT "&Delete" ;
      OF oDlg SIZE 80, 25 ;
      ACTION DeleteRecord() ;
      WHEN ( !Empty( cRecId ) )

   ACTIVATE DIALOG oDlg CENTERED
RETURN NIL

Example 2: Bitmap Buttons

#include "FiveWin.ch"

FUNCTION TestBmpButtons()
   LOCAL oWnd, oBtn

   DEFINE WINDOW oWnd TITLE "Bitmap Button Demo"

   // Bitmap button from a file
   @ 2, 2 BTNBMP oBtn OF oWnd ;
      SIZE 120, 40 ;
      FILENAME "..\bitmaps\save.bmp" ;
      PROMPT "Save" ;
      ACTION MsgInfo( "Saving..." ) ;
      FLAT

   // Bitmap button from a resource
   @ 5, 2 BTNBMP OF oWnd ;
      SIZE 120, 40 ;
      RESOURCE "PRINT" ;
      PROMPT "Print" ;
      ACTION PrintReport() ;
      TOOLTIP "Print current report"

   ACTIVATE WINDOW oWnd
RETURN NIL

Example 3: Complete Toolbar

#include "FiveWin.ch"

FUNCTION Main()
   LOCAL oWnd, oBar

   DEFINE WINDOW oWnd TITLE "Application with Toolbar"

   DEFINE BUTTONBAR oBar OF oWnd SIZE 56, 56 2015

   DEFINE BUTTON OF oBar ;
      RESOURCE "NEW"   TOOLTIP "New"   ACTION NewDoc()
   DEFINE BUTTON OF oBar ;
      RESOURCE "OPEN"  TOOLTIP "Open"  ACTION OpenDoc()
   DEFINE BUTTON OF oBar ;
      RESOURCE "SAVE"  TOOLTIP "Save"  ACTION SaveDoc()

   DEFINE BUTTON OF oBar ;
      RESOURCE "PRINT" TOOLTIP "Print" ACTION PrintDoc() GROUP

   DEFINE BUTTON OF oBar ;
      RESOURCE "HELP"  TOOLTIP "Help"  ACTION HelpDoc() GROUP

   SET MESSAGE OF oWnd TO "Ready" CENTERED

   ACTIVATE WINDOW oWnd
RETURN NIL

Tips

Ver También