关于ListView的OnCustomDrawXXX事件

来源:互联网 发布:开发java web最快的ide 编辑:程序博客网 时间:2024/04/27 15:12

显示一个背景图像

 

只填充有效列

procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
  const ARect: TRect; var DefaultDraw: Boolean);
 
  function GetHeaderHeight: Integer;
  var
    Header: HWND;           // header window handle
    Pl: TWindowPlacement;   // header window placement
  begin
    // Get header window
    Header := SendMessage(ListView1.Handle, LVM_GETHEADER, 0, 0);
    // Get header window placement
    FillChar(Pl, SizeOf(Pl), 0);
    Pl.length := SizeOf(Pl);
    GetWindowPlacement(Header, @Pl);
    // Calculate header window height
    Result  := Pl.rcNormalPosition.Bottom - Pl.rcNormalPosition.Top;
  end;
 
var
  BmpXPos, BmpYPos: Integer;  // X and Y position for bitmap
  Bmp: TBitmap;               // Reference to bitmap
  ItemRect: TRect;            // List item bounds rectangle
  TopOffset: Integer;         // Y pos where bmp drawing starts
begin
  // Get top offset where bitmap drawing starts
  if ListView1.Items.Count > 0 then
  begin
    ListView_GetItemRect(ListView1.Handle, 0, ItemRect, LVIR_BOUNDS);
    TopOffset := ListView_GetTopIndex(ListView1.Handle) *
      (ItemRect.Bottom - ItemRect.Top);
  end
  else
    TopOffset := 0;
  BmpYPos := ARect.Top - TopOffset + GetHeaderHeight;
  // Draw the bitmap
  // get reference to bitmap
  Bmp := Image1.Picture.Bitmap;
  // loop until bmp is past bottom of list view
  while BmpYPos < ARect.Bottom do
  begin
    // draw bitmaps across width of display
    BmpXPos := ARect.Left;
    while BmpXPos < ARect.Right do
    begin
      ListView1.Canvas.Draw(BmpXPos, BmpYPos, Bmp);
      Inc(BmpXPos, Bmp.Width);
    end;
    // move to next row
    Inc(BmpYPos, Bmp.Height);
  end;
end;

 

全部填充

// Ensure that the items are drawn transparently

  SetBkMode(ListView1.Canvas.Handle, TRANSPARENT);

  ListView_SetTextBkColor(ListView1.Handle, CLR_NONE);

  ListView_SetBKColor(ListView1.Handle, CLR_NONE);

 

 

隔行换色

 

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
const
  cStripe = $CCFFCC;  // colour of alternate list items
begin
  if Odd(Item.Index) then
    // odd list items have green background
    ListView1.Canvas.Brush.Color := cStripe
  else
    // even list items have window colour background
    ListView1.Canvas.Brush.Color := clWindow;
end;

 

 

用不同的颜色显示列

 

procedure TForm1.SetLVColumnColour(ColIdx: Integer);
  // Sets the list view brush colour for the column
const
  // The colours for each list view column
  cRainbow: array[0..3] of TColor = (
    $FFCCCC, $CCFFCC, $CCCCFF, $CCFFFF
  );
begin
  ListView1.Canvas.Brush.Color := cRainBow[ColIdx];
end;
 
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
  // Draw the "Caption" column
begin
  // Set the colour for column 0
  SetLVColumnColour(0);
end;
 
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
  // Draw the sub item columns
begin
  // Check if SubItem is 0 and exit (Delphi 4 calls this event 
  // with SubItem = 0, while Delphi 7 starts with SubItem = 1
  if SubItem = 0 then Exit;
  // We set the background colour to the colour required for
  // the column per the SubItem parameter
  SetLVColumnColour(SubItem);
end;

 

 

用不同的字体显示列

 

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
  // Set column 0 to use Comic Sans MS italic
begin
  ListView1.Canvas.Font.Name := 'Comic Sans MS';
  ListView1.Canvas.Font.Style := [fsItalic];
end;
 
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  // Ensure SubItem 0 not updated here in Delphi 4
  if SubItem = 0 then Exit;
  if (SubItem = 3) and (Pos('(', Item.SubItems[2]) > 0) then
    // Display negative values in "Amount" column in red
    ListView1.Canvas.Font.Color := clRed
  else
    // Display all other sub item colums in black
    ListView1.Canvas.Font.Color := clBlack;
end;

 

 

显示一个有阴影的列

 

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
  // Handles column click: updates shaded column
begin
  // Updates the index of the shaded column
  fCurrentCol := Column.Index;
  // Redisplays list view with new column highlighted
  ListView1.Invalidate;
end;

 

private
  fCurrentCol: Integer; // currently selected column 

 

procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
  const ARect: TRect; var DefaultDraw: Boolean);
  // Displays shading in any area not occupied by list items
var
  ColLeft: Integer; // left edge of selected column
  ColBounds: TRect; // bounds of the selected column
  I: Integer;       // loops thru columns
begin
  // Calculate left side of selected column
  ColLeft := ARect.Left;
  for I := 0 to Pred(fCurrentCol) do
    ColLeft := ColLeft + ListView_GetColumnWidth(ListView1.Handle, I);
  // Calculate bounding rectangle of selected column
  ColBounds := Rect(
    ColLeft,
    ARect.Top,
    ColLeft + ListView_GetColumnWidth(ListView1.Handle, fCurrentCol),
    ARect.Bottom
  );
  // Shade the column
  // other event handlers overwrite this where there are list
  // items but this code ensures shading extends to bottom of
  // list view client rectangle
  ListView1.Canvas.Brush.Color := cShade;
  ListView1.Canvas.FillRect(ColBounds);
end;

 

procedure TForm1.SetLVColumnShading(ColIdx: Integer);
begin
  if fCurrentCol = ColIdx then
    // given column is selected: shade it
    ListView1.Canvas.Brush.Color := cShade
  else
    // given column not shaded: ensure correct background used
    ListView1.Canvas.Brush.Color := ColorToRGB(ListView1.Color);
end;
 
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
  // Shade the first column (index 0) if this is selected
begin
  SetLVColumnShading(0);
end;
 
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
  // Shade a sub item column if selected
begin
  if SubItem > 0 then // ensure not column 0 (Delphi 4)
    SetLVColumnShading(SubItem);
end;

 

原创粉丝点击