删除焦点rect on control - Delphi

时间:2009-07-27 20:54:07

标签: delphi delphi-2009

有没有选项不在Delphi 2009中的控件上绘制“focus rect”?

-Pavan。

3 个答案:

答案 0 :(得分:2)

对此的解决方案取决于控件的继承。有些需要覆盖Paint方法,有些则需要拥有者。我不知道一般的解决方案。

对于某些Raize components,您可以将ShowFocusRect属性设置为false - 制作精良的组件的好处之一。

有些人会说你提出的建议并不是一个好主意 - 认为焦点矩形是标准Windows用户界面的一部分(你会发现相关的讨论here)。我确信在某些情况下可以覆盖行为。

答案 1 :(得分:0)

这是使用来自this newsgroup post的ownerdraw来抑制StringGrid中的焦点矩形的示例。这对于在Paint方法中绘制焦点矩形的控件不起作用。

将默认绘图设置为false,将其附加到OnDrawCell事件:

procedure TMiscForm.StringGrid1DrawCell(Sender: TObject;
          ACol, ARow: Integer;
          Rect  : TRect;
          State : TGridDrawState);
var 
  SG: TStringGrid;
begin
  if Sender is TStringGrid then 
  begin
    SG:= TStringGrid(Sender);
    SG.Canvas.Font:= SG.Font;
    SG.Canvas.Brush.Color:= SG.Color;
    SG.Canvas.Brush.Style:= bsSolid;

    if gdFixed in State then 
      SG.Canvas.Brush.Color:= SG.FixedColor;

    if  (gdSelected in State) and not (gdFocused  in State) then 
    begin
      SG.Canvas.Brush.Color:= clHighLight;
      SG.Canvas.Font.color := clHighLightText;
    end;

    SG.Canvas.Pen.Color  := SG.Canvas.Brush.Color;
    SG.Canvas.Pen.Mode   := pmCopy;
    SG.Canvas.Pen.Style  := psSolid;
    SG.Canvas.Pen.Width  := 1;
    SG.Canvas.Rectangle(Rect);

    if SG.Canvas.Ctl3D and (gdFixed in State) then 
    begin
      if goFixedVertLine in SG.Options then 
      begin
        SG.Canvas.Pen.Color  := clBtnHighLight;
        MoveTo(Rect.Left,  Rect.Bottom-1);
        LineTo(Rect.Left,  Rect.Top);
        SG.Canvas.Pen.Color  := clBtnShadow;
        MoveTo(Rect.Right-1,  Rect.Top);
        if goFixedHorzLine in SG.Options then 
          LineTo(Rect.Right-1, Rect.Bottom)
        else LineTo(Rect.Right-1, Rect.Bottom+SG.GridLineWidth);
      end;

      if goFixedHorzLine in SG.Options then 
      begin
        SG.Canvas.Pen.Color  := clBtnHighLight;
        MoveTo(Rect.Left,  Rect.Top);
        LineTo(Rect.Right, Rect.Top);
        SG.Canvas.Pen.Color  := clBtnShadow;
        if goFixedVertLine in SG.Options then 
        begin
          MoveTo(Rect.Left+1, Rect.Bottom-1);
          LineTo(Rect.Right,  Rect.Bottom-1)
        end 
        else 
        begin
          MoveTo(Rect.Left, Rect.Bottom-1);
          LineTo(Rect.Right + SG.GridLineWidth, Rect.Bottom-1);
        end;
      end;
    end;

    SG.Canvas.Brush.Style:= bsClear;
    TextRect(Rect, Rect.Left + 2, Rect.Top + 2, SG.Cells[ACol,ARow]);

    SG.Canvas.Brush.Style:= bsSolid;
    if gdFocused in State then 
      SG.Canvas.DrawFocusRect(Rect);
  end;
end;

答案 2 :(得分:0)

我知道这是一个老问题,但如果您仍然感兴趣,可以随时尝试

procedure TfrmPic.MyStringGridDrawCell(Sender: TObject;
  ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  // Deliberately draw focus rectangle which is subsequently redrawn;
  if gdSelected in State then 
    MyStringGrid.Canvas.DrawFocusRect(Rect);
end; { procedure TfrmPic.sgCorDrawCell }