BoundsRules 约束形状位置和大小
区域规则 是定义一个形状的大小和位置限制的类。 它提供重复调用的方法,当用户拖动形状的角上时或端。
下面的示例约束矩形是栏固定大小,水平或垂直。 当用户拖动角或端时,轮廓翻转在高度和宽度的两种允许的配置之间。
区域规则是从 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));
} } }
通知位置和大小可以约束,如果您希望。