How can I avoid the caption of a TToggleSwitch
control from displaying the selection dashes on its caption upon click?
I've tried ActiveControl := nil;
after the click event, but it still displays the selection for a moment while clicking on it.
How can I avoid the caption of a TToggleSwitch
control from displaying the selection dashes on its caption upon click?
I've tried ActiveControl := nil;
after the click event, but it still displays the selection for a moment while clicking on it.
You could use an interposer declaration with a message handler for WM_QUERYUISTATE, like this:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.WinXCtrls;
type
TToggleSwitch = class(Vcl.WinXCtrls.TToggleSwitch)
private
procedure WMQueryUIState(var Msg: TMessage); message WM_QUERYUISTATE;
end;
TForm1 = class(TForm)
ToggleSwitch1: TToggleSwitch;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TToggleSwitch }
procedure TToggleSwitch.WMQueryUIState(var Msg: TMessage);
begin
Msg.Result := UISF_HIDEFOCUS;
end;
end.
Shame there isn't a property for it.
This is a focus rectangle which indicates that the control has keyboard focus. These visual hints are very important for users who prefer to use the keyboard over the mouse1, like myself (much faster and more ergonomic), and in situations where you cannot use a pointing device for some reason (that do happen).
When I see the focus rectangle in your toggle switch, I know that that control has keyboard focus and that I can toggle it by pressing spacebar. Had this rectangle not been there, I wouldn't have dared pressing spacebar2. And if you try to set the focus away from the control as soon as it receives keyboard focus, you would make it impossible for me to use the form without the mouse.
Thus, this rectangle is important. You shouldn't put aesthetics before functionality and accessibility.
In Windows XP, if I recall correctly, Microsoft realised that most people aren't like me. Instead, they prefer to use the mouse and do indeed find focus rectangles and underlined letters annoying. Hence, they chose to hide these things until the user indicates that (s)he wants to use the keyboard. For instance, you might have noticed that underlined characters in dialog boxes often do not appear until you press the Alt key.
This you can do. And as far as I can see, the TToggleSwitch
control does follow these rules by default, so you shouldn't need to change anything.
Dave's approach seems to use the related API to unconditionally disable the focus rectangle, which I strongly advice against. If I am not mistaken, Raymond Chen has blogged about this topic. (Maybe here.) Never do that in professional software.
1 For keyboard users, the most important things in a form is that all controls are accessible using the keyboard only, and in a convenient way. For instance, the tab order is very important. Pressing the tab key should take you from a control to its neighbour to the right or below, not jumping chaotically across the screen. Also, keyboard accelerators (like &Save
, or &Colour:
) should always be present, and the TButton.Default
and TButton.Cancel
properties should always be set where appropriate.
This might sound like a rant, but I suffer from these things daily! :)
2 Because I can't be sure that it actually is this toggle switch control that is the currently focused control. I mean, it looks the same with and without focus! It could be focused, or the focused control might be some button far away on the screen, or some other control that also misbehaves by not drawing a focus rectangle. This other control might well be the "quit without saving" button.
That dash line around button text is used to mark selecting the button using Tab selection.
So setting TabStop
property to False
will prevent those doted lines to appear.
But do note that this will also prevent anyone for using TAB key to move focus to this button and thus use keyboard to press/toggle this button.