我试图隐藏Delphi在StringGrid中当前选定单元格周围绘制的边框(焦点矩形)。我正在做所有者绘图,以定制字符串网格的外观。我已经设法摆脱了所有的东西,除了选择。
我用的是
GR.Left := -1;
GR.Top := -1;
GR.Right := -1;
GR.Bottom := -1;
StringGrid.Selection := GR;但是如果你把这个设置得很快,就会产生错误(我在onMouseMove中运行了这个)。我的意思是它工作得很好,但是如果我足够快地调用这段特定的代码,我会在StringGrid的呈现中得到一个异常(因此,我不能只是尝试一下,除非绕过它)。
关于如何可靠地解决这个问题,有什么想法吗?
发布于 2013-05-23 18:24:37
您可以使用TStringgrid的插入器类,并覆盖绘制过程以移除绘制的焦点矩形。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TStringgrid=Class(Grids.TStringGrid)
private
FHideFocusRect: Boolean;
protected
Procedure Paint;override;
public
Property HideFocusRect:Boolean Read FHideFocusRect Write FHideFocusRect;
End;
TForm2 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TStringgrid.Paint;
var
L_Rect:Trect;
begin
inherited;
if HideFocusRect then
begin
L_Rect := CellRect(Col,Row);
if DrawingStyle = gdsThemed then InflateRect(L_Rect,-1,-1);
DrawFocusrect(Canvas.Handle,L_Rect)
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
StringGrid1.HideFocusRect := not StringGrid1.HideFocusRect;
end;
end.发布于 2015-03-31 09:28:29
在OnDrawCell事件中添加
with Sender as TStringgrid do
begin
if (gdSelected in State) then
begin
Canvas.Brush.Color := Color;
Canvas.Font.Color := Font.Color;
Canvas.TextRect(Rect, Rect.Left +2,Rect.Top +2, Cells[Col,Row]);
end;
end;在OnSelectCell事件中添加
CanSelect := False
发布于 2015-08-22 07:33:32
将DefaultDrawing属性设置为false,onDrawCell事件将绘制类似如下的文本(这也会交替使用行颜色和单元格中的居中文本):
procedure GridDrawCell(Sender: TObject; ACol,ARow: Integer; Rect: TRect; State: TGridDrawState);
var S: string;
c:TColor;
SavedAlign: word;
begin
S := Grid.Cells[ACol, ARow];
SavedAlign := SetTextAlign(Grid.Canvas.Handle,TA_CENTER);
if (ARow mod 2 = 0)
then c:=clWhite
else c:=$00E8E8E8;
// Fill rectangle with colour
Grid.Canvas.Brush.Color := c;
Grid.Canvas.FillRect(Rect);
// Next, draw the text in the rectangle
if (ACol=1) or (ACol=3) or (ACol=5) then
begin
Grid.Canvas.Font.Color := $005F5F5F;
end
else
begin
Grid.Canvas.Font.Color := $005F5F5F;
end;
Grid.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
SetTextAlign(Grid.Canvas.Handle, SavedAlign);
end;https://stackoverflow.com/questions/16703691
复制相似问题