实现跟设置窗口Align属性同等效果的API有没有?速答速结,谢谢大家! ( 积分: 30 )

  • 主题发起人 主题发起人 YuZi
  • 开始时间 开始时间
Y

YuZi

Unregistered / Unconfirmed
GUEST, unregistred user!
如:实现Align :=alBottom、Align := alLeft的效果的API。
 
如:实现Align :=alBottom、Align := alLeft的效果的API。
 
好像没有,等下面的人<br><br>procedure TWinControl.AlignControls(AControl: TControl; var Rect: TRect);<br>var<br> &nbsp;AlignList: TList;<br><br> &nbsp;function InsertBefore(C1, C2: TControl; AAlign: TAlign): Boolean;<br> &nbsp;begin<br> &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;case AAlign of<br> &nbsp; &nbsp; &nbsp;alTop: Result := C1.Top &lt; C2.Top;<br> &nbsp; &nbsp; &nbsp;alBottom: Result := (C1.Top + C1.Height) &gt;= (C2.Top + C2.Height);<br> &nbsp; &nbsp; &nbsp;alLeft: Result := C1.Left &lt; C2.Left;<br> &nbsp; &nbsp; &nbsp;alRight: Result := (C1.Left + C1.Width) &gt;= (C2.Left + C2.Width);<br> &nbsp; &nbsp; &nbsp;alCustom: Result := CustomAlignInsertBefore(C1, C2);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;procedure DoPosition(Control: TControl; AAlign: TAlign; AlignInfo: TAlignInfo);<br> &nbsp;var<br> &nbsp; &nbsp;NewLeft, NewTop, NewWidth, NewHeight: Integer;<br> &nbsp; &nbsp;ParentSize: TPoint;<br> &nbsp;begin<br> &nbsp; &nbsp;with Rect do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if (AAlign = alNone) or (Control.Anchors &lt;&gt; AnchorAlign[AAlign]) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;with Control do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (FOriginalParentSize.X &lt;&gt; 0) and (FOriginalParentSize.Y &lt;&gt; 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewLeft := Left;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewTop := Top;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewWidth := Width;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewHeight := Height;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Parent.HandleAllocated then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ParentSize := Parent.ClientRect.BottomRight<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ParentSize := Point(Parent.Width, Parent.Height);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if akRight in Anchors then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if akLeft in Anchors then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The AnchorRules.X is the original width<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewWidth := ParentSize.X - (FOriginalParentSize.X - FAnchorRules.X)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The AnchorRules.X is the original left<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewLeft := ParentSize.X - (FOriginalParentSize.X - FAnchorRules.X)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if not (akLeft in Anchors) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The AnchorRules.X is the original middle of the control<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewLeft := MulDiv(FAnchorRules.X, ParentSize.X, FOriginalParentSize.X) -<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewWidth div 2;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if akBottom in Anchors then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if akTop in Anchors then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The AnchorRules.Y is the original height<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewHeight := ParentSize.Y - (FOriginalParentSize.Y - FAnchorRules.Y)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The AnchorRules.Y is the original top<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewTop := ParentSize.Y - (FOriginalParentSize.Y - FAnchorRules.Y)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if not (akTop in Anchors) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The AnchorRules.Y is the original middle of the control<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewTop := MulDiv(FAnchorRules.Y, ParentSize.Y, FOriginalParentSize.Y) -<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewHeight div 2;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FAnchorMove := True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetBounds(NewLeft, NewTop, NewWidth, NewHeight);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FAnchorMove := False;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;if AAlign = alNone then Exit;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp; &nbsp;NewWidth := Right - Left;<br> &nbsp; &nbsp; &nbsp;if (NewWidth &lt; 0) or (AAlign in [alLeft, alRight, alCustom]) then<br> &nbsp; &nbsp; &nbsp; &nbsp;NewWidth := Control.Width;<br> &nbsp; &nbsp; &nbsp;NewHeight := Bottom - Top;<br> &nbsp; &nbsp; &nbsp;if (NewHeight &lt; 0) or (AAlign in [alTop, alBottom, alCustom]) then<br> &nbsp; &nbsp; &nbsp; &nbsp;NewHeight := Control.Height;<br> &nbsp; &nbsp; &nbsp;NewLeft := Left;<br> &nbsp; &nbsp; &nbsp;NewTop := Top;<br> &nbsp; &nbsp; &nbsp;case AAlign of<br> &nbsp; &nbsp; &nbsp; &nbsp;alTop:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(Top, NewHeight);<br> &nbsp; &nbsp; &nbsp; &nbsp;alBottom:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dec(Bottom, NewHeight);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewTop := Bottom;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;alLeft:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(Left, NewWidth);<br> &nbsp; &nbsp; &nbsp; &nbsp;alRight:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dec(Right, NewWidth);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewLeft := Right;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;alCustom:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewLeft := Control.Left;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewTop := Control.Top;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CustomAlignPosition(Control, NewLeft, NewTop, NewWidth,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewHeight, Rect, AlignInfo);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Control.FAnchorMove := True;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Control.SetBounds(NewLeft, NewTop, NewWidth, NewHeight);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;Control.FAnchorMove := False;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;{ Adjust client rect if control didn't resize as we expected }<br> &nbsp; &nbsp;if (Control.Width &lt;&gt; NewWidth) or (Control.Height &lt;&gt; NewHeight) then<br> &nbsp; &nbsp; &nbsp;with Rect do<br> &nbsp; &nbsp; &nbsp; &nbsp;case AAlign of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alTop: Dec(Top, NewHeight - Control.Height);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alBottom: Inc(Bottom, NewHeight - Control.Height);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alLeft: Dec(Left, NewWidth - Control.Width);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alRight: Inc(Right, NewWidth - Control.Width);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alClient:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(Right, NewWidth - Control.Width);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(Bottom, NewHeight - Control.Height);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;function Anchored(Align: TAlign; Anchors: TAnchors): Boolean;<br> &nbsp;begin<br> &nbsp; &nbsp;case Align of<br> &nbsp; &nbsp; &nbsp;alLeft: Result := akLeft in Anchors;<br> &nbsp; &nbsp; &nbsp;alTop: Result := akTop in Anchors;<br> &nbsp; &nbsp; &nbsp;alRight: Result := akRight in Anchors;<br> &nbsp; &nbsp; &nbsp;alBottom: Result := akBottom in Anchors;<br> &nbsp; &nbsp; &nbsp;alClient: Result := Anchors = [akLeft, akTop, akRight, akBottom];<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;procedure DoAlign(AAlign: TAlign);<br> &nbsp;var<br> &nbsp; &nbsp;I, J: Integer;<br> &nbsp; &nbsp;Control: TControl;<br> &nbsp; &nbsp;AlignInfo: TAlignInfo;<br> &nbsp;begin<br> &nbsp; &nbsp;AlignList.Clear;<br> &nbsp; &nbsp;if (AControl &lt;&gt; nil) and ((AAlign = alNone) or AControl.Visible or<br> &nbsp; &nbsp; &nbsp;(csDesigning in AControl.ComponentState) and<br> &nbsp; &nbsp; &nbsp;not (csNoDesignVisible in AControl.ControlStyle)) and<br> &nbsp; &nbsp; &nbsp;(AControl.Align = AAlign) then<br> &nbsp; &nbsp; &nbsp;AlignList.Add(AControl);<br> &nbsp; &nbsp;for I := 0 to ControlCount - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Control := Controls;<br> &nbsp; &nbsp; &nbsp;if (Control.Align = AAlign) and ((AAlign = alNone) or (Control.Visible or<br> &nbsp; &nbsp; &nbsp; &nbsp;(Control.ControlStyle * [csAcceptsControls, csNoDesignVisible] =<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[csAcceptsControls, csNoDesignVisible])) or<br> &nbsp; &nbsp; &nbsp; &nbsp;(csDesigning in Control.ComponentState) and<br> &nbsp; &nbsp; &nbsp; &nbsp;not (csNoDesignVisible in Control.ControlStyle)) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if Control = AControl then Continue;<br> &nbsp; &nbsp; &nbsp; &nbsp;J := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp;while (J &lt; AlignList.Count) and not InsertBefore(Control,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TControl(AlignList[J]), AAlign) do Inc(J);<br> &nbsp; &nbsp; &nbsp; &nbsp;AlignList.Insert(J, Control);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;for I := 0 to AlignList.Count - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;AlignInfo.AlignList := AlignList;<br> &nbsp; &nbsp; &nbsp;AlignInfo.ControlIndex := I;<br> &nbsp; &nbsp; &nbsp;AlignInfo.Align := AAlign;<br> &nbsp; &nbsp; &nbsp;DoPosition(TControl(AlignList), AAlign, AlignInfo);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;function AlignWork: Boolean;<br> &nbsp;var<br> &nbsp; &nbsp;I: Integer;<br> &nbsp;begin<br> &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp;for I := ControlCount - 1 downto 0 do<br> &nbsp; &nbsp; &nbsp;if (Controls.Align &lt;&gt; alNone) or<br> &nbsp; &nbsp; &nbsp; &nbsp;(Controls.Anchors &lt;&gt; [akLeft, akTop]) then Exit;<br> &nbsp; &nbsp;Result := False;<br> &nbsp;end;<br><br>begin<br> &nbsp;if FDockSite and FUseDockManager and (FDockManager &lt;&gt; nil) then<br> &nbsp; &nbsp;FDockManager.ResetBounds(False);<br> &nbsp;{ D5 VCL Change (ME): Aligned controls that are not dock clients now<br> &nbsp; &nbsp;get realigned. &nbsp;Previously the code below was &quot;else if AlignWork&quot;. }<br> &nbsp;if AlignWork then<br> &nbsp;begin<br> &nbsp; &nbsp;AdjustClientRect(Rect);<br> &nbsp; &nbsp;AlignList := TList.Create;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;DoAlign(alTop);<br> &nbsp; &nbsp; &nbsp;DoAlign(alBottom);<br> &nbsp; &nbsp; &nbsp;DoAlign(alLeft);<br> &nbsp; &nbsp; &nbsp;DoAlign(alRight);<br> &nbsp; &nbsp; &nbsp;DoAlign(alClient);<br> &nbsp; &nbsp; &nbsp;DoAlign(alCustom);<br> &nbsp; &nbsp; &nbsp;DoAlign(alNone);// Move anchored controls<br> &nbsp; &nbsp; &nbsp;ControlsAligned;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;AlignList.Free;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;{ Apply any constraints }<br> &nbsp;if Showing then AdjustSize;<br>end;<br><br>procedure TWinControl.AlignControl(AControl: TControl);<br>var<br> &nbsp;Rect: TRect;<br>begin<br> &nbsp;if not HandleAllocated or (csDestroying in ComponentState) then Exit;<br> &nbsp;if FAlignLevel &lt;&gt; 0 then<br> &nbsp; &nbsp;Include(FControlState, csAlignmentNeeded)<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;DisableAlign;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Rect := GetClientRect;<br> &nbsp; &nbsp; &nbsp;AlignControls(AControl, Rect);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;Exclude(FControlState, csAlignmentNeeded);<br> &nbsp; &nbsp; &nbsp;EnableAlign;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;
 
谢谢chenybin,我看过Delphi中的这些代码,我担心没有这直接实现的API,比如:通过设置窗口的风格和扩展风格,但没找到。<br>贴这么多代码可能会把人吓跑了,呵呵,随便说说,别介意。
 
晕,怎么给结。。。。。。
 
后退
顶部