Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-09

Displaying a Listview entry in bold and a different colour

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Richard Mace

Hi All,
I just want to make sure that I am not doing anything wrong here, please can somebody give me a little code example of how to correctly set the font to bold and a differnet colour in a listview entry? Also, what event should I be using?
I think I've found a bug, but am not sure

Thanks in advance.

Richard

Win32 Lazarus 1.24 FPC 2.64
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140915/3560e16e/attachment.html>

Reply by email

Howard Page-Clark
to Richard Mace
Richard Mace wrote
Hi All, I just want to make sure that I am not doing anything wrong here, please can somebody give me a little code example of how to correctly set the font to bold and a differnet colour in a listview entry?

I wouldn't claim the following is 'correct', but it works on Windows using TListView's OnCustomDrawItem() event.

Howard

==code==

unit mainListviewOwnerDraw;
{$mode objfpc}{$H+}
interface
uses
   Classes, SysUtils, Forms, Graphics, ComCtrls, LCLType, StdCtrls;
type
   TForm1 = class(TForm)
     ListView1: TListView;
     procedure FormCreate(Sender: TObject);
     procedure ListView1CustomDrawItem(Sender: TCustomListView; Item:
TListItem;
       State: TCustomDrawState; var DefaultDraw: Boolean);
   end;
var
   Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.FormCreate(Sender: TObject);
var
   li: TListItem;
   lc: TListColumn;
   i: Integer;
begin
   ListView1.ViewStyle:=vsReport;
   ListView1.OwnerDraw:=True;
   lc:=TListColumn.Create(ListView1.Columns);
   lc.Caption:='Select an item to show custom draw:';
   lc.Width:=ListView1.Canvas.TextWidth(lc.Caption) + 20;
   for i:=0 to 4 do begin
     li:=TListItem.Create(ListView1.Items);
     li.Caption:=Format('Item number %d',[i]);
     ListView1.Items.AddItem(li);
   end;
end;
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
   Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
   rct: TRect;
begin
   DefaultDraw:=False;
   if (cdsSelected in State) then begin
     Sender.Canvas.Font.Style:=[fsBold];
     Sender.Canvas.Font.Color:=clRed;
   end
   else begin
     Sender.Canvas.Font.Style:=[];
     Sender.Canvas.Font.Color:=clBlack;
   end;
   rct:=Item.DisplayRect(drLabel);
   Sender.Canvas.FillRect(rct); //paint background with default brush colour
   Sender.Canvas.TextOut(rct.Left, rct.Top, Item.Caption);
end;
end.

Reply by email

Richard Mace
to Howard Page-Clark

Hi Howard,
Many thanks for your reply.
That does seem to work, but my subitems and images don't seem to be displayed now?
Would you mind giving me a further pointer as to how to display the images, and the subitems in the same colour?

Thanks

Richard

Howard Page-Clark wrote 85 lines
On 15/09/2014 13:10, Richard Mace wrote: > Hi All, > I just want to make sure that I am not doing anything wrong here, please > can somebody give me a little code example of how to correctly set the > font to bold and a differnet colour in a listview entry? > I wouldn't claim the following is 'correct', but it works on Windows using TListView's OnCustomDrawItem() event. Howard ==code== unit mainListviewOwnerDraw; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Graphics, ComCtrls, LCLType, StdCtrls; type TForm1 = class(TForm) ListView1: TListView; procedure FormCreate(Sender: TObject); procedure ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); end; var Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject); var li: TListItem; lc: TListColumn; i: Integer; begin ListView1.ViewStyle:=vsReport; ListView1.OwnerDraw:=True; lc:=TListColumn.Create(ListView1.Columns); lc.Caption:='Select an item to show custom draw:'; lc.Width:=ListView1.Canvas.TextWidth(lc.Caption) + 20; for i:=0 to 4 do begin li:=TListItem.Create(ListView1.Items); li.Caption:=Format('Item number %d',[i]); ListView1.Items.AddItem(li); end; end; procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); var rct: TRect; begin DefaultDraw:=False; if (cdsSelected in State) then begin Sender.Canvas.Font.Style:=[fsBold]; Sender.Canvas.Font.Color:=clRed; end else begin Sender.Canvas.Font.Style:=[]; Sender.Canvas.Font.Color:=clBlack; end; rct:=Item.DisplayRect(drLabel); Sender.Canvas.FillRect(rct); //paint background with default brush colour Sender.Canvas.TextOut(rct.Left, rct.Top, Item.Caption); end; end. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140916/7cf79a87/attachment-0001.html>

Reply by email