![]() |
|
#1
|
|||
|
|||
|
Hi folks, next question:
I'm building a component based on TListView, and now I have to know how I can define the height of each item like in TListBox with OnMeasureItem. THX, Obeliks *_______________ | | | | | | |_______________| |
|
#2
|
|||
|
|||
|
try this peace of code I found:
This is not hard depending on the complexity of your DRAWITEM and MEASUREITEM. First, subclass from TCustomListView. Override CreateParams to add the LVS_OWNERDRAWFIXED to the style: procedure TODListView.CreateParams(var Params: TCreateParams); begin inherited; with Params do begin Style := Style or LVS_OWNERDRAWFIXED; end; end; Then provide a handler for CN_MEASUREITEM and CN_DRAWITEM: procedure CNMeasureItem(var Message: TWMMeasureItem); message CN_MEASUREITEM; procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM; Finally, go crazy trying to implement these two. procedure TODListView.CNMeasureItem(var Message: TWMMeasureItem); begin // inherited; FCanvas.Font := Font; Message.MeasureItemStruct^.ItemHeight := FCanvas.TextHeight('Xy'); Message.Result := 1; end; procedure TODListView.CNDrawItem(var Message: TWMDrawItem); begin // inherited; DrawItem(Message.DrawItemStruct^); Message.Result := 1; end; Also, a hint: The ItemData of the DrawItemStruct in the message passed to CNDrawItem is a TListItem. procedure TODListView.DrawItem(const DrawItemStruct: TDrawItemStruct); var item: TListItem; begin with DrawItemStruct do begin item := TListItem(ItemData); ... end; Greetz, ::TeD On The NeT:: =[ QUESTIONS ]========================================= ![]() ================================================== ===== =[ EXTRA INFO ]======================================== Location : Netherlands (GMT +01.00) Languages : English, Dutch, German Age : 25 ICQ : Be smart and figure out what my UIN is... ================================================== ===== |
|
#3
|
|||
|
|||
|
I also found this peace of C++ builder code, which should be ported (shouldn't be to hard..):
==== That shouldn't be happening, as item's display rectangle is handled by Windows -- i.e., no WM_MEASUREITEM message is sent. Try this API approach, and see if the results differ... //in header... void __fastcall WMDrawItem(TMessage &Msg); BEGIN_MESSAGE_MAP MESSAGE_HANDLER(WM_DRAWITEM, TMessage, WMDrawItem) END_MESSAGE_MAP(TForm) //in source... __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DWORD dwStyle = GetWindowLong(ListView1->Handle, GWL_STYLE); SetWindowLong(ListView1->Handle, GWL_STYLE, dwStyle | LVS_OWNERDRAWFIXED); } void __fastcall TForm1::WMDrawItem(TMessage &Msg) { LPDRAWITEMSTRUCT lpdis = (DRAWITEMSTRUCT *)Msg.LParam; if (lpdis->hwndItem != ListView1->Handle) { TForm: ispatch(&Msg);return; } TCanvas *LVCanvas = new TCanvas(); LVCanvas->Handle = lpdis->hDC; LVCanvas->Font = ListView1->Font; TRect Rect = lpdis->rcItem; TListItem *Item = ListView1->Items->Item[lpdis->itemID]; TOwnerDrawState State = TOwnerDrawState(); if (lpdis->itemState & ODS_SELECTED) State = State << odSelected; if (lpdis->itemState & ODS_FOCUS) State = State << odFocused; if (lpdis->itemState & ODS_GRAYED) State = State << odGrayed; if (lpdis->itemState & ODS_DISABLED) State = State << odDisabled; if (lpdis->itemState & ODS_CHECKED) State = State << odChecked; if (State.Contains(odSelected)) { LVCanvas->Font->Color = clHighlightText; LVCanvas->Brush->Color = clHighlight; } RECT rcItem, rcIcon; ListView_GetItemRect(ListView1->Handle, Item->Index, &rcItem, LVIR_LABEL); ListView_GetItemRect(ListView1->Handle, Item->Index, &rcIcon, LVIR_ICON); TRect ItemRect = (TRect)rcItem; TRect IconRect = (TRect)rcIcon; LVCanvas->TextRect(ItemRect, ItemRect.Left, ItemRect.Top, Item->Caption); if (State.Contains(odFocused)) LVCanvas->DrawFocusRect(ItemRect); ListView1->SmallImages->Draw(LVCanvas, IconRect.Left, IconRect.Top, Item->ImageIndex); LVCanvas->Handle = 0; delete LVCanvas; Msg.Result = true; } Greetz, ::TeD On The NeT:: =[ QUESTIONS ]========================================= ![]() ================================================== ===== =[ EXTRA INFO ]======================================== Location : Netherlands (GMT +01.00) Languages : English, Dutch, German Age : 25 ICQ : Be smart and figure out what my UIN is... ================================================== ===== |
|
#4
|
|||
|
|||
|
Sounds good, these really seems as it could work...
But already my first problem: When compiling an error occurs that LVS_OWNERDRAWFIXED is unknown... Did you forget to tell me a unit? Or did I forget to think? And btw, doesn't it have to be LVS_OWNDERDRAWVARIABLE? I mean, this style exists in TListBox and I WANT to create items with different heights... Please help me, as I'd really like to write this component ![]() So long, Obeliks *_______________ | | | | | | |_______________| |
|
#5
|
|||
|
|||
|
I forgot to send you my reply. You can find this variable in the COMMCTRL unit. add it to your uses clause. that should fix your problem.
Greetz, ::TeD On The NeT:: =[ QUESTIONS ]========================================= ![]() ================================================== ===== =[ EXTRA INFO ]======================================== Location : Netherlands (GMT +01.00) Languages : English, Dutch, German Age : 25 ICQ : Be smart and figure out what my UIN is... ================================================== ===== |
|
#6
|
|||
|
|||
|
Yeah, thanks man, but too late *g* Posted already another thread...
But I've got another problem, as I'm a little bit unfamiliar with this stuff... You told me to call DrawItem(Message.DrawItemStruct^). But DrawItem is the function of TListView, so it wants as parameters a TListItem, a TRect and a TOwnerDrawState... I tried to use the following: with Message.DrawItemStruct^ do DrawItem(TListItem(ItemData),rcItem,S); as I wasnt capable to convert itemState to TOwnerDrawState I tried to use a variable S: TOwnerDrawState, check for every possibility (e.g. ODS_SELECTED) and add it to S. But I didn't figure it out how to check if e.g. ODS_SELECTED was in itemState... How can I do this, or better, how can I convert itemState to TOwnerDrawState? THX, Obeliks PS: I'll accept your answer now, but please keep helping me ![]() *_______________ | | | | | | |_______________| |
|
#7
|
|||
|
|||
|
You could also contact me per ICQ or per eMail...
UIN: 65632051 (click here: http://web.icq.com/whitepages/add_me/1,,,00.icq?uin=65632051&action=add) MAIL: Delphi@obeliks.de Please add me first to the contact list, and wait for me adding you as I block all msgs not coming from someone on my contact list... (Damned spam :/ ) So long, Obeliks *_______________ | | | | | | |_______________| |
![]() |
| Thread Tools | |
| Display Modes | |
|
|