Hi,
Is there any option to not draw "focus rect" on a control in Delphi 2009?
-Pavan.
Hi,
Is there any option to not draw "focus rect" on a control in Delphi 2009?
-Pavan.
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.
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;