ViewStyle为vsIcon时如何使用OnDrawItem事件自定义绘制ListView [英] How to custom draw a ListView using the OnDrawItem event when the ViewStyle is vsIcon

查看:43
本文介绍了ViewStyle为vsIcon时如何使用OnDrawItem事件自定义绘制ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自定义绘制ListView使其看起来像这样?

How can I custom draw a ListView to make it look like this?

推荐答案

只有仔细阅读帮助资源,您才能以自定义的方式绘制 TListView 的内容.
下图是代码运行的结果.此图片后附有代码.

附加到 TListView 组件 ImageList1 的宽度和高度均设置为24像素

You can draw content of TListView in custom way very eae only if you will read help resources carefully.
Image below is a result of code running. The code attached after this picture.

Component ImageList1 that is attached to TListView has both width and height set to 24 pixels

这张图片是相同的 TListView ,但没有附加的ImageList.

This one picture is the same TListView but without attached ImageList.

橙色矩形是一个选定的项目

现在转到代码.

procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean);
var
  Bmp: TBitmap;
  Image: TBitmap;
  R: TRect;
  CenterH: Integer;
  CenterV: Integer;
  ImageIndex: Integer;
begin
  R := Item.DisplayRect(drBounds);
  Bmp := TBitmap.Create;
  try
    Image := TBitmap.Create;
    try
      Bmp.SetSize(R.Width, R.Height);

      // Make fill for item
      if Item.Selected then
        Bmp.Canvas.Brush.Color := clWebOrange
      else
        Bmp.Canvas.Brush.Color := clMoneyGreen;
      Bmp.Canvas.FillRect(Bmp.Canvas.ClipRect);

      // Output image associated with current item
      if Assigned(TListView(Sender).LargeImages) then
        begin
          TListView(Sender).LargeImages.GetBitmap(Item.ImageIndex, Image);
          CenterH := (R.Width - Image.Width) div 2;
          CenterV := (R.Height - Image.Height) div 2;
          Bmp.Canvas.Draw(CenterH, CenterV, Image);
        end;

      // Draw ready item's image onto sender's canvas
      Sender.Canvas.Draw(R.Left, R.Top, Bmp);
    finally
      Image.Free;
    end;
  finally
    Bmp.Free;
  end;
end;

必须告知您, vsIcon ViewMode中 TListView 的每个项目的大小取决于通过附加到控件的 TImageList 的大小> LargeImages 属性.比大图片-比 TListView 中的大项目大.

You must be informed that size of each item of TListView in vsIcon ViewMode depend on the size of TImageList attached to control via LargeImages property. Than large image - than large item in TListView.

这篇关于ViewStyle为vsIcon时如何使用OnDrawItem事件自定义绘制ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆