📄 customdrawdemomain.pas
字号:
FFont := Value;
FIsFontAssigned := True;
end;
{ TCustomDrawDemoMainForm }
procedure TCustomDrawDemoMainForm.FormCreate(Sender: TObject);
procedure AddCustomDrawInfos;
begin
FCustomDrawInfo.AddNewItem(cdaBackground, itNormal);
FCustomDrawInfo.AddNewItem(cdaCategory, itNormal);
FCustomDrawInfo.AddNewItem(cdaCell, itCell);
FCustomDrawInfo.AddNewItem(cdaHeader, itNormal);
end;
procedure AdjustCustomDrawItems;
begin
FCustomDrawInfo[cdaBackground].BkImageType := bkiTile;
FCustomDrawInfo[cdaCategory].DrawingStyle := cdsGradient;
FCustomDrawInfo[cdaCategory].ColorScheme := csBlue;
FCustomDrawInfo[cdaCell].DrawingStyle := cdsGradient;
FCustomDrawInfo[cdaHeader].DrawingStyle := cdsDefaultDrawing;
end;
begin
FCustomDrawInfo := TcxCustomDrawInfo.Create(cxDBVerticalGrid);
AddCustomDrawInfos;
AdjustCustomDrawItems;
end;
procedure TCustomDrawDemoMainForm.FormDestroy(Sender: TObject);
begin
FCustomDrawInfo.Free;
end;
procedure TCustomDrawDemoMainForm.FormShow(Sender: TObject);
begin
cxDBVerticalGrid.FullExpand;
CustomDrawDemoEditorForm.Show;
end;
procedure TCustomDrawDemoMainForm.actCustomizationFormExecute(Sender: TObject);
begin
CustomDrawDemoEditorForm.Show;
end;
procedure TCustomDrawDemoMainForm.cxDBVerticalGridDrawBackground(
Sender: TObject; ACanvas: TcxCanvas; const R: TRect;
const AViewParams: TcxViewParams; var Done: Boolean);
begin
Done := DrawBackgroundItem(FCustomDrawInfo[cdaBackground], ACanvas, R, AViewParams, Sender);
end;
function TCustomDrawDemoMainForm.DrawItem(AItem: TcxItemCustomDrawInfo;
ACanvas: TcxCanvas; const R: TRect; AHorizontal: Boolean): Boolean;
begin
case AItem.DrawingStyle of
cdsBkImage:
ACanvas.FillRect(R, AItem.Bitmap);
cdsGradient:
FillGradientRect(ACanvas.Canvas.Handle, R,
ColorScheme[Integer(AItem.ColorScheme), 1],
ColorScheme[Integer(AItem.ColorScheme), 0],
AHorizontal);
end;
Result := (AItem.DrawingStyle <> cdsDefaultDrawing);
end;
function TCustomDrawDemoMainForm.DrawBackgroundItem(AItem: TcxItemCustomDrawInfo;
ACanvas: TcxCanvas; const R: TRect; AViewParams: TcxViewParams; Sender: TObject): Boolean;
begin
Result := False;
if AItem.DrawingStyle = cdsDefaultDrawing then Exit;
Result := DrawItem(AItem, ACanvas, R, Integer(AItem.ColorScheme) > 1);
end;
function TCustomDrawDemoMainForm.DrawCellItem(AItem: TcxItemCustomDrawInfo;
ACanvas: TcxCanvas; AValueInfo: TcxRowValueInfo; APainter: TcxvgPainter;
Sender: TObject): Boolean;
procedure OwnerDrawText(ALineColor: TColor; AFont: TFont);
var
ARect: TRect;
begin
ACanvas.Pen.Color := ALineColor;
ACanvas.Brush.Style := bsClear;
ACanvas.Font := AFont;
ARect := cxRectInflate(AValueInfo.BoundsRect, -1, 0, 0, -1);
with ARect do
ACanvas.Canvas.Rectangle(Left, Top, Right, Bottom);
if AValueInfo.EditViewInfo is TcxCustomCheckBoxViewInfo then
with TcxCustomCheckBoxViewInfo(AValueInfo.EditViewInfo) do
AValueInfo.LookAndFeelPainterClass.DrawCheckButton(ACanvas,
cxRectOffset(CheckBoxRect, AValueInfo.BoundsRect.Left, AValueInfo.BoundsRect.Top),
cxbsDefault, State = cbsChecked)
else
if AValueInfo.EditViewInfo is TcxCustomTextEditViewInfo then
with TcxCustomTextEditViewInfo(AValueInfo.EditViewInfo) do
ACanvas.DrawTexT(Text, cxRectInflate(AValueInfo.BoundsRect, -2, -2), 0);
end;
procedure AddLines(ALines: TLineInfoList; const R: TRect; AColor: TColor);
begin
ALines.Add(R.Left, R.Top, R.Right - R.Left, 1, AColor);
ALines.Add(R.Left, R.Top, R.Bottom - R.Top, 1, AColor);
ALines.Add(R.Right - 1, R.Top, R.Right - R.Left, 1, AColor);
ALines.Add(R.Left, R.Bottom - 1, R.Right - R.Left, 1, AColor);
end;
var
AStyle: TcxStyle;
I: Integer;
begin
with TcxVerticalGrid(Sender).ViewInfo do
begin
LinesInfo.Clear;
for I := 0 to ViewRects.BandRects.Count - 1 do
AddLines(LinesInfo, ViewRects.BandRects[I], BandBorderColor);
end;
AValueInfo.Transparent := (AValueInfo.ViewParams.Bitmap <> nil) and
(not AValueInfo.ViewParams.Bitmap.Empty);
if AItem.DrawingStyle = cdsDefaultDrawing then
begin
APainter.DrawRowValueCell(AValueInfo);
DrawCellsLines(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, AValueInfo.BoundsRect);
DrawRightLine(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, AValueInfo.BoundsRect);
Result := True;
Exit;
end;
if AItem.DrawingStyle = cdsDependsOnData then
begin
if cxDBVerticalGridCustomer.Properties.Values[AValueInfo.RecordIndex] = 'Y' then
AStyle := CustomDrawDemoDataDM.stCustomer
else
AStyle := CustomDrawDemoDataDM.stNoCustomer;
ACanvas.Brush.Color := AStyle.Color;
ACanvas.FillRect(AValueInfo.VisibleRect);
OwnerDrawText(AStyle.TextColor, AStyle.Font);
DrawCellsLines(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, AValueInfo.BoundsRect);
Result := True;
end
else
begin
Result := DrawItem(AItem, ACanvas, cxRectInflate(AValueInfo.BoundsRect, 0, 0, 1, 1) , Integer(AItem.ColorScheme) > 1);
if AItem.OwnerTextDraw then
OwnerDrawText(clGray, AItem.Font)
else
begin
AValueInfo.Transparent := True;
Result := False;
end;
end;
end;
procedure TCustomDrawDemoMainForm.cxDBVerticalGridDrawValue(
Sender: TObject; ACanvas: TcxCanvas; APainter: TcxvgPainter;
AValueInfo: TcxRowValueInfo; var Done: Boolean);
begin
Done := DrawCellItem(FCustomDrawInfo[cdaCell], ACanvas, AValueInfo, APainter, Sender);
end;
procedure TCustomDrawDemoMainForm.DrawIndents(AHeaderViewInfo: TcxCustomRowHeaderInfo; ACustomDrawArea: TCustomDrawArea; ACanvas: TcxCanvas; AViewParams: TcxViewParams;
APainter: TcxvgPainter; AIndentInfoList: TIndentInfoList);
var
i: Integer;
begin
if FCustomDrawInfo[ACustomDrawArea].DrawingStyle = cdsDefaultDrawing then
for I := 0 to AIndentInfoList.Count - 1 do
with AIndentInfoList[I]^ do
begin
APainter.Canvas.Brush.Color := AViewParams.Color;
APainter.FillRect(cxRectInflate(Bounds, 0, 0, 0, 1), ViewParams.Bitmap);
end
else
for I := 0 to AIndentInfoList.Count - 1 do
DrawItem(FCustomDrawInfo[ACustomDrawArea], ACanvas, cxRectInflate(AIndentInfoList[I].Bounds, 0, 0, 0, 1),
True);
end;
procedure TCustomDrawDemoMainForm.DrawCellsLines(ACanvas: TcxCanvas; AColor: TColor; ARect: TRect);
begin
ACanvas.Brush.Color := AColor;
with ARect do
begin
ACanvas.FillRect(cxRectBounds(Left, Top - 1, Right - Left + 1, 1));
ACanvas.FillRect(cxRectBounds(Left, Bottom, Right - Left + 1, 1));
end;
end;
procedure TCustomDrawDemoMainForm.DrawDefaultLines(ACanvas: TcxCanvas; AColor: TColor;
AHeaderViewInfo: TcxCustomRowHeaderInfo; ARect: TRect; ALineInfos: TLineInfos);
begin
FillRects(ACanvas, AColor, GetIndents(AHeaderViewInfo, ALineInfos));
DrawCellsLines(ACanvas, AColor, ARect);
end;
procedure TCustomDrawDemoMainForm.DrawRightLine(ACanvas: TcxCanvas; AColor: TColor; ARect: TRect);
begin
ACanvas.Brush.Color := AColor;
with ARect do
ACanvas.FillRect(cxRectBounds(Right, Top, 1, Bottom - Top));
end;
function TCustomDrawDemoMainForm.GetAdditionalLines(Sender: TcxDBVerticalGrid; AHeaderViewInfo: TcxCustomRowHeaderInfo): TLineInfos;
var
i: Integer;
begin
Result := [];
if Sender.LayoutStyle <> lsBandsView then Exit;
for i := 0 to Sender.ViewInfo.BandInfo.Count - 1 do
if Sender.ViewInfo.BandInfo[i].FirstRow = AHeaderViewInfo.Row then
begin
Result := [liTop];
if Sender.ViewInfo.BandInfo[i].RowsCount = 1 then
Result := Result + [liBottom];
end;
for i := 0 to Sender.ViewInfo.BandInfo.Count - 1 do
if (Sender.PrevVisibleRow(Sender.ViewInfo.BandInfo[i].FirstRow)
= AHeaderViewInfo.Row) or (Sender.LastVisibleRow = AHeaderViewInfo.Row) then
begin
Result := Result + [liBottom];
Exit;
end;
end;
procedure TCustomDrawDemoMainForm.cxDBVerticalGridDrawRowHeader(Sender: TObject; ACanvas: TcxCanvas;
APainter: TcxvgPainter; AHeaderViewInfo: TcxCustomRowHeaderInfo; var Done: Boolean);
begin
if AHeaderViewInfo is TcxEditorRowHeaderInfo then
Done := DrawHeaderItem(FCustomDrawInfo[cdaHeader], ACanvas, AHeaderViewInfo, APainter, Sender) else
if AHeaderViewInfo is TcxCategoryRowHeaderInfo then
Done := DrawCategoryItem(FCustomDrawInfo[cdaCategory], ACanvas, TcxCategoryRowHeaderInfo(AHeaderViewInfo),
APainter, Sender);
end;
function TCustomDrawDemoMainForm.DrawHeaderItem(AItem: TcxItemCustomDrawInfo; ACanvas: TcxCanvas;
AHeaderViewInfo: TcxCustomRowHeaderInfo; APainter: TcxvgPainter; Sender: TObject): Boolean;
var
ARect: TRect;
ACategoryViewParams: TcxViewParams;
ALineInfo: TLineInfos;
begin
TcxVerticalGrid(Sender).ViewInfo.LinesInfo.Clear;
if (AItem.DrawingStyle = cdsDefaultDrawing) then
begin
APainter.DrawRowHeader(AHeaderViewInfo);
DrawIndents(AHeaderViewInfo, cdaHeader, ACanvas, AHeaderViewInfo.ViewParams, APainter, AHeaderViewInfo.RowIndents);
Result := True;
end else
begin
with AHeaderViewInfo do
begin
ARect := cxRectInflate(HeaderRect, 0, 0, 1, 1);
ARect.Left := RowIndents[0].Bounds.Left;
DrawItem(FCustomDrawInfo[cdaHeader], ACanvas, ARect, Integer(AItem.ColorScheme) > 1);
end;
APainter.Painter.DrawExpandButton(ACanvas, AHeaderViewInfo.ButtonRect, AHeaderViewInfo.Row.Expanded);
AHeaderViewInfo.Transparent := True;
Result := False;
end;
with ACategoryViewParams do
begin
Bitmap := cxDBVerticalGrid.Styles.Category.Bitmap;
Color := cxDBVerticalGrid.Styles.Category.Color;
end;
DrawIndents(AHeaderViewInfo, cdaCategory, ACanvas, ACategoryViewParams, APainter, AHeaderViewInfo.CategoryIndents);
if (FCustomDrawInfo[cdaHeader].DrawingStyle = cdsDefaultDrawing) or
(FCustomDrawInfo[cdaCategory].DrawingStyle = cdsDefaultDrawing) then
begin
DrawRightLine(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, AHeaderViewInfo.HeaderRect);
if cxRectIsNull(AHeaderViewInfo.HeaderCellsRect) then
ARect := AHeaderViewInfo.RowIndents[AHeaderViewInfo.RowIndents.Count - 1].Bounds
else
ARect := AHeaderViewInfo.HeaderCellsRect;
ALineInfo := GetAdditionalLines(TcxDBVerticalGrid(Sender), AHeaderViewInfo);
DrawDefaultLines(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, AHeaderViewInfo, ARect, ALineInfo);
end;
AHeaderViewInfo.LinesInfo.Clear;
end;
function TCustomDrawDemoMainForm.DrawCategoryItem(AItem: TcxItemCustomDrawInfo;
ACanvas: TcxCanvas; ACategoryViewInfo: TcxCategoryRowHeaderInfo; APainter: TcxvgPainter;
Sender: TObject): Boolean;
var
ARect: TRect;
ARowViewParams: TcxViewParams;
ADelta: Integer;
ALineInfo: TLineInfos;
begin
TcxVerticalGrid(Sender).ViewInfo.LinesInfo.Clear;
if (ACategoryViewInfo.RowIndents.Count > 0) then
begin
if FCustomDrawInfo[cdaHeader].DrawingStyle <> cdsDefaultDrawing then
begin
ADelta := ACategoryViewInfo.RowIndents[0].Bounds.Left - ACategoryViewInfo.HeaderRect.Left;
ARect := ACategoryViewInfo.HeaderRect;
ARect.Left := ACategoryViewInfo.RowIndents[0].Bounds.Left;
ARect.Right := ARect.Left + (TcxVerticalGrid(Sender).ViewInfo.FullHeaderWidth - ADelta);
DrawItem(FCustomDrawInfo[cdaHeader], ACanvas, cxRectInflate(ARect, 0, 0, 0, 1), Integer(FCustomDrawInfo[cdaHeader].ColorScheme) > 1)
end else
begin
with ARowViewParams do
begin
Bitmap := cxDBVerticalGrid.Styles.Header.Bitmap;
Color := cxDBVerticalGrid.Styles.Header.Color;
end;
DrawIndents(ACategoryViewInfo, cdaHeader, ACanvas, ARowViewParams, APainter, ACategoryViewInfo.RowIndents);
end;
end;
if (AItem.DrawingStyle = cdsDefaultDrawing) then
begin
APainter.DrawRowHeaderCell(ACategoryViewInfo.CaptionsInfo[0], False);
Result := True;
end else
begin
DrawItem(AItem, ACanvas, cxRectInflate(ACategoryViewInfo.HeaderCellsRect, 0, 0, 1, 1), Integer(AItem.ColorScheme) > 1);
ACategoryViewInfo.Transparent := True;
Result := False;
end;
APainter.Painter.DrawExpandButton(ACanvas, ACategoryViewInfo.ButtonRect, ACategoryViewInfo.Row.Expanded);
DrawIndents(ACategoryViewInfo, cdaCategory, ACanvas, ACategoryViewInfo.ViewParams, APainter, ACategoryViewInfo.CategoryIndents);
if (FCustomDrawInfo[cdaHeader].DrawingStyle = cdsDefaultDrawing) or
(FCustomDrawInfo[cdaCategory].DrawingStyle = cdsDefaultDrawing) then
begin
DrawRightLine(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, ACategoryViewInfo.HeaderRect);
ALineInfo := GetAdditionalLines(TcxDBVerticalGrid(Sender), ACategoryViewInfo);
DrawDefaultLines(ACanvas, TcxVerticalGrid(Sender).OptionsView.GridLineColor, ACategoryViewInfo, ACategoryViewInfo.HeaderCellsRect, ALineInfo);
end;
ACategoryViewInfo.LinesInfo.Clear;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -