[控件]Cxgrid标题栏自绘

再次感谢研究的无私分享,很实用的功能吧,老司机带路少走弯路!

上码。


 procedure CustomDrawColumnHeader(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: Boolean);
const
  MultiLines: array [Boolean] of Integer = (cxSingleLine, cxWordBreak);
  ShowEndEllipsises: array [Boolean] of Integer = (0, cxShowEndEllipsis);
var
  ATextRect, ABounds: TRect;
  AFontTextColor, ABkColor, AColor: TColor;
  ABorders: TcxBorders;
  AText: string;
  AFont: TFont;
  AState: TcxButtonState;
begin
  ABounds := AViewInfo.Bounds;
  ABorders := [bBottom, bLeft, bRight];
  AFont := ACanvas.Font;
  ATextRect := AViewInfo.ContentBounds;
  AFontTextColor := ACanvas.Font.Color;
  InflateRect(ATextRect, -1, -1);

//设置颜色及鼠标移上、按下效果 
  case AViewInfo.ButtonState of
    cxbsDefault, cxbsNormal, cxbsDisabled:
      ABkColor := clInactiveCaption;
    cxbsHot:
      ABkColor := $00CFBDAB;
    cxbsPressed:
      ABkColor := $00A78665;
  end;
 
  AText := AViewInfo.Column.Caption;//列标题
  with ACanvas do 
  begin
    //填充标题颜色
    if (ABkColor <> clNone) then
    begin
      SetBrushColor(ABkColor);
      FillRect(ABounds);
    end;
   //绘制文字 
    if AText <> '' then
    begin
      Brush.Style := bsClear;
      Font := AFont;
      Font.Color := AFontTextColor;
      DrawText(AText, ATextRect, cxAlignmentsHorz[taCenter] or cxAlignmentsVert[vaCenter] or MultiLines[False] or ShowEndEllipsises[False]);
      Brush.Style := bsSolid;
    end;
  end;

  with ABounds do
  begin
    // 这里是加边框
    ACanvas.Brush.Color := $00A78665;
    if bLeft in ABorders then // 左边框
      ACanvas.FillRect(Rect(Left, Top, Left + 1, Bottom));
    if bTop in ABorders then //上边框
      ACanvas.FillRect(Rect(Left, Top, Right, Top + 1));
    if bRight in ABorders then //右边框
      ACanvas.FillRect(Rect(Right - 1, Top, Right, Bottom));
    if bBottom in ABorders then //下边框
      ACanvas.FillRect(Rect(Left, Bottom - 1, Right, Bottom));
  end;
  ADone := True;
end;

//使用示例

 procedure TForm15.cxGrid1DBTableView1CustomDrawColumnHeader(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo;
  var ADone: Boolean);
begin
    CustomDrawColumnHeader(Sender, ACanvas, AViewInfo, ADone);
end;
分享到:

评论已关闭。