• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3891
  • Last Modified:

Owner-Drawn Listview BG Color Changing

I need to know how to change the background color of an owner-drawn listview. Any Delphi code would be much appreciated to get this done.
0
OPDelphiCoder
Asked:
OPDelphiCoder
4 Solutions
 
rfwoolfCommented:
Perhaps you can show some code?

If you have trouble changing the listview's color directly, one idea is to set its ParentColor property to True, put it on a panel and then programatically change the panel's color.
0
 
mokuleCommented:
procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
  const ARect: TRect; var DefaultDraw: Boolean);
begin
  ListView1.Canvas.Brush.Color := clred;
  ListView1.Canvas.Rectangle(ARect);
end;

0
 
Tomas Helgi JohannssonCommented:
   Hi!

Check out this examples:
http://www.swissdelphicenter.ch/torry/showcode.php?id=818
Hope it helps.

Regards,
   Tomas Helgi
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rfwoolfCommented:
There are numerous examples of using ownerdraw for changing the colours of say, text or rows in a listview:

You can look at the Delphi demo files:
  \delphi\demos\Virtual ListView
  \delphi\demos\CustomDraw

MAKING TEXT BOLD:
http://groups.google.com.au/group/comp.lang.pascal.delphi.misc/browse_thread/thread/68f96e4e996b6531/e037949554a61efc%23e037949554a61efc

How to color a specific subitem in a TListView
http://www.delphi3000.com/articles/article_2768.asp?SK=

===

So the question is, are you trying to change the colour of the entire listview, using ownerdraw?
0
 
rfwoolfCommented:
Hi Mokule

I tested your code and it works for me!
Ownerdraw was set to true and I put your procedure in the onCustomerDraw event (like you) and the listview was red.

I'm using Delphi 2006 (Borland Delphi Studio)
0
 
TheRealLokiSenior DeveloperCommented:
here's a demo I wrote for you
make sure you have set OwnerDraw to false in the listview for this code to work
I have done some rudimentary handling of the selecetd/focused state, but you will want to change this due to the colour choices you make

function GetActualColumnBounds(ListView: TListView; Item: TListItem; column: integer): TRect;
var
  i: integer;
begin
  result := item.DisplayRect(drBounds);
  for i := 1 to column do
  begin
    result.left := result.left + ListView.Columns[i-1].Width;
  end;
  result.Right := result.Left + ListView.Columns[column].Width;
end;

procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  therect: trect;

begin
  DefaultDraw := false;
  therect := GetActualColumnBounds((Sender as TlistView), Item, subitem);
// depending on the row and the column, show a different colour
  if (SubItem mod 2) = 0 then
  begin
    if (item.Index mod 2) = 0 then
      ListView1.Canvas.brush.Color := clLime
    else
      ListView1.Canvas.brush.Color := clMaroon;
  end
  else
  begin
    if (item.Index mod 2) = 0 then
      ListView1.Canvas.brush.Color := clBlue
    else
      ListView1.Canvas.brush.Color := clRed;
  end;

  ListView1.Canvas.FillRect(therect);

//  ListView1.Canvas.Font.color := clFuchsia;
  ListView1.Canvas.TextRect(therect, therect.left, therect.top, Item.SubItems[SubItem-1]);

  ListView1.Canvas.Brush.Color := clWhite;
  if (cdsFocused in State) then
    ListView1.Canvas.FrameRect(therect)
  else if (cdsSelected in State) then
    ListView1.Canvas. DrawFocusRect(therect);
end;
0
 
OPDelphiCoderAuthor Commented:
Hi TheRealLoki
I wasn't specific enough when I opened this question. I'm not using the visual component library or classes. I'm using windows api only so I'd have to wait for messages in my listview's window procedure like wm_drawitem and wm_erasebackground etc. I'm under the impression that I could use SetBkColor api maybe or would I have to draw each colum with the specified background color?
0
 
TheRealLokiSenior DeveloperCommented:
sorry, haven't done much with listview api :-)
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now