Rab*_*wal 2 delphi tstringgrid delphi-xe8
我正在尝试编写自定义日期选择器(日历)。日期将显示在字符串网格上。我正在尝试用自定义颜色填充单击的单元格并使所选的单元格文本变为粗体。
这是我的代码:
type
TStringGrid = Class(Vcl.Grids.TStringGrid)
private
FHideFocusRect: Boolean;
protected
Procedure Paint;override;
public
Property HideFocusRect:Boolean Read FHideFocusRect Write FHideFocusRect;
End;
TfrmNepaliCalendar = class(TForm)
...
...
...
end;
procedure TfrmNepaliCalendar.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if gdSelected in State then begin
StringGrid.Canvas.Brush.Color := $00940A4B;
StringGrid.Canvas.FillRect(Rect);
StringGrid.Canvas.Font.Style := [fsBold];
StringGrid.Canvas.Font.Color := clHighlightText;
StringGrid.Canvas.TextOut(Rect.Left + 3, Rect.Top + 5, StringGrid.Cells[ACol,ARow]);
StringGrid.HideFocusRect := True;
end;
end;
{ TStringGrid }
procedure TStringGrid.Paint;
var
LRect: TRect;
begin
inherited;
if HideFocusRect then begin
LRect := CellRect(Col,Row);
if DrawingStyle = gdsThemed then InflateRect(LRect,-1,-1);
DrawFocusrect(Canvas.Handle,LRect)
end;
end;
Run Code Online (Sandbox Code Playgroud)
我得到的输出:
问题#1:我需要隐藏作为所选单元格边框出现的不需要的矩形
问题#2:避免单元格背景剪切
在 OnDrawCell 过程之前添加FillRect
Rect.Left := Rect.Left-4;
Run Code Online (Sandbox Code Playgroud)
似乎有效。
替代
即使使用您的绘画程序插件,上述内容也不能完全解决焦点问题。有时,单元格边框内会出现一条白线。
但以下是一个替代方案,可以解决您的两个问题。它需要更多的编码,但不需要那么多。另一方面,不需要子类化 TStringGrid,也不需要Rect调整
基础是禁用默认绘制,因此设置 grids 属性DefaultDrawing := false;
,然后添加到 OnDrawCell 事件中:
procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if gdFixed in State then
begin
StringGrid.Canvas.Brush.Color := clGradientInactiveCaption;
StringGrid.Canvas.Font.Style := [];
StringGrid.Canvas.Font.Color := clBlack;
end
else
if gdSelected in State then
begin
StringGrid.Canvas.Brush.Color := $00940A4B;
StringGrid.Canvas.Font.Style := [fsBold];
StringGrid.Canvas.Font.Color := clHighlightText;
end
else
begin
StringGrid.Canvas.Brush.Color := $00FFFFFF;
StringGrid.Canvas.Font.Style := [];
StringGrid.Canvas.Font.Color := clWindowText;
end;
StringGrid.Canvas.FillRect(Rect);
StringGrid.Canvas.TextOut(Rect.Left + 3, Rect.Top + 5, StringGrid.Cells[ACol,ARow]);
end;
Run Code Online (Sandbox Code Playgroud)
禁用默认绘图后,网格将绘制网格框和网格线,但将所有其他绘图留给程序员。需要注意的是,如果需要,您必须自己添加精美的主题图画。通过上面的编码我得到这个结果: