Win32 Controls: The Button |
|
Introduction to Command Buttons |
Description |
A button is a Windows control used to initiate an action. From the user’s standpoint, a button is useful when clicked, in which case the user positions the mouse on it and presses one of the mouse’s buttons. There are various kinds of buttons. The most common and most regularly used is a rectangular object. In some programming environments, the classic button is called a command button. There are other controls that can serve as click controls and initiate the same behavior as if a button were clicked. Such controls include a label, a static control, or a panel. |
To support the command button, the VCL provides a class named TButton. The TButton class is derived from TCustomButton. The TCustomButton class is derived from a class named TButtonControl, which is of TWinControl type:
Both the TCustomButton and the TButton classes are members of the StdCtrls library. To visually create a button, in the Standard section of the Tool Palette, click the TButton control and click a container. The container could be a form, a frame, etc. To programmatically get a button, create a pointer to TButton. Here is an example: //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components private: // User declarations TButton *btnSubmit; public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif To initialize the variable, specify its owner in the constructor; also specify its parent. Here is an example: //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
btnSubmit = new TButton(Form1);
btnSubmit->Parent = Form1;
}
//---------------------------------------------------------------------------
For a user, the only things important about a button are the message it displays and the action it performs. The default property of a button is the Caption: this is the word or group of words that display(s) on top of the control, showing the message that would direct the user as to what the button is used for. By default, after adding a button to a form, the Caption property of the Object Inspector has focus; this allows you to just type a caption. |
When adding a button to a form, the Caption field would have focus only if the last action you performed on the Object Inspector was for a control that does not have Caption. If the last control you were working on has a Caption property but you were setting another property, when you add a button, the Caption field would not have focus. |
The most popular captions are OK and Cancel. The OK caption is set for a form or a dialog box that informs the user of an error, an intermediary situation, or an acknowledgement of an action that was performed on the dialog that hosts the button. The Cancel caption is useful on a button whose main parent (the form) would ask a question or request a follow-up action from the user. Whenever a dialog box allows the user to dismiss it without continuing the action, you should provide a button with a Cancel caption. The easiest way to change the caption on a control, on the Object Inspector, is to click the word Caption and type the desired text. |
After adding a button to a form (by design or with code), you can change its caption with code by calling the TControl::Caption property. For example, you can change the caption of a button as follows: //---------------------------------------------------------------------------
void __fastcall TForm1::ChangeButtonCaption()
{
btnSubmit = new TButton(Form1);
btnSubmit->Parent = Form1;
btnSubmit->Caption = "Submit";
//---------------------------------------------------------------------------
The most regular action the user performs on a button is to click it. Based on this, the default event on a button is OnClick. To visually initiate the OnClick event on a button, you can double-click it. If you programmatically create the button, create a method that will handle its OnClick event. Pass a TObject pointer to the method. Here is an example: //---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
TButton *btnSubmit;
__fastcall void SubmitClicked(TObject *Sender);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Implement the method, then assign it to the OnClick property of the button. Here is an example: //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
btnSubmit = new TButton(Form1);
btnSubmit->Parent = Form1;
btnSubmit->Caption = "Submit";
btnSubmit->OnClick = SubmitClicked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SubmitClicked(TObject *Sender)
{
ShowMessage(L"The Submit button was clicked.");
}
//---------------------------------------------------------------------------
One of the most regular actions you will assign to a button is to close its parent form when the user clicks the button. There are different reasons you would want to close a form. If the form that hosts the button displays an intermediary message to the user, you should provide all the necessary code to follow up after the user has clicked the button; this is the case of property sheets or wizard pages, among others. There are various ways you can close a dialog box from a button. The simplest way is to call the TCustomForm::Close() method. This dismisses the form. To close a form, you can also use the Win32 API’s PostQuitMessage() function. This function takes one argument that is an integer. The argument could be set to almost any integer value although it should be WM_QUIT. The Button doest not have many methods to customize its behavior. This is because a button does not need more than what you would expect from it: point and click. Probably the only method you would use from a button is the constructor used to dynamically create a button. Most of the other methods used on a button are derived from member variables of the TWinControl class.
On a form or a dialog box, a button is said to be the default if its action would be activated if the user presses Enter. Such a button has a thick border. To let you specify that a button would be the default, the TCustomButton class provides a Boolean property named Default: __property bool Default = {read=FDefault,write=SetDefault}; If a form or dialog box has at least one button and you want to find out if a certain button is the default, get the value of its Default you property.
We already know that, to close a form or dialog box, you can call the TCustomForm::Close() method. Microsoft Windows provides a convenient means of closing a form or dialog box if you don't care about that changes that may have taken place on its own. The VCL supports this in a Boolean property named Cancel, which is a member of the TCustomButton class: __property bool Cancel = {read=FCancel,write=FCancel}; To specify that a button will be used to close a dialog box following the above description, set its Cancel property to True. To close the dialog box, the user can simply press the Esc key. To find out whether a button is set to cancel a dialog box, get the value of its Cancel property.
The possible values of the ModalResult property are:
If the form that is hosting the button is not the first form or dialog (in other words, if the form is accessed by a call from another form), you can use the ModalResult property to conveniently associate an action. By default, the ModalResul is set to mrNone. The ModalResult property is an integer that represents a button that the user has clicked on a dependent dialog box. To use a button as a valid integral ModalResult value, set its ModalResult property. When coding the result of the user clicking one of the buttons, call the TForm::ShowModal() method (once again, the ShowModal() method actually belongs to the TCustomForm class) and assign it the corresponding value of the TModalResult integer. The following example uses two forms. The first form has a button used to call the second. The second form has buttons with different ModalResult values. After the user clicks the button on the first form, the second would display, the program simply finds out what button was clicked, and the programmer can act accordingly: //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if( Form2->ShowModal() == mrOk ) Caption = "I am OK now"; else if( Form2->ShowModal() == mrCancel ) Caption = "Soldier, dismiss!!!"; else { Form2->Close(); Caption = "What happened? Where are you?"; } } //---------------------------------------------------------------------------
Besides, or instead of, a caption, you can display a picture on a button. To make it possible, the TCustomButton class is equipped with the Images property: __property Imglist::TCustomImageList * Images = {read=FImages,write=SetImages}; As you can see, you must first have an image list, which you can visually or programmatically create. Once you have an image list, assign it to the Images property of the button. To actually use the image(s), you have many options.
To set the picture that should display on the button, specify its index using the ImageIndex property.
To align the picture with regards to the client area of the button, you use the the ImageAlignment property, which is of type TImageAlignment: __property Stdctrls::TImageAlignment ImageAlignment = { read=FImageAlignment, write=SetImageAlignment }; The TImageAlignment enumeration has the following members: enum TImageAlignment{ iaLeft, iaRight, iaTop, iaBottom, iaCenter }; You can use a combination of the caption and the image or omit one of them. If you don't use a caption, you can use picture-only button where the image must indicate the role of the button to the user.
If you decide to display both the caption and the picture on a button, in some cases, the image may appear too far from the caption, especially if the button is big. In some other cases, mostly when the ImageAlignment property is set to a value other than iaCenter, the image may appear too close to the border of the button, up to touching the border and consequently hiding part of the image. If you want, you can change the margin of the image. To let you specify the margin amount, the TCustomButton class provides the ImageMargins property that is of type TImageMargins: __property Stdctrls::TImageMargins * ImageMargins = { read=FImageMargins, write=SetImageMargins }; TImageMargins is a simple class derived from TPersistent. It is only equipped with four properties named Bottom, Left, Right, Top, and an event named OnChange. Each of the properties is of type int. To specify a margin amount for the picture of a button, access the desired TImageMargins property from the ImageMargins property of the button and assign the desired value:
Microsoft Windows Vista introduced new features to command buttons. It makes it possible for a button to appear with either a link or a menu, increasing its usefulness and aesthetics. This is done through a style. To let you specify the style of a button, the TCustomButton class is equipped with the Style property, which is of type TButtonStyle. It gives you three options. The buttons we have used so far are referred to as push buttons. To support this type, the TButtonStyle enumeration has the bsPushButton member. This the default value of the Style property. To use another style, access the Style property of the button and and change its value. |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|