RAD Studio VCL Reference
|
Occurs when the application needs to redisplay an item in a variable height owner-draw list box.
property OnMeasureItem: TMeasureItemEvent;
__property TMeasureItemEvent OnMeasureItem;
Use OnMeasureItem to write a handler to measure items to be drawn in a list box with a Style property value of lbOwnerDrawVariable.
OnMeasureItem is of type TMeasureItemEvent which contains three parameters describing the item to measure:
Control, specifying the control containing the item.
Index, specifying the index of the item in the control.
Height, specifying the height of the item.
The Index parameter identifies the position of the item in the list box.
The Height parameter should specify the height in pixels that the given item will occupy in the control. The Height parameter is passed by reference (a var parameter), which initially contains the default height of the item or the height of the item text in the control's font. The handler can set Height to a value appropriate to the contents of the item, such as the height of a graphical image to be displayed within the item.
After the OnMeasureItem event occurs, the OnDrawItem event occurs, rendering the item with the measured size.
C++ Examples:
/* Here is a typical handler for an OnMeasureItem event. The example assumes that a variable owner-draw list box already has bitmaps associated with each of its strings. It sets the height of the item to the height of the associated bitmap if that height is greater than the default height (ListBox1.ItemHeight). Note that TWinControl can also be used in place of TWidgetControl on Windows. */ void __fastcall TForm1::ListBox1MeasureItem(TWinControl *Control, int Index, int &Height) { Graphics::TBitmap *bitmap = dynamic_cast<Graphics::TBitmap *>((dynamic_cast<TListBox *>(Control))->Items->Objects[Index]); if (bitmap && bitmap->Height > Height) Height = bitmap->Height; } void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State) { Graphics::TBitmap *pBitmap; // temporary variable for item’s bitmap int Offset = 2; // default text offset width // note that we draw on the listbox’s canvas, not on the form TCanvas *pCanvas = ((TListBox *)Control)->Canvas; pCanvas->FillRect(Rect); // clear the rectangle pBitmap = dynamic_cast<Graphics::TBitmap *>((dynamic_cast<TListBox *>(Control))->Items->Objects[Index]); if (pBitmap) { pCanvas->BrushCopy(Bounds(Rect.Left + Offset, Rect.Top, pBitmap->Width, pBitmap->Height), pBitmap, Bounds(0, 0, pBitmap->Width, pBitmap->Height), clRed); // render bitmap Offset += pBitmap->Width + 4; // add four pixels between bitmap and text } // display the text pCanvas->TextOut(Rect.Left + Offset, Rect.Top, (dynamic_cast<TListBox *>(Control))->Items->Strings[Index]); } #include <memory> //for STL auto_ptr class Graphics::TBitmap *bitmap0, *bitmap1, *bitmap2; void __fastcall TForm1::FormCreate(TObject *Sender) { // Making these static keeps them around until the project terminates. static std::auto_ptr<Graphics::TBitmap> _bitmap0Cleaner(bitmap0 = new Graphics::TBitmap); static std::auto_ptr<Graphics::TBitmap> _bitmap1Cleaner(bitmap1 = new Graphics::TBitmap); static std::auto_ptr<Graphics::TBitmap> _bitmap2Cleaner(bitmap2 = new Graphics::TBitmap); bitmap0->LoadFromFile("../littleB_64.bmp"); // ImageList1->GetBitmap(0, bitmap0); ListBox1->Items->AddObject("Butterfly", bitmap0); ImageList1->GetBitmap(3, bitmap1); ListBox1->Items->AddObject("Animal", bitmap1); // no bitmap, see if it leaves this height at 64. ImageList1->GetBitmap(4, bitmap2); ListBox1->Items->AddObject("Sunflower", bitmap2); // no bitmap, see if it leaves this height at 64. }
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|