Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Neuen Beitrag zur Code-Library hinzufügen (https://www.delphipraxis.net/33-neuen-beitrag-zur-code-library-hinzufuegen/)
-   -   Delphi Listbox - OnMeasureItem kann nicht auf Objekt zugreifen (https://www.delphipraxis.net/133989-listbox-onmeasureitem-kann-nicht-auf-objekt-zugreifen.html)

HeinzJ 13. Mai 2009 12:08


Listbox - OnMeasureItem kann nicht auf Objekt zugreifen
 
Im Ereignis OnMeasureItem kann in einer ListBox nach dem
Hinzufügen eines neuen Items (invl. Objekt, z.B. einer Bitmap)
nicht auf das Objekt zugegriffen werden.

Mir hilfreich war Peter Below (TeamB) sowie dieser Code:

Zitat:

"Yes, this [Objekt nicht verfügbar nach Add] is an old problem caused by the fact that the AddObject method internally has to send two messages to the Windows listbox control. The first creates the item and sets it text, the second stores the object into the item. Unfortunately it is the first message (LB_ADDSTRING or LB_INSERTSTRING) that causes the WM_MEASUREITEM message to be end back, so the object has not been stored yet when the OnMeasureItem event fires. The best way to fix this is to simply ignore he OnMeasureItem event. Instead you measure the item immediately after the AddObject call and send a LB_SETITEMHEIGHT message to the listbox to tell it the height. Peter Below (TeamB)
Delphi-Quellcode:
// Aufruf z.B. nach aListbox.Add
// => ListBoxRefreshItem(aListBox, aListBox.Items.Count-1);
// oder aListBox.Items.Move(oldPos, newPos);
// => ListBoxRefreshItem(aListBox, newPos);

procedure TXYZ.ListBoxRefreshItem(Control: TWinControl; index:
    integer);
var
  lb: TListBox;
  H: integer;
begin
  lb := TListBox(Control);
  if lb.Style = lbStandard then exit;
  ListBoxMeasureItem(lb, index, H); // Siehe unten
  lb.Perform (LB_SETITEMHEIGHT, index, MAKELPARAM(H, 0));
  lb.refresh;
end;
Delphi-Quellcode:
procedure THeavyLiftEditDlg.ListBoxMeasureItem(Control: TWinControl; Index:
    Integer; var Height: Integer);
var
   lb: TListbox;
   Bitmap: TBitmap;
begin
   if Index = -1 then exit;

   lb := TListBox(Control);
   Bitmap:= TBitmap( lb.Items.Objects[Index] )

   // Measure hight according to available subitem entries.
   with lb.Canvas do
   begin

      // Measure height sample
      Font.Style := [fsBold];
      Height := TextHeight('Wg') + 2 ;
      Height := Bitmap.Height;
   end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:44 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz