Delphi TRzListView自绘表头标题背景色示例(OnDrawHeader)

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Delphi TRzListView自绘表头标题背景色示例(OnDrawHeader),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

RzListView自带了OnDrawHeader 事件,在该事件中进行自绘即可。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,CommCtrl, ComCtrls,  ProgressBarEx, StdCtrls, URLLabel, RzListVw;

type
  TForm1 = class(TForm)
    RzListView1: TRzListView;
    procedure FormCreate(Sender: TObject);
    procedure RzListView1DrawHeader(Sender: TObject; Canvas: TCanvas;
      Index: Integer; Rect: TRect);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: integer;
  lit: TListItem;
begin
  with RzListView1 do
    for i := 0 to 5 do
    begin
      Columns.Add.Caption := 'Column' + IntToStr(i);
      Columns[i].Width := 70;
      lit := Items.Add;
      lit.Caption := 'Item' + IntToStr(i);
      for j := 1 to 5 do
        lit.SubItems.Add('SubItem' + IntToStr(j) + IntToStr(i))
    end
end;



procedure TForm1.RzListView1DrawHeader(Sender: TObject; Canvas: TCanvas; Index: Integer; Rect: TRect);

procedure ADrawText(Canvas: TCanvas; AText: string; ARect: TRect);
var
  H, W, X, Y: Integer;
begin
  Canvas.Brush.Style := bsClear;

  Canvas.Font.Color := clWhite;
  //Canvas.Font.Name := '微软雅黑';
  //Canvas.Font.Size := 18;

  H := Canvas.TextHeight('a');
  W := Canvas.TextWidth(AText);
  X := ARect.Left + (ARect.Right - ARect.Left - W) div 2;
  Y := ARect.Top + (ARect.Bottom - ARect.top - H) div 2;
  if X < ARect.Left then
    X := ARect.Left;
  Canvas.TextOut(X, Y, AText);
end;

var
  bmp : TBitmap;
  Str: string;
  ARect: TRect;
begin
  bmp := TBitmap.Create;
  try
    bmp.SetSize(1, Rect.Bottom - Rect.Top);
    bmp.Canvas.Brush.Color := clblue;
    bmp.Canvas.FillRect(bmp.Canvas.ClipRect);
    ARect := Rect;
    if Index = RzListView1.Columns.Count - 1  then
    begin
      ARect.Right :=  ARect.Right
    end;
    Canvas.StretchDraw(ARect, bmp);
    {画分隔线}
    Canvas.Pen.Color := $00838383;
    Canvas.MoveTo(ARect.Right - 1, ARect.Top + 1);
    Canvas.LineTo(ARect.Right - 1, ARect.Bottom - 1);
//    if Index = 0 then
//    begin
//      Canvas.Pen.Color := $FF6E6E6E;
//      Canvas.MoveTo(ARect.Left , ARect.Top + 1);
//      Canvas.LineTo(ARect.Left , ARect.Bottom - 1);
//    end;
    Str := RzListView1.Columns[Index].Caption;
    ADrawText(Canvas, Str, ARect);
  finally
    bmp.Free;
  end;
end;

end.

示例效果如下截图:

Delphi TRzListView自绘表头标题背景色示例(OnDrawHeader)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/139921.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!