Buttons Get New Features

You might think that the classic Windows push buttons are well established, stable controls. That's actually not true. Since Windows XP, you can hook an image from an image list to a button, and have a graphical bitmap button without having to derive a custom owner-drawn control as Delphi did since the early days with the BitBtn (bitmap button) control. In Delphi 2009 you can now have the same graphical effect with a plain and standard TButton. Image list support comes through a series of properties you can use to determine which image to use in each of various states of the button. Here is the list of the new image-related properties of the TCustomButton class, listed with only their types:

property Disabledlmagelndex: TImagelndex ... property Hotlmagelndex: TImagelndex ... property ImageAlignment: TImageAlignment ... property ImageIndex: TImageIndex ... property ImageMargins: TImageMargins ... property Images: TCustomImageList ... property PressedImageIndex: TImageIndex ... property SelectedImageIndex: TImageIndex ...

Since this feature was introduced in the Win32 API in Windows XP, if your application needs to run on Windows 2000, you should use it with care or avoid using it altogether.

Similarly, if your program is meant to be running on Vista, you can activate more new features, like the command link style used by many standard dialogs of the operating system and split button styles that let you hook a drop down menu to the button, which is activated by pressing the small drop down arrow. The overall layout of the button is determined by the value of the new Style property of an enumerated type defined as a nested type114 of the TCustomButton class:

type

TButtonStyle = (bsPushButton, bsCommandLink, bsSplitButton);

There are further properties you can use depending on the selected style:

  • With the split button style (in the API, the BS_SPLITBUTTON style value) you can use the DropDownMenu property (of type TPopupMenu) and customize it in the OnDropDownClick event.
  • In the case of the command link type (the bs_commandlink style value in the API) you can use the default icon (a green arrow) or a specific image (as mentioned earlier) and provide more information about the action with the new CommandLinkHint string property.

Finally, the ElevationRequired property, applicable both to a standard button and to a command link one, enables the display of the Windows shield to be used if the button leads to a UAC protected operation. The ElevationRequired property sends the BCM_SETSHIELD message to the button.

Using all of these new properties can affect the layout of your application quite radically, although you can obtain some of these user interface effects only if the application runs on Windows Vista (or later versions of the operating system). These properties are not very complex to use, so rather than describing the ButtonsDemo example in detail, I'll simply list its key elements, after showing you the design-time form:

114 Nested types, introduced in Delphi 2006, let you define a type within an existing type. A nested type is subject to the visibility rules determined by the class hosting it, and can be private or public. In this case the type TCustomButton .TButtonStyl e is public. Nested types provide a sort of name space, as the full name includes the outer class. For detailed coverage see, again, the "Delphi 2007 Handbook".

Delphi Glow Button

This is the summary of the DFM file of the project:

object FormButtonsDemo: TFormButtonsDemo object Buttonl: TButton ImageIndex = 0 Images = ImageListl PressedImageIndex = 1 end object Button2: TButton ImageIndex = 1 Images = ImageListl PressedImageIndex = 2 end object Button3: TButton DropDownMenu = PopupMenul Style = bsSplitButton end object Button4: TButton

CommandLinkHint = 'This is a command link hint' Style = bsCommandLink end object Button5: TButton

CommandLinkHint = 'Another hint' ImageIndex = 1 Images = ImageListl Style = bsCommandLink end object Button6: TButton

ElevationRequired = True Style = bsCommandLink end object ImageList1: TImageList... object PopupMenu1: TPopupMenu... end

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment