Board index » delphi » Help w/ TListView.OnAdvancedCustomDrawSubItem

Help w/ TListView.OnAdvancedCustomDrawSubItem

I am using a TListView in vsReport mode to display some info. the
listview has two columns, the first displays a person's name, the second
displays some text regarding the person. The first 3 charachters of the
info represent the status of the person. If these chharachters match a
given string I want to display them in clBlue with the following
characters in clWindowText. I was originally using a TListBox for this
and everything was working perfectly but i would much rather use a
listview. Here is a simplified version of my code:

//OnAdvancedCustomDrawSubItem procedure...
var
  Text,
  Status: string;
  X: integer;
  Y: integer;
begin
  //x = displayrect.left
  //y = displayrect.top;
  //Text = SubItems string

  Status := Copy(Text,1,3);
  if (Status = 'XXX') then
  with Sender.Canvas do
  begin
    Font.Color := clBlue;
    TextOut(X,Y,Status);
    Inc(X,TextWidth(Status));
    Font.Color := clWindowText;
    TextOut(X,Y,Copy(Text,4,Length(Text)));
  end;
end;

I have tried various approaches but the only effect i get is changing
the Font color for the entire subitem instead of only the first word. I
also tried creating a tbitmap and then drawing on that, then painting
the bitmap onto the canvas but maybe i'm not doing it right. Also, i am
aware that i need the OnAdvancedDrawItem event to be assigned before the
DrawSubItem event is fired. All of that appears to be in order. I just
can't manage to draw to the canvas using two different font colors.

Any help would be greatly appreciated.

=)

Jellowe

--

"I suppose everything in existence takes its color
from the average hue of our surroundings."

        - H.G. Wells

 

Re:Help w/ TListView.OnAdvancedCustomDrawSubItem


Quote
Jellowe wrote in message <3CB21CE4.F96DC...@pirchworld.com>...
>I am using a TListView in vsReport mode to display some info. the
>listview has two columns, the first displays a person's name, the second
>displays some text regarding the person. The first 3 charachters of the
>info represent the status of the person. If these chharachters match a
>given string I want to display them in clBlue with the following
>characters in clWindowText. ...

Apart from the whole scary ownerdraw stuff, it sounds
like you have a status and a description compressed
into a single field. You're (probably) free to store
your data any way you like, but couldn't you render it
as three columns?

Take a step back, look at the bigger picture.

Groetjes,
Maarten Wiltink

Re:Help w/ TListView.OnAdvancedCustomDrawSubItem


Yes, i could use three columns, would be easy enough, but i also want to
highlight certain words within the columns' captions using clBlue. If i
can figure out how to draw on the listview's canvas using more than one
font color i can handle the rest. I have done lots of owner draw stuff
using listboxes and have also created a custom component which does this
(a syntax highlighting editor of sorts). The listview is really really
strange though. It doesn't work the same way  =(

Quote
Maarten Wiltink wrote:

> Apart from the whole scary ownerdraw stuff, it sounds
> like you have a status and a description compressed
> into a single field. You're (probably) free to store
> your data any way you like, but couldn't you render it
> as three columns?

> Take a step back, look at the bigger picture.

> Groetjes,
> Maarten Wiltink

--

"I suppose everything in existence takes its color
from the average hue of our surroundings."

        - H.G. Wells

Re:Help w/ TListView.OnAdvancedCustomDrawSubItem


In article <3CB21CE4.F96DC...@pirchworld.com>, Jellowe <jell...@pirchworld.com>
writes:

Quote
>  if (Status = 'XXX') then
>  with Sender.Canvas do
>  begin

Are you sure you are attempting to draw on the correct Canvas, and not the
Form's canvas. I don't have the reference to OnAdvancedCustomDrawSubItem (I
have D3) but in my experience a Sender is usually a TObject and must be
typecast to get a Canvas.

The fact that you did what you wanted on a TListBox shows that the detail
approach of your code is essentailly correct.

Alan Lloyd
alangll...@aol.com

Re:Help w/ TListView.OnAdvancedCustomDrawSubItem


Yeah, that's just my pseudo-cod. The sender is the TCustomListView
itself. I'm guessing that the listview paints its items a number of
times or something, or in stages, but i messed around with the "Stage"
param but to no avail. If you try this with a listbox and a listview
you'll see what i mean:

// The text ends up all blue here
// i played around with the "Stage" param too, no good.
procedure TForm1.ListView1AdvancedCustomDrawSubItem(
  Sender: TCustomListView; Item: TListItem; SubItem: Integer;
  State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
begin
  with (Sender as TListView).Canvas do
  begin
    Font.Color := clBlue;
    TextOut(50,50,'Hey');
    Font.Color := clRed;
    TextOut(80,50,'Hey');
  end;
end;

procedure TForm1.ListView1AdvancedCustomDrawItem(Sender:
TCustomListView;
  Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
begin
//
end;

// different font colors are painted here
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListBox) do
  begin
    Style := lbOwnerDrawFixed;
    Canvas.Font.Color := clBlue;
    Canvas.TextOut(50,50,'Hey');
    Canvas.Font.Color := clRed;
    Canvas.TextOut(80,50,'Hey');
  end;
end;

Quote
AlanGLLoyd wrote:

> Are you sure you are attempting to draw on the correct Canvas, and not the
> Form's canvas. I don't have the reference to OnAdvancedCustomDrawSubItem (I
> have D3) but in my experience a Sender is usually a TObject and must be
> typecast to get a Canvas.

> The fact that you did what you wanted on a TListBox shows that the detail
> approach of your code is essentailly correct.

> Alan Lloyd
> alangll...@aol.com

--

"I suppose everything in existence takes its color
from the average hue of our surroundings."

        - H.G. Wells

Other Threads