关于TControl焦点获取
一、焦点有效的条件:系统可视化控件(组件)的焦点属性
Canfocus --- 不是所有的组件,设计面板上Object Inspector -- Properties都显式的给出啦
:需要你去设置控件的Canfocus属性=true,焦点才能生效
二、设置焦点的方法
1、方法1:窗体TForm强制设置其下可视控件的焦点
AButton.Canfocus:=true;
Self.SetFocused(AButton);
2、方法2:通用调用方法,与窗体无关
///<summary>强制设置TControl焦点,返回是否获得焦点:</summary>
///<param name="ATControl">uses FMX.Controls,FMX.Objects;</param>
function FocusMe(const ATControl:TControl):Boolean;
begin
ATControl.CanFocus:=true;
ATControl.SetFocus;
if ATControl.IsFocused then
Result:=true
else Result:=false;
end;
调用方法:
2.1、直接调用:
FocusMe(Image1) ;
2.2、获取焦点并加判断提示或做点什么:
if FocusMe(Image1) then
ShowAMessage(Image1.Name+'获取了焦点',procedure begin end );
2.3、案例:
procedure TfmxMergeImageAndWords.ControlAction(
Sender: TObject);
begin
if (Sender as TControl) is TSpeedButton then
begin
if Sender=btnHome then close;
//......
end;
if (Sender as TControl) is TImage then
begin
//FocusMe(Sender as TControl);
if FocusMe(Sender as TControl) then ShowAMessage((Sender as TControl).Name+'获取了焦点',procedure begin end );
end;
end;