WPF中的Adorner是一个挺有用的东西,表示刚刚学的话还是需要看一会的=-= 先学习下最简单的基础应用先,然后再继续往下挖掘。
首先,还是了解下Adorner是撒,由于本人比较菜,还是找个地方看一下比较好。先弄个连接放在以后慢慢翻一翻。考资料: &&
然后么自己先弄一个简单的关于Adorner的东西,先把那个简单的比如把一个label的四个角都加一个圆圈。就是先弄一个label,然后用AdornerLayer.GetAdornerLayer()方法把label传进去,再用AdornerLayer.Add()把Adorner加进去。
View Code
private void Window_Loaded_1(object sender, RoutedEventArgs e) { AdornerLayer adorner = AdornerLayer.GetAdornerLayer(label); adorner.Add(new SimpleTestAdorner(label)); }
随后只要重写OnRander就可以了,在里面加上四个角:
View Code
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize); SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green); renderBrush.Opacity = 0.2; Pen renderPen = new Pen(new SolidColorBrush(Colors.Red), 1.5); double renderRadius = 5.0; drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius); Rect rect = new Rect(0, 0, adornedElement.ActualWidth, adornedElement.ActualHeight); Pen rectrenderPen = new Pen(new SolidColorBrush(Colors.Blue), 1.5); drawingContext.DrawRectangle(Brushes.Transparent, rectrenderPen, rect); base.OnRender(drawingContext); }
这样的话好像比较低端一点,好像都是在一层上面的,貌似还需要从外面加东西进去,如果先在里面画一个矩形,然后可以再用AddVisualChild来添加一些东西进去,如下:
View Code
class SimpleTestAdorner : Adorner { public SimpleTestAdorner(UIElement andornerElement) : base(andornerElement) { if (andornerElement is FrameworkElement) { adornedElement = andornerElement as FrameworkElement; CreateEllipse(); } } private void CreateEllipse() { Ellipse ellipse = new Ellipse(); ellipse.Stroke = Brushes.Red; ellipse.Fill = Brushes.Green; ellipse.Cursor = Cursors.SizeNWSE; AddVisualChild(ellipse); ellipseDrag = ellipse; } protected override int VisualChildrenCount { get { return 1; } } protected override Visual GetVisualChild(int index) { if (adornedElement != null) return ellipseDrag; return base.GetVisualChild(index); } protected override Size ArrangeOverride(Size finalSize) { Size size = base.ArrangeOverride(finalSize); if (VisualChildrenCount == 1) { ellipseDrag.Arrange(new Rect(finalSize.Width - 205, finalSize.Height - 105, 10, 10)); } return size; } protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { //Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize); //SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green); //renderBrush.Opacity = 0.2; //Pen renderPen = new Pen(new SolidColorBrush(Colors.Red), 1.5); //double renderRadius = 5.0; //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius); //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius); //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius); //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius); Rect rect = new Rect(0, 0, adornedElement.ActualWidth, adornedElement.ActualHeight); Pen rectrenderPen = new Pen(new SolidColorBrush(Colors.Blue), 1.5); drawingContext.DrawRectangle(Brushes.Transparent, rectrenderPen, rect); base.OnRender(drawingContext); } Ellipse ellipseDrag = null; FrameworkElement adornedElement = null; }
然后就可以通过这个圈来放大缩小,当然我还没写= = 容我再多学习一下~