Avatar of ebi1
ebi1Flag for Israel

asked on 

how to cancel the the focus rectangle mark?

hi all

i want that my program will NOT show the rectangle mark of a fucused control.
how can i(you) do it please?

in other words, i want my program to act normally, but without the focus "dotted" rectangle mark.
Delphi

Avatar of undefined
Last Comment
Lukasz Zielinski
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

listening
on which controls?

ziolko
Avatar of ebi1
ebi1
Flag of Israel image

ASKER

i preffer all controls, but if it's not possible, then i need at least the buttons(TBitBtn, TButton)
thanks
all controls is not possible for sure as different controls may be written in different ways, when it comes to TButton and TBitButton should be possible... I'll take a look

ziolko.
ok here's button:

uses Themes, DwmApi, UxTheme;

  TMyButton = class(TButton)
  private
    FCanvas: TCanvas;
    IsFocused: Boolean;
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(const DrawItemStruct: TDrawItemStruct);
    procedure DrawButtonText(Canvas: TCanvas; const Caption: string;TextBounds: TRect; State: TButtonState; Flags: LongInt);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure SetButtonStyle(ADefault: Boolean); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;


{ TMyButton }

procedure TMyButton.CNDrawItem(var Message: TWMDrawItem);
begin
  DrawItem(Message.DrawItemStruct^);
end;

constructor TMyButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FCanvas := TCanvas.Create;  
end;

destructor TMyButton.Destroy;
begin
  FCanvas.Free;
  inherited Destroy;
end;

procedure TMyButton.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do Style := Style or BS_OWNERDRAW;
end;

procedure TMyButton.DrawItem(const DrawItemStruct: TDrawItemStruct);
const
  WordBreakFlag: array[Boolean] of Integer = (0, DT_WORDBREAK);
var
  IsDown, IsDefault: Boolean;
  State: TButtonState;
  R: TRect;
  Flags: Longint;
  Details: TThemedElementDetails;
  Button: TThemedButton;
  Offset: TPoint;
  PaintOnGlass: Boolean;
  LForm: TCustomForm;
  Options: TDTTOpts;
begin
  FCanvas.Handle := DrawItemStruct.hDC;
  R := ClientRect;

  with DrawItemStruct do begin
    FCanvas.Handle := hDC;
    FCanvas.Font := Self.Font;
    IsDown := itemState and ODS_SELECTED <> 0;
    IsDefault := itemState and ODS_FOCUS <> 0;

    if not Enabled then
      State := bsDisabled
    else if
      IsDown then State := bsDown
    else
      State := bsUp;
  end;

  if ThemeControl(Self) then begin
    if not Enabled then
      Button := tbPushButtonDisabled
    else if IsDown then
      Button := tbPushButtonPressed
    else if IsFocused or IsDefault then
      Button := tbPushButtonDefaulted
    else
      Button := tbPushButtonNormal;

    Details := ThemeServices.GetElementDetails(Button);
    PaintOnGlass := ThemeServices.ThemesEnabled and DwmCompositionEnabled and not (csDesigning in ComponentState);
    if PaintOnGlass then begin
      LForm := GetParentForm(Self);
      PaintOnGlass := (LForm <> nil) and LForm.GlassFrame.FrameExtended and LForm.GlassFrame.IntersectsControl(Self);
    end;

    if not PaintOnGlass then
      ThemeServices.DrawParentBackground(Handle, DrawItemStruct.hDC, @Details, True)
    else begin
      InflateRect(R, -2, -2);
      FillRect(DrawItemStruct.hDC, R, GetStockObject(BLACK_BRUSH));
    end;

    ThemeServices.DrawElement(DrawItemStruct.hDC, Details, DrawItemStruct.rcItem);
    R := ThemeServices.ContentRect(FCanvas.Handle, Details, DrawItemStruct.rcItem);

    if Button = tbPushButtonPressed then
      Offset := Point(1, 0)
    else
      Offset := Point(0, 0);
    if PaintOnGlass then
      State := TButtonState(Integer(State) or 4);
    FillChar(Options, SizeOf(Options), 0);
    Options.dwSize := SizeOf(Options);
    Options.dwFlags := DTT_TEXTCOLOR or DTT_COMPOSITED;
    Options.crText := ColorToRGB(FCanvas.Font.Color);

    OffsetRect(R, 0, 2);
    DrawButtonText(FCanvas, Caption, R, State, DrawTextBiDiModeFlags(0) or WordBreakFlag[WordWrap]);

    if IsFocused and IsDefault then begin
      FCanvas.Pen.Color := clWindowFrame;
      FCanvas.Brush.Color := clBtnFace;
    end;
  end else begin
    R := ClientRect;

    Flags := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
    if IsDown then
      Flags := Flags or DFCS_PUSHED;
    if DrawItemStruct.itemState and ODS_DISABLED <> 0 then
      Flags := Flags or DFCS_INACTIVE;

    if IsFocused or IsDefault then begin
      FCanvas.Pen.Color := clWindowFrame;
      FCanvas.Pen.Width := 1;
      FCanvas.Brush.Style := bsClear;
      FCanvas.Rectangle(R.Left, R.Top, R.Right, R.Bottom);

      InflateRect(R, -1, -1);
    end;

    if IsDown then begin
      FCanvas.Pen.Color := clBtnShadow;
      FCanvas.Pen.Width := 1;
      FCanvas.Brush.Color := clBtnFace;
      FCanvas.Rectangle(R.Left, R.Top, R.Right, R.Bottom);
      InflateRect(R, -1, -1);
    end else
      DrawFrameControl(DrawItemStruct.hDC, R, DFC_BUTTON, Flags);

    if IsFocused then begin
      R := ClientRect;
      InflateRect(R, -1, -1);
    end;

    FCanvas.Font := Self.Font;
    if IsDown then
      OffsetRect(R, 1, 1);
    Windows.DrawText(FCanvas.Handle, PChar(Caption), Length(Caption), R, DT_CENTER or DT_VCENTER);

    if IsFocused and IsDefault then begin
      R := ClientRect;
      InflateRect(R, -4, -4);
      FCanvas.Pen.Color := clWindowFrame;
      FCanvas.Brush.Color := clBtnFace;
    end;
  end;

  FCanvas.Handle := 0;
end;

procedure TMyButton.SetButtonStyle(ADefault: Boolean);
begin
  if ADefault <> IsFocused then begin
    IsFocused := ADefault;
    Refresh;
  end;
end;

procedure TMyButton.DrawButtonText(Canvas: TCanvas; const Caption: string;TextBounds: TRect; State: TButtonState; Flags: LongInt);
var
  PaintOnGlass: Boolean;

  procedure DoDrawText(DC: HDC; const Text: string; TextLen: Integer;var TextRect: TRect; TextFlags: Cardinal);
  var
    Options: TDTTOpts;
  begin
    if PaintOnGlass then begin
      FillChar(Options, SizeOf(Options), 0);
      Options.dwSize := SizeOf(Options);
      Options.dwFlags := DTT_TEXTCOLOR or DTT_COMPOSITED;
      Options.crText := ColorToRGB(Canvas.Font.Color);

      with ThemeServices.GetElementDetails(tbPushButtonNormal) do
        DrawThemeTextEx(ThemeServices.Theme[teButton], DC, Part, State, PWideChar(WideString(Text)), Length(WideString(Text)),TextFlags, @TextRect, Options);
    end else
     Windows.DrawText(DC, PChar(Text), TextLen, TextRect, TextFlags);
  end;

begin
  PaintOnGlass := Integer(State) and 4 = 4;
  if PaintOnGlass then
    State := TButtonState(Integer(State) and 3);
  with Canvas do begin
    Brush.Style := bsClear;
    if State = bsDisabled then begin
      OffsetRect(TextBounds, 1, 1);
      Font.Color := clBtnHighlight;
      DoDrawText(Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or Flags);
      OffsetRect(TextBounds, -1, -1);
      Font.Color := clBtnShadow;
      DoDrawText(Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or Flags);
    end else
      DoDrawText(Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or Flags);
  end;
end;


ziolko.
Avatar of Geert G
Geert G
Flag of Belgium image

i have to agree with ziolko.
for every control there is a different approach.
Some controls even implement a property to switch it on or off, like ShowFocusRect

But like Ziolko displayed, you need to look into the Paint routine of the control and find out how the FocusRectange is drawn.
If possible override it with your own procedure or create a new control.
the new control needs to be installed on your pallet and then change the controls on your forms to the newly installed components
in addition to Geert's comment, some controls have Paint() method but others like button are really windows controls so you have to use BS_OWNERDRAW paramter and write your own painting procedure.
the code I gave could be a lot simpler if you don't want themed controls, otherwise it's a bit more complicated

ziolko.
Avatar of ebi1
ebi1
Flag of Israel image

ASKER

WOW ziolko, you really put your mind in it  :)

thank you guys for your kind help.

ziolko, i'm not a real Delphi programmer, can you make it shorter only for the buttons(and TBitbtn) please?

except for the Themes, i dont know the DwmApi unit, and the UxTheme unit.
i use D6.
are those units from higher version of Delphi?

thanks again guys
(and sorry for my english...)
Avatar of ebi1
ebi1
Flag of Israel image

ASKER

i forgot to add that i don't want themed controls.
i just need a BitBtn(or a TButton), with a glyph on it, so it will look like a button of a game, with no focus mark.
thanks again
sure I can:)
I'll re-write it on monday when I get back to office

ziolko.
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ebi1
ebi1
Flag of Israel image

ASKER

wow this is really great.
thanks

so i just make a "new" component?
do i need to make another like this but for BitBtn too?

thanks again
yeah the best way is to install it as new component, for bitbtn very similar but you have to add painting icon

ziolko.
Avatar of ebi1
ebi1
Flag of Israel image

ASKER

thank you my friend
i wish i could give more points, but there is a harder question i'm about to post, and i have less then 500 points...

thanks again. you helped a lot  :)
glad I could help:)

ziolko.
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo