My answer below does not answer DIRECTLY your question, but it is still relevant because you rely on CanFocus. CanFocus returns a lie. You should not rely on it. The documentation is also wrong. More exactly, CanFocus can return True even if the control is not focusable. In this case, an exception will be raised.
PS: Under Lazarus CanFocus works properly.
So, you will have to write a new CanFocus that tests not only the state of the current control but ALL its parents. You will end up with a recursive call.
Only then you can write a new SetFocus that really works:
procedure SetFocus(Control: TWinControl);
begin
if CanFocusFixed(Control)
then Control.SetFocus;
end;
本条中的充分编码和解释:Setfocus-is-broken-in-Delphi。
(SO 规则规定,我必须宣布我附属于该网站,以便:把我的网站带上)
理由:
J provided a nice answer, but PERSONALLY I don t like class helpers because if you have more than one class helper for the same class, then only one will be used - which one is determined (almost randomly) by the order of the units in the "uses" clause. Accidentally change the order of the units in the "uses" and you accidentally change the behavior of your program! I don t like this amount of randomness in a programming language.
Until Emba comes with a stable solution, I won t use Class Helpers.