1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Input;
namespace WpfUserControlAdorner
{
public class CustAdornerB : Adorner
{
private Rectangle _presenter;
// To store and manage the adorner's visual children.
VisualCollection visualChildren;
//Filtre GeneralTransformGroup
private GeneralTransformGroup resultFilter;
//parametre TransformGroup de la passe courante LayoutTransform ou Disposition
//agit sur MeasureOverride =>DesiredSize et sur ArrangeOverride=>rect
private TransformGroup myTrfGroup;
// nos transforms courantes seront modifies
// pour agir sur passe courante LayoutTransform(disposition)
private ScaleTransform myScale;
private RotateTransform myRotation;
private SkewTransform mySkew;
public CustAdornerB(UIElement adornedElt,TransformGroup trfGroup)
: base(adornedElt)
{
myTrfGroup = trfGroup;
visualChildren = new VisualCollection(this);
AddVisual();
}
protected override Size MeasureOverride(Size constraint)
{
_presenter.Measure(constraint);
return constraint;
}
protected override Size ArrangeOverride(Size finalSize)
{
double desiredWidth = AdornedElement.DesiredSize.Width;
double desiredHeight = AdornedElement.DesiredSize.Height;
// adornerWidth & adornerHeight are used for placement as well.
double adornerWidth = this.DesiredSize.Width;
double adornerHeight = this.DesiredSize.Height;
//arrange _presenter
Rect rect = new Rect(-adornerWidth / 2, -adornerHeight / 2, adornerWidth, adornerHeight);
_presenter.Arrange(rect);
return finalSize;
}
// Override the VisualChildrenCount and GetVisualChild properties to interface with
// the adorner's visual collection.
protected override int VisualChildrenCount { get { return visualChildren.Count; } }
protected override Visual GetVisualChild(int index) { return visualChildren[index]; }
// Add the Visual to adorner's visual collection.
private void AddVisual()
{
_presenter = new Rectangle ();
_presenter.Stroke = Brushes.Black;
_presenter.Fill= Brushes.LightBlue;
_presenter.Width = 10;
_presenter.Height = 10;
_presenter.IsHitTestVisible = true;
visualChildren.Add(_presenter);
}
//Appele avant passe LayoutTransform(Disposition)
// par le layer..en sous-main
public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
{
// Recupere nos transforms courantes apres modifications
// renvoi tout ( en lieu et place des originales) à layer
ModifiedTransforms(myTrfGroup);
resultFilter.Children.Add(base.GetDesiredTransform(transform));
return resultFilter;
}
//Modifie nos transforms courantes
private void ModifiedTransforms(TransformGroup currentGroupe)
{
resultFilter = new GeneralTransformGroup();
for (int i = 0; i < currentGroupe.Children.Count; i++)
{
Transform trf = currentGroupe.Children[i];
if (trf.GetType() == typeof(ScaleTransform))
{
myScale = (ScaleTransform)currentGroupe.Children[i];
double x = myScale.ScaleX;
double y = myScale.ScaleY;
resultFilter.Children.Add(new ScaleTransform(1 / x, 1 / y));
}
else if (trf.GetType() == typeof(RotateTransform))
{
myRotation = (RotateTransform)currentGroupe.Children[i];
double theta = myRotation.Angle;
resultFilter.Children.Add(new RotateTransform(-theta, 0.5, 0.5));
}
else if (trf.GetType() == typeof(SkewTransform))
{
mySkew = (SkewTransform)currentGroupe.Children[i];
double thetaX = mySkew.AngleX;
double thetaY = mySkew.AngleY;
resultFilter.Children.Add(new SkewTransform(-thetaX, -thetaY));
}
}
}
}
} |
Partager