TButton / TBtnBmp
Standard push buttons and bitmap buttons for dialogs and toolbars
TButton
Fonte: 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
TButton Properties
| Property | Type | Description |
|---|---|---|
bAction | Block | Code block executed when the button is clicked |
lDefault | Logical | If .T., this is the default button (Enter key activates it) |
lCancel | Logical | If .T., this is the cancel button (Escape key activates it) |
cCaption | String | Text displayed on the button face |
lProcessing | Logical | Internal reentrancy guard -- prevents double-clicks from firing the action twice |
TButton Methods
| Method | Description |
|---|---|
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
TBtnBmp
Fonte: 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
| Property | Type | Description |
|---|---|---|
cResName | String | Resource name for the button bitmap |
cBmpFile | String | Path to an external BMP/PNG file for the button image |
bAction | Block | Code block executed on click |
lBtnUp | Logical | Current pressed state (internal) |
lGroup | Logical | If .T., starts a new group of toolbar buttons |
lPressed | Logical | For toggle-style buttons, indicates the toggled-on state |
cToolTip | String | Tooltip text shown on mouse hover |
lFlat | Logical | Flat 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:
| State | Constant | Position | Description |
|---|---|---|---|
| Normal | BTN_UP | 1 | Default appearance when the button is idle and enabled |
| Pressed | BTN_DOWN | 2 | Appearance while the user clicks or holds the mouse button down |
| Disabled | BTN_DISABLE | 3 | Appearance when the button is inactive (lActive := .F.) |
| Hover | BTN_OVER | 4 | Appearance 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:
lGDIP-- Enables GDI+ alpha-blended rendering, providing anti-aliased edges and smooth transparency for PNG and 32-bit BMP bitmaps.bColorMap-- A codeblock for dynamic color remapping on the button bitmap, allowing per-state or per-button color substitution (e.g., changing icon color for active vs. inactive states).bAlphaLevel-- A codeblock that returns a custom alpha value (0-255) per state, enabling granular transparency control for each button state independently.
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
- Use
&in the PROMPT to define an accelerator key:"&Save"makes Alt+S activate the button. - Only one button per dialog should be
DEFAULTand one should beCANCEL. - For modern toolbar looks, use the
2015style onDEFINE BUTTONBAR. - TBtnBmp supports PNG files with alpha transparency for high-quality icons.
- Use the
GROUPclause to insert a visual separator between logical groups of toolbar buttons.