界面美化(来自网络)

1.PageControl

设置其ownerdrow为true,然后在其DrawTab事件中加入如下代码

procedure TForm1.PageControl1DrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  aRect:TRect;
begin
  with PageControl.aCanvas do
  begin
      Font.Color:=clBlue;
      Font.Style:=[];
      aRect:=Rect;
      inflateRect(aRect,0,-1);
      FillRect(aRect);
      if Active then
      begin
        Font.Style:=[fsBold];
        Brush.Color:=$000080FF; //画底色
        aRect:=Rect;
        aRect.Top:=aRect.Bottom-2;
        FillRect(aRect);
        Brush.Style:=bsClear;

        TextOut(Rect.Left+3,Rect.Top+2,TPageControl(Control).Pages[TabIndex].Caption);

       //具体字体的高度要靠自己调节了.
    end else
       TextOut(Rect.Left+5,Rect.Top+2,TPageControl(Control).Pages[TabIndex].Caption);
    end;
end;

2.listview

a. 背景贴图 

procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
     const ARect: TRect; var DefaultDraw: Boolean);
var
  x,y,w,h:Integer;
begin
  with bb do
  begin
    w:=Width;
    h:=Height;
  end;
  y:=0;
  while y<ListView1.Height do
  begin
    x:=0;
    while x<ListView1.Width do
    begin
      ListView1.Canvas.Draw(x,y,bb);
      Inc(x,w);
    end;
    Inc(y,h);
  end;
end;

b.item的自画.

 uses CommCtrl;

//画状态条
procedure DrawSubItem(LV: TListView; Item: TListItem; SubItem: Integer;
 Prosition: Single; Max, Style: Integer; IsShowProgress: Boolean;
 DrawColor: TColor = $00005B00;
 FrameColor: TColor = $00002F00);
//获取SubItem的区域
 function GetItemRect(LV_Handle, iItem, iSubItem: Integer): TRect;
 var
    Rect: TRect;
 begin
    ListView_GetSubItemRect(LV_Handle, iItem, iSubItem, LVIR_LABEL, @Rect);
    Result := Rect;
 end;
var
 PaintRect, r: TRect;
 i, iWidth, x, y: integer;
 S: string;
begin
  try
 
    with lv do
    begin
      //LockPaint := True;
      PaintRect := GetItemRect(LV.Handle, Item.Index, SubItem);
     r := PaintRect;
//      if SubItem = DrawSubItem then
      Begin
        //这一段是算出百分比
        if Prosition >= Max then
          Prosition := 100
        else
          if Prosition <= 0 then
            Prosition := 0
          else
            Prosition := Round((Prosition / Max) * 100);
 
        if (Prosition = 0) and (not IsShowProgress) then
        begin
        //如果是百分比是0,就直接显示空白
          Canvas.FillRect(r);

        end else
        begin
        //先直充背景色
          Canvas.FillRect(r);
          Canvas.Brush.Color := Color;
//          Canvas.FillRect(r);

          //画一个外框
          InflateRect(r, -2, -2);
          Canvas.Brush.Color := FrameColor; //$00002F00;
          Canvas.FrameRect(R);

          Canvas.Brush.Color := Color;
          InflateRect(r, -1, -1);
          //Canvas.FillRect(r);

          InflateRect(r, -1, -1);
        //根据百分比算出要画的进度条内容宽度
          iWidth := R.Right - Round((R.Right - r.Left) * ((100 - Prosition) /
            100));
          case Style of
            0: //进度条类型,实心填充
              begin
                Canvas.Brush.Color := DrawColor;
                r.Right := iWidth;
                Canvas.FillRect(r);
              end;
            1: //进度条类型,竖线填充
              begin
                i := r.Left;
                while i < iWidth do
                begin
                  Canvas.Pen.Color := Color;
                  Canvas.MoveTo(i, r.Top);
                  Canvas.Pen.Color := DrawColor;
                  canvas.LineTo(i, r.Bottom);
                  Inc(i, 3);
                end;
              end;
          end;
           //画好了进度条后,现在要做的就是显示进度数字了
          Canvas.Brush.Style := bsClear;
          if Prosition = Round(Prosition) then
            S := Format('%d%%', [Round(Prosition)])
          else
            S := FormatFloat('#0.0', Prosition);
 
          with PaintRect do
          begin
            x := Left + (Right - Left + 1 - Canvas.TextWidth(S)) div 2;
            y := Top + (Bottom - Top + 1 - Canvas.TextHeight(S)) div 2;
          end;
          SetBkMode(Canvas.handle, TRANSPARENT);
          Canvas.TextRect(PaintRect, x, y, S);

        end;
        //进度条全部画完,把颜色设置成默认色了
        Canvas.Brush.Color := Color;
 
      end
    end;
  except
  end;
end;

procedure TForm1.ListView1CustomDrawItem(
 Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
 var DefaultDraw: Boolean);
var
 BoundRect, Rect: TRect;
 i: integer;
 TextFormat: Word;
 LV: TListView;

//这个子过程是用来画CheckBox的
  procedure Draw_CheckBox(r: TRect; aCanvas: TCanvas; Checked: Boolean);
  begin
    if Sender.Checkboxes then
    begin
      aCanvas.Pen.Color := clBlue;  //小方框的颜色
      aCanvas.Pen.Width := 2;
      //画CheckBox外框
      aCanvas.Rectangle(r.Left + 2, r.Top + 2, r.Left + 14, r.Bottom - 2);
      if Checked then
      begin //画CheckBox的勾
        aCanvas.MoveTo(r.Left + 4, r.Top + 6);
        aCanvas.LineTo(r.Left + 6, r.Top + 11);
        aCanvas.LineTo(r.Left + 11, r.Top + 5);
      end;
      aCanvas.Pen.Width := 1;
    end;
  end;
begin
  LV := ListView1;
  BoundRect := Item.DisplayRect(drBounds);
  InflateRect(BoundRect, -1, -1);

//    设置字体颜色
//    LV.Canvas.Font.Color := clred;

//查看是否是被选中
  if Item.Selected then
  begin
    if cdsFocused in State then
    begin
      LV.Canvas.Brush.Color := $00ECCCB9; // //clHighlight;
    end
    else
    begin
      LV.Canvas.Brush.Color := $00F8ECE5; //clSilver;
    end;
  end
  else
  begin
    if (Item.Index mod 2) = 0 then
      LV.Canvas.Brush.Color := $00F2F2F2
    else
      LV.Canvas.Brush.Color := clWindow; //隔行换颜色
  end;

  LV.Canvas.FillRect(BoundRect); //初始化背景
 
  for i := 0 to LV.Columns.Count - 1 do
  begin
 //获取SubItem的Rect
    ListView_GetSubItemRect(LV.Handle, Item.Index, i, LVIR_LABEL, @Rect);
    case LV.Columns[i].Alignment of
      taLeftJustify:
        TextFormat := 0;
      taRightJustify:
        TextFormat := DT_RIGHT;
      taCenter:
        TextFormat := DT_CENTER;
    end;
    case i of
      0:
        begin
         Draw_CheckBox(BoundRect, LV.Canvas, Item.Checked);
         InflateRect(Rect, -3, 0); //向后移3个像素,避免被后面画线框时覆盖
         DrawText(
            LV.Canvas.Handle,
            PCHAR(Item.Caption),
            Length(Item.Caption),
            Rect,
            DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or TextFormat);
        end;
      1..MaxInt: //画Subitems[i]
        begin
          if i - 1 = 0 then //显示状态条
          begin
            DrawSubItem(TListView(Sender),
              item,
              i,
              StrToFloatDef(Item.SubItems[i - 1], 0),
              100,
              0,
              True);
          end
          else
          //画SubItem的文字
            if i - 1 <= Item.SubItems.Count - 1 then
              DrawText(
                LV.Canvas.Handle,
                PCHAR(Item.SubItems[i - 1]),
                Length(Item.SubItems[i - 1]),
                Rect,
                DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or TextFormat);
        end;

    end;
  end;

  if Item.Selected then //画选中条外框
  begin
    if cdsFocused in State then//控件是否处于激活状态
      LV.Canvas.Brush.Color := $00DAA07A // $00E2B598; //clHighlight;
    else
      LV.Canvas.Brush.Color := $00E2B598; //$00DAA07A // clHighlight;
    LV.Canvas.FrameRect(BoundRect); //
  end;

  DefaultDraw := False; //不让系统画了

  with Sender.Canvas do
    if Assigned(Font.OnChange) then Font.OnChange(Font);
end;

调用

DrawSubItem(ListView1, ListView1.Items[0],1,
    90,100,0,False);

原文地址:https://www.cnblogs.com/doorsky/p/1617776.html