关于ListView的OnCustomDrawXXX事件

href="显示一个背景图像.files/filelist.xml" rel="File-List" />

显示一个背景图像

 

只填充有效列

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;
   
   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VB中,ListView控件是用于显示和编辑项目列表的常用控件之一。当我们在处理ListView控件时,经常需要响应双击事件以执行特定的操作。以下是在VB中处理ListView的双击事件的方法。 首先,我们要确保我们已经创建了一个ListView控件并将其放置在窗体上。然后,在窗体的代码视图中,找到ListView的DoubleClick事件并将其添加到相应的方法中。 可以通过在窗体的代码视图中查找ListView控件的DoubleClick事件来添加响应代码。也可以通过在窗体设计器中选择ListView控件并点击属性窗格上的黄色闪电图标来添加DoubleClick事件。 双击事件的方法中,我们可以执行我们想要的操作,例如获取选定的项、获取选定项的值、显示消息框等等。 以下是一个示例的双击事件处理方法: Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick ' 获取选定的项 Dim selectedItems As ListView.SelectedListViewItemCollection = ListView1.SelectedItems ' 遍历选定的项并执行特定的操作 For Each selectedItem As ListViewItem In selectedItems ' 获取选定项的值 Dim itemValue As String = selectedItem.Text ' 显示消息框 MessageBox.Show("选定项的值为:" & itemValue) Next End Sub 在上述示例中,ListView1_DoubleClick方法会在用户双击ListView的项目时触发。首先,我们获取选定的项并通过遍历来执行操作。在这个示例中,我们获取选定项的文本值,并通过消息框显示出来。 通过这种方式,我们可以处理ListView控件的双击事件,并在双击时执行我们希望的操作。希望这个简单的示例能够帮助你理解如何在VB中处理ListView的双击事件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值