我的Firemonkey APP中有很多布局,所有TVertscrollBox共享相同的DoCalcContentsBounds,DoRestorePosition,DoUpdateKBBounds,如下所示,我想减少它并保持相同的功能,我该怎么办?
procedure TBTCXCHGForm.BTCXCHGDoRestorePosition;
begin
if BTCXCHGAddSalesVertScrollBox.Visible then
begin
with BTCXCHGAddSalesVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGEnterSellerDetailVertScrollBox.Visible then
begin
with BTCXCHGEnterSellerDetailVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGEnterBuyBTCDetailVertScrollBox.Visible then
begin
with BTCXCHGEnterBuyBTCDetailVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGAddBuyVertScrollBox.Visible then
begin
with BTCXCHGAddBuyVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGChangeSalesConfirmationVertScrollBox.Visible then
begin
with BTCXCHGChangeSalesConfirmationVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGEnterSellBTCDetailVertScrollBox.Visible then
begin
with BTCXCHGEnterSellBTCDetailVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGMinerPurchaseVertScrollBox.Visible then
begin
with BTCXCHGMinerPurchaseVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGReceiveBitcoinsVertScrollBox.Visible then
begin
with BTCXCHGReceiveBitcoinsVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGSendBitcoinsVertScrollBox.Visible then
begin
with BTCXCHGSendBitcoinsVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGUserRegistrationVertScrollBox.Visible then
begin
with BTCXCHGUserRegistrationVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGChangeSecurityPasswordVertScrollBox.Visible then
begin
with BTCXCHGChangeSecurityPasswordVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGChangeUserPasswordVertScrollBox.Visible then
begin
with BTCXCHGChangeUserPasswordVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGLoginRegisterSelectionUserEmailVerificationVertScrollBox.Visible then
begin
with BTCXCHGLoginRegisterSelectionUserEmailVerificationVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGLoginRegisterSelectionUserForgetPasswordVertScrollBox.Visible then
begin
with BTCXCHGLoginRegisterSelectionUserForgetPasswordVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGLoginRegisterSelectionUserLoginVertScrollBox.Visible then
begin
with BTCXCHGLoginRegisterSelectionUserLoginVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end
else
begin
if BTCXCHGLoginRegisterSelectionRegistrationVertScrollBox.Visible then
begin
with BTCXCHGLoginRegisterSelectionRegistrationVertScrollBox do
ViewportPosition := PointF(ViewportPosition.X, 0);
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
procedure TBTCXCHGForm.BTCXCHGDoUpdateKBBounds;
var
LFocused: TControl;
LFocusRect: TRectF;
begin
if BTCXCHGAddSalesVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGAddSalesVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGAddSalesLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGAddSalesVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGEnterSellerDetailVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGEnterSellerDetailVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGEnterSellerDetailLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGEnterSellerDetailVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGEnterBuyBTCDetailVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGEnterBuyBTCDetailVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGEnterBuyBTCDetailLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGEnterBuyBTCDetailVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGAddBuyVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGAddBuyVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGAddBuyLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGAddBuyVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGChangeSalesConfirmationVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGChangeSalesConfirmationVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGChangeSalesConfirmationLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGChangeSalesConfirmationVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGEnterSellBTCDetailVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGEnterSellBTCDetailVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGEnterSellBTCDetailLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGEnterSellBTCDetailVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGMinerPurchaseVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGMinerPurchaseVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGMinerPurchaseLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGMinerPurchaseVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGReceiveBitcoinsVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGReceiveBitcoinsVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGReceiveBitcoinsLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGReceiveBitcoinsVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGSendBitcoinsVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGSendBitcoinsVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGSendBitcoinsLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGSendBitcoinsVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGUserRegistrationVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGUserRegistrationVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGUserRegistrationLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGUserRegistrationVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGChangeSecurityPasswordVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGChangeSecurityPasswordVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGChangeSecurityPasswordLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGChangeSecurityPasswordVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGChangeUserPasswordVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGChangeUserPasswordVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGChangeUserPasswordLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGChangeUserPasswordVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGLoginRegisterSelectionUserEmailVerificationVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGLoginRegisterSelectionUserEmailVerificationVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGLoginRegisterSelectionUserEmailVerificationLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGLoginRegisterSelectionUserEmailVerificationVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGLoginRegisterSelectionUserForgetPasswordVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGLoginRegisterSelectionUserForgetPasswordVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGLoginRegisterSelectionUserForgetPasswordLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGLoginRegisterSelectionUserForgetPasswordVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGLoginRegisterSelectionUserLoginVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGLoginRegisterSelectionUserLoginVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGLoginRegisterSelectionUserLoginLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGLoginRegisterSelectionUserLoginVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end
else
begin
if BTCXCHGLoginRegisterSelectionRegistrationVertScrollBox.Visible then
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(BTCXCHGLoginRegisterSelectionRegistrationVertScrollBox.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGLoginRegisterSelectionRegistrationLayout.Align := TAlignLayout.Horizontal;
with BTCXCHGLoginRegisterSelectionRegistrationVertScrollBox do
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
答案 0 :(得分:0)
在TBTCXCHGForm
中声明一个字段,以保存对当前可见的VertScrollBox的引用。每当更改可见的VertScrollBox时,将滚动框分配给此字段。例如:
CurrentlyVisibleVertScrollBox: TVertscrollBox;
然后您的BTCXCHGDoRestorePosition
程序可以缩减为
procedure TBTCXCHGForm.BTCXCHGDoRestorePosition;
begin
with CurrentlyVisibleVertScrollBox do // <----
ViewportPosition := PointF(ViewportPosition.X, 0);
end;
并且BTCXCHGDoUpdateKBBounds
可以缩减为
procedure TBTCXCHGForm.BTCXCHGDoUpdateKBBounds;
var
LFocused: TControl;
LFocusRect: TRectF;
begin
BTCXCHGNeedOffset := False;
if Assigned(Focused) then
begin
LFocused := TControl(Focused.GetObject);
LFocusRect := LFocused.AbsoluteRect;
LFocusRect.Offset(CurrentlyVisibleVertScrollBox.ViewportPosition); // <----
if (LFocusRect.IntersectsWith(TRectF.Create(BTCXCHGKBBounds))) and (LFocusRect.Bottom > BTCXCHGKBBounds.Top) then
begin
BTCXCHGNeedOffset := True;
BTCXCHGAddSalesLayout.Align := TAlignLayout.Horizontal;
with CurrentlyVisibleVertScrollBox do // <----
begin
RealignContent;
Application.ProcessMessages;
ViewportPosition := PointF(ViewportPosition.X, LFocusRect.Bottom - BTCXCHGKBBounds.Top);
end;
end;
end;
if not BTCXCHGNeedOffset then
BTCXCHGDoRestorePosition;
end;
标记修改的行(// <----)。
你没有发布第三个程序DoCalcContentsBounds
,但如果它遵循类似的模式,那么应该很明显如何修改它。