【发布时间】:2011-11-07 19:03:26
【问题描述】:
我有一个自定义的椭圆代码,如下所示。我使用椭圆绘制橡皮筋,使用两个点设置宽度和高度,代码如下所示。但是,当我绘制椭圆时,边界框正在切割侧面的边缘。我之前使用实际高度和宽度解决了这个问题,但这是在一个独立的应用程序中。当我将它与橡皮筋绘图部分集成时,实际高度和宽度不再起作用,由于某种原因,当我设置宽度和高度时它们没有得到更新。你知道我怎样才能修复它,这样边缘就不会被切断。
namespace WpfApplication4
{
class Ellipse2 : Shape
{
EllipseGeometry ellipse;
public static readonly DependencyProperty TextBoxShapeProperty = DependencyProperty.Register("TextBoxShape", typeof(TextBoxShape), typeof(Ellipse2), new FrameworkPropertyMetadata(null));
public TextBoxShape TextBoxShape
{
get { return (TextBoxShape)GetValue(TextBoxShapeProperty); }
set { SetValue(TextBoxShapeProperty, value); }
}
public Ellipse2()
{
ellipse = new EllipseGeometry();
this.Fill = Brushes.Transparent;
this.Stroke = Brushes.Gray;
this.StrokeThickness = 3;
}
protected override Geometry DefiningGeometry
{
get
{
TranslateTransform t = new TranslateTransform(Width / 2, Height / 2);
ellipse.Transform = t;
ellipse.RadiusX = this.Width / 2;
ellipse.RadiusY = this.Height / 2;
return ellipse;
}
}
}
}
double width = Math.Abs(initMousePoint.X - currMousePoint.X);
double height = Math.Abs(initMousePoint.Y - currMousePoint.Y);
double left = Math.Min(initMousePoint.X, currMousePoint.X);
double top = Math.Min(initMousePoint.Y, currMousePoint.Y);
rubberBandShape.Width = width;
rubberBandShape.Height = height;
Canvas.SetTop(rubberBandShape, top);
Canvas.SetLeft(rubberBandShape, left);
【问题讨论】:
-
请停止prepending tags in your question titles,没有必要,页面标题会自动添加最流行的标签。
-
Ellipse2的唯一问题是切边吗?我试过了,它似乎无法处理Margin之类的东西。无论如何,尝试补偿StrokeThickness,例如ellipse.RadiusX = (this.Width / 2) - StrokeThickness / 2; -
@Meleak 感谢它完美地工作,我不知道我是怎么想到的。如果您愿意,可以将其发布为答案,我会接受。顺便问一下,你知道为什么实际的高度和宽度没有更新吗?
-
@mihajlv:好的,添加了一个答案 :) 我也尝试了
ActualWidth和ActualHeight,它对我来说比Width和Height效果更好。不知道你为什么会遇到这个问题
标签: wpf height width ellipse bounding-box