Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 03-21-2002, 07:24 AM
Obeliks Obeliks is offline
Senior Member
 
Join Date: Aug 2000
Posts: 139
Default Defining height for an item in TListView??

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

*_______________
| |
| member of the |
| crazy |
| coders |
| club |
|_______________|
Reply With Quote
  #2  
Old 03-24-2002, 11:15 AM
Ted On The NeT Ted On The NeT is offline
Senior Member
 
Join Date: Apr 2001
Posts: 2,182
Default RE: Defining height for an item in TListView??

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 ]=========================================
don't forget to accept this reply if it helped you!
================================================== =====

=[ EXTRA INFO ]========================================
Location : Netherlands (GMT +01.00)
Languages : English, Dutch, German
Age : 25
ICQ : Be smart and figure out what my UIN is...
================================================== =====
Reply With Quote
  #3  
Old 03-24-2002, 11:16 AM
Ted On The NeT Ted On The NeT is offline
Senior Member
 
Join Date: Apr 2001
Posts: 2,182
Default RE: Defining height for an item in TListView??

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 ]=========================================
don't forget to accept this reply if it helped you!
================================================== =====

=[ EXTRA INFO ]========================================
Location : Netherlands (GMT +01.00)
Languages : English, Dutch, German
Age : 25
ICQ : Be smart and figure out what my UIN is...
================================================== =====
Reply With Quote
  #4  
Old 03-25-2002, 02:12 AM
Obeliks Obeliks is offline
Senior Member
 
Join Date: Aug 2000
Posts: 139
Default RE: Defining height for an item in TListView??

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

*_______________
| |
| member of the |
| crazy |
| coders |
| club |
|_______________|
Reply With Quote
  #5  
Old 03-26-2002, 01:53 AM
Ted On The NeT Ted On The NeT is offline
Senior Member
 
Join Date: Apr 2001
Posts: 2,182
Default RE: Defining height for an item in TListView??

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 ]=========================================
don't forget to accept this reply if it helped you!
================================================== =====

=[ EXTRA INFO ]========================================
Location : Netherlands (GMT +01.00)
Languages : English, Dutch, German
Age : 25
ICQ : Be smart and figure out what my UIN is...
================================================== =====
Reply With Quote
  #6  
Old 03-26-2002, 02:10 AM
Obeliks Obeliks is offline
Senior Member
 
Join Date: Aug 2000
Posts: 139
Default RE: Defining height for an item in TListView??

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

*_______________
| |
| member of the |
| crazy |
| coders |
| club |
|_______________|
Reply With Quote
  #7  
Old 03-26-2002, 02:24 AM
Obeliks Obeliks is offline
Senior Member
 
Join Date: Aug 2000
Posts: 139
Default RE: Defining height for an item in TListView??

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

*_______________
| |
| member of the |
| crazy |
| coders |
| club |
|_______________|
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:44 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.