TBitBtnCopyImage (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses a special 4-in-1 64X16 bitmap for a TBitBtn. Such a bitmap can be created using an image editor. As stated in the Help for the TBitBtn class or TBitBtn Glyph property, this bitmap supplies images for the Up, Disabled, Clicked and Down button states (TButtonState) for this TBitBtn.

Notice that the left side of the arrow turns red when you click and hold on the button. Also, the arrow turns grey with a red stripe when you click BitBtn2 to disable BitBtn1. The bitmap can be installed on the TBitBtn using the Glyph property or the CopyImage method. CopyImage allows you to use graphics other than a bitmap.

The BitBtn1 properties have to be assigned at run time because is a descendant of TBitBtn. I did this to be able to use CopyImage, which is a protected method. Configuring the imagelist is usually done at design time, but for those not building the code project, the code is here and not hidden in design work.

Code

TForm1 *Form1;
myBitBtn *BitBtn1;
TImageList *imList;
Graphics::TBitmap *bmap;
TIcon *ico;

__fastcall myBitBtn::myBitBtn(TComponent* Owner)
	: TBitBtn(Owner)
{
}

__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
  BitBtn1 = new myBitBtn(this);
  BitBtn1->Parent = this;
  BitBtn1->Visible = True;
  BitBtn1->Caption = "BitBtn1";
  BitBtn1->Height = 137;
  BitBtn1->Width = 185;
  BitBtn1->Top = 100;
  BitBtn1->Left = 100;
  BitBtn1->OnClick = BitBtn1Click;
  imList = new TImageList(this);
  imList->Height = 32;
  imList->Width = 128;
  BitBtn1->Images = imList;
  bmap = new Graphics::TBitmap();
  ico = new TIcon();
  ico->Height = 32;
  ico->Width = 128;

//	bmap->LoadFromFile("MYARROW2D.bmp");
  ico->LoadFromFile("../../MYARROW2D32.ico");
  imList->AddIcon(ico);
//  BitBtn1->Glyph = bmap;
  BitBtn1->CallCopyImage(imList, 0);
  BitBtn1->NumGlyphs = 4;
//  Repaint();
}
//---------------------------------------------------------------------------;

// CopyImage is a protected method, so myBitBtn is a descendant of TBitBtn so that I can access it.
void __fastcall myBitBtn::CallCopyImage(Imglist::TCustomImageList* ImageList, int Index)
{
  CopyImage(ImageList, Index);
}

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
  switch (random(4))
  {
	case 0: BitBtn1->Layout = blGlyphLeft; break;
	case 1: BitBtn1->Layout = blGlyphRight; break;
	case 2: BitBtn1->Layout = blGlyphTop; break;
	case 3: BitBtn1->Layout = blGlyphBottom; break;
	default: ShowMessage("Unhandled case");
  }
//  BitBtn1->CopyImage(imList, RNum);
}

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
  BitBtn1->Enabled = !BitBtn1->Enabled;
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  delete bmap;
  delete ico;
}

Uses