Udostępnij za pośrednictwem


BoundsRules ograniczyć położenia kształtu i rozmiaru

A Reguły granice jest klasa, która definiuje ograniczenia na rozmiar i położenie kształtu.Zapewnia metodę, która jest wywoływany cyklicznie, podczas gdy użytkownik jest przeciągnięcie kształtu lub narożniki lub boków kształtu.

Poniższy przykład ogranicza prostokątny kształt za pasek o ustalonym rozmiarze, poziomej lub pionowej.Gdy użytkownik przeciągnie narożnikami lub boków, konspekt Przerzuca między dwie konfiguracje dozwolone z wysokość i szerokość.

Granice reguły jest Klasa pochodna od BoundsRules.W kształcie tworzone jest wystąpienie reguły:

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));
} } }

Należy zauważyć, że lokalizacja i rozmiar może być ograniczona, jeśli chcesz.

Zobacz też

Informacje

BoundsRules

Inne zasoby

Reagowanie na i propagowanie zmian