1

Is there any option to not draw "focus rect" on a control in Delphi 2009?

-Pavan.

1
  • 2
    Which control? Relevant details make for better answers.
    – Argalatyr
    Jul 27, 2009 at 22:35

3 Answers 3

2

The solution for this varies depending on the inheritance of the control. Some require an override of the Paint method, others require an ownerdraw. I don't know of a general solution.

For some Raize components there is a ShowFocusRect property that you can set to false - one of the benefits of well-made components.

Some would say that what you propose is not a good idea - arguing that the focus rectangle is part of the standard Windows user interface (you will find a relevant discussion here). I'm sure there's a case to be made for overriding the behavior in some situations.

0

This is an example for suppressing the focus rectangle in a StringGrid using ownerdraw, from this newsgroup post. This will not work for controls for which the focus rectangle is drawn in the Paint method.

Set default drawing to false, attach this to the OnDrawCell event:

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;
0

I know this is an old question but if your still interested you can always try

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 }
1
  • Will this work generally? The OP did not specify inheritance of the object in question.
    – Argalatyr
    Feb 2, 2016 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.