delphi 设置默认控件得到焦点

如果同一窗体有多个按钮的话,追踪源码发现最后是taborder来的

如: 在空白窗体上拖入两个button (btn1,btn2) 

如果在btn2设置default = True 运行后,默认焦点还是在btn1上。需要把btn2的taborder设置为0才可以。

如此看来还是以taborder 来认的。

 1 procedure TCustomForm.SetActive(Value: Boolean);
 2 begin
 3   FActive := Value;
 4   if FActiveOleControl <> nil then
 5     FActiveOleControl.Perform(CM_DOCWINDOWACTIVATE, Ord(Value), 0);
 6   if Value then
 7   begin
 8     if (ActiveControl = nil) and not (csDesigning in ComponentState) then
 9       ActiveControl := FindNextControl(nil, True, True, False);
10     MergeMenu(True);
11     SetWindowFocus;
12   end;
13 end;

其中的 FindNextControl 可以看出是gettaborderlist来认的

 1 function TWinControl.FindNextControl(CurControl: TWinControl;
 2   GoForward, CheckTabStop, CheckParent: Boolean): TWinControl;
 3 var
 4   I, StartIndex: Integer;
 5   List: TList;
 6 begin
 7   Result := nil;
 8   List := TList.Create;
 9   try
10     GetTabOrderList(List);
11     if List.Count > 0 then
12     begin
13       StartIndex := List.IndexOf(CurControl);
14       if StartIndex = -1 then
15         if GoForward then StartIndex := List.Count - 1 else StartIndex := 0;
16       I := StartIndex;
17       repeat
18         if GoForward then
19         begin
20           Inc(I);
21           if I = List.Count then I := 0;
22         end else
23         begin
24           if I = 0 then I := List.Count;
25           Dec(I);
26         end;
27         CurControl := List[I];
28         if CurControl.CanFocus and
29           (not CheckTabStop or CurControl.TabStop) and
30           (not CheckParent or (CurControl.Parent = Self)) then
31           Result := CurControl;
32       until (Result <> nil) or (I = StartIndex);
33     end;
34   finally
35     List.Free;
36   end;
37 end;
原文地址:https://www.cnblogs.com/BTag/p/13972769.html