gpt4 book ai didi

delphi - 在 DBGrid 中使用 Canvas.TextOut - Delphi XE 3

转载 作者:行者123 更新时间:2023-12-02 00:51:46 54 4
gpt4 key购买 nike

请,我需要有关 Canvas 使用的帮助:当用户将鼠标移动到 DBGrid 的标题列上时,标题描述在 Delphi XE 3 中消失。delphI 7 中不会出现此问题。

请按照以下代码操作:

unit Unit1;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Graphics, Controls, Forms, Dialogs, Data.DB, Datasnap.DBClient,
Grids, DBGrids, Types, StdCtrls;

type
TAccessDBGrid = class(TCustomGrid);

type
TForm1 = class(TForm)

DataSource1: TDataSource;
grid1: TDBGrid;
cdsTabela: TClientDataSet;
cdsTabelacodigo_1: TIntegerField;
cdsTabelacodigo_2: TIntegerField;`enter code here`

procedure grid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);

procedure FormCreate(Sender: TObject);

procedure FormResize(Sender: TObject);

procedure grid1TitleClick(Column: TColumn);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
cdsTabela.CreateDataSet;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
grid1.Refresh;
end;

procedure TForm1.grid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
S1 : string;

begin

with TAccessDBGrid(grid1) do
begin
RowHeights[0] := 29;
Canvas.Brush.Color := clBtnFace;
case Column.Index of
0:
begin
Column.Title.Caption := '';
S1 := 'Code';
end;
1:
begin
Column.Title.Caption := '';
S1 := 'Description';
end;
end;

TDBGrid(Sender).Canvas.Font.Color:= clBlack;
Canvas.TextOut(Rect.Left + 3, 19, S1);
end;
end;

procedure TForm1.grid1TitleClick(Column: TColumn);
begin
ShowMessage('Title Click! ');
end;

end

有关更多信息,请参阅我发布的答案。

最佳答案

您发布的答案中包含的额外信息使您更容易理解您想要做什么,这很好。请在以后的问题中尝试这样做,因为这样您更有可能更快地获得更好的答案。

无论如何,我建议您将 TDBGrid 替换为 Mike Shkolnik 的 TSMDBGrid。请参阅:http://www.scalabium.com/smdbgrid.htm并观看动画。

他的网格包含一个 OnDrawColumnTitle 事件,我认为这比尝试使用 TDBGrid 更容易实现您想要的目标。我从您的评论中得知您已遵循此建议并成功完成了您想要做的事情。

原答案如下:

我同意肯·怀特所说的,但除此之外,我认为你的代码是被误解的。要明白我的意思,请尝试以下操作:

  1. 保存 IDE 调试布局,其中代码编辑器不与你的表格1。这样做的目的是为了让 Form1 不会被迫重新绘制IDE 在断点处停止。

  2. DefaultDrawing 设置为 False。这样做的要点是将其设置为True 隐藏了您的 Grid1DrawColumnCell 的损坏程度。

  3. Grid1DrawColumnCell 的第一行放置一个断点,即与 TAccessDBGrid(grid1)

  4. 编译并运行。请注意,调试器在断点处停止了几个当表单被绘制在屏幕上时,但之后就不再出现了完全停止你的血压。因此,一旦表单出现在屏幕上,您的自定义绘画就永远不会发生,直到您导致网格刷新,例如使用表单的 OnResize 处理程序。

  5. 那么是什么原因导致移动鼠标后标题标题列变为空白在网格上。答案是,你愿意!看看为什么......

  6. 在VCL.DBGrids.Pas中,找到TCustomDBGrid.DrawCell中的DrawTitleCell,并放置一个BPWriteText( Canvas ,TextRect,LFrameOffs,LFrameOffs,标题,对齐方式,...

  7. 再次运行应用程序,直至执行 Grid1DrawColumnCell 的位置并且调试器在步骤 6 中设置的 WriteText BP 上停止。评估 Caption您将看到它是空的(当然,因为您的 Grid1DrawColumnCell 已清除它)。我想您可以在 DrawColumnCell 退出之前将您的 S1 值分配给列标题的 Caption,但是绘制它确实仍然会很困惑,如果你尝试例如,你会发现

    case Column.Index of
    0: begin
    // Column.Title.Caption := '';
    S1 := 'Code';
    Column.Title.Caption := S1;
    end;
    1: begin
    // Column.Title.Caption := '';
    S1 := 'Description';
    Column.Title.Caption := S1;
    end;

QED

所以基本上你是在浪费时间在 Grid1DrawColumnCell 上。如果你想知道是否可以自定义绘制列标题/标题,如果可以,如何绘制,我建议你在做了一些研究后问一个新的问题。如果您没有找到任何内容,您可能会考虑派生 TCustomDBGrid 后代并覆盖其 DrawCell;这样您就可以更好地控制整个绘图过程,并可能更接近您想要实现的目标。

顺便说一句,上面是基于在 Delphi Seatlle 中编译的应用程序,它比 XE3 更新,但我怀疑这有什么区别。

关于delphi - 在 DBGrid 中使用 Canvas.TextOut - Delphi XE 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44147805/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com