首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏StringGrid的focusRectangle : Delphi

隐藏StringGrid的focusRectangle : Delphi
EN

Stack Overflow用户
提问于 2013-05-23 08:21:31
回答 4查看 6.3K关注 0票数 4

我试图隐藏Delphi在StringGrid中当前选定单元格周围绘制的边框(焦点矩形)。我正在做所有者绘图,以定制字符串网格的外观。我已经设法摆脱了所有的东西,除了选择。

我用的是

代码语言:javascript
复制
 GR.Left := -1;
 GR.Top  := -1;
 GR.Right := -1;
 GR.Bottom := -1;
 StringGrid.Selection := GR;

但是如果你把这个设置得很快,就会产生错误(我在onMouseMove中运行了这个)。我的意思是它工作得很好,但是如果我足够快地调用这段特定的代码,我会在StringGrid的呈现中得到一个异常(因此,我不能只是尝试一下,除非绕过它)。

关于如何可靠地解决这个问题,有什么想法吗?

EN

回答 4

Stack Overflow用户

发布于 2013-05-23 18:24:37

您可以使用TStringgrid的插入器类,并覆盖绘制过程以移除绘制的焦点矩形。

代码语言:javascript
复制
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.
票数 3
EN

Stack Overflow用户

发布于 2015-03-31 09:28:29

OnDrawCell事件中添加

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2015-08-22 07:33:32

DefaultDrawing属性设置为false,onDrawCell事件将绘制类似如下的文本(这也会交替使用行颜色和单元格中的居中文本):

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16703691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档