BoundsRules 限制圖案位置和大小
A 範圍規則是一種類別定義的大小和位置的圖形上的限制。 它提供當使用者拖曳圖形角落或側邊的圖形就會重複呼叫的方法。
下列範例會限制為帶有子橫條的水平或垂直的固定大小的矩形圖形。 當使用者拖曳角落或側邊時,大綱會翻轉之間允許的兩種組態的高度和寬度。
範圍規則類別衍生自BoundsRules。 在圖形中建立規則的執行個體:
using Microsoft.VisualStudio.Modeling.Diagrams; ...
public partial class BarShape
{
/// <summary>
/// Rule invoked when the user is resizing a shape.
/// </summary>
public override BoundsRules BoundsRules
{ get { return new BarBoundsRule(); } }
}
/// <summary>
/// Rule invoked when the user is changing a shape's outline.
/// Provides real-time mouse rubber-band feedback, so must work fast.
/// </summary>
public class BarBoundsRule: BoundsRules
{
public override RectangleD GetCompliantBounds
(ShapeElement shape, RectangleD proposedBounds)
{
double thickness = 0.1;
if (proposedBounds.Height > proposedBounds.Width)
{
// There is a minimum width for a shape; the width
// will actually be set to the lesser of
// thickness and that minimum.
return new RectangleD(proposedBounds.Location,
new SizeD(thickness, proposedBounds.Height));
}
else
{
// There is a minimum height for a shape; the
// height will actually be set to the lesser of
// thickness and that minimum.
return new RectangleD(proposedBounds.Location,
new SizeD(proposedBounds.Width, thickness));
} } }
請注意位置和大小可以被限制是否您想要的。