C# InkCanvas绘制自定义形状(矩形)
我像画画一样创建程序。现在,有一个用户正在尝试创建标准形状。但我在画矩形时遇到了问题。我使用方法C# InkCanvas绘制自定义形状(矩形),c#,wpf,inkcanvas,C#,Wpf,Inkcanvas,我像画画一样创建程序。现在,有一个用户正在尝试创建标准形状。但我在画矩形时遇到了问题。我使用方法DrawCore在InkCanvas上创建自定义形状 代码: protected override void DrawCore(DrawingContext DrawingContext,DrawingAttributes DrawingAttributes) { if(drawingContext==null) { 抛出新ArgumentNullException(“drawingContext”)
DrawCore
在InkCanvas上创建自定义形状
代码:
protected override void DrawCore(DrawingContext DrawingContext,DrawingAttributes DrawingAttributes)
{
if(drawingContext==null)
{
抛出新ArgumentNullException(“drawingContext”);
}
if(null==DrawingAttribute)
{
抛出新ArgumentNullException(“DrawingAttribute”);
}
DrawingAttributes originalDa=DrawingAttributes.Clone();
SolidColorBrush笔刷=新的SolidColorBrush(颜色);
System.Windows.Media.Pen Pen=新的System.Windows.Media.Pen(新的SolidColorBrush(颜色),1);
刷子。冻结();
drawingContext.DrawRectangle(null,pen,new Rect(GetTheLeftTopPoint(),GetTheRightBottomPoint());
}
}
System.Windows.Point gettheleftoppoint()
{
if(this.StylusPoints==null)
抛出新的ArgumentNullException(“StylusPoints”);
StylusPoint tmpPoint=新StylusPoint(double.MaxValue,double.MaxValue);
foreach(此.StylusPoints中的StylusPoint点)
{
if((点XtmpPoint.X)| |(点Y>tmpPoint.Y))
tmpPoint=点;
}
返回tmpPoint.ToPoint();
}
但我的问题是,我不能画所有的方向。即:
它是如何修复的
谢谢。您需要分别查看x和y值。如果某个点的x或y值优于当前点,则不要选择整个点;如果某个点的x或y值优于当前点,则仅选择其x值;如果某个点的y值优于当前点,则仅选择其y值。@PieterWitvoet,谢谢!我只是用这个
drawingContext.DrawRectangle(画笔、画笔、新矩形(新点(sp.X、sp.Y)、新点(stp.X、stp.Y))来替换方法代码>现在所有工作都正常了。
protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
{
if (drawingContext == null)
{
throw new ArgumentNullException("drawingContext");
}
if (null == drawingAttributes)
{
throw new ArgumentNullException("drawingAttributes");
}
DrawingAttributes originalDa = drawingAttributes.Clone();
SolidColorBrush brush = new SolidColorBrush(color);
System.Windows.Media.Pen pen = new System.Windows.Media.Pen(new SolidColorBrush(color), 1);
brush.Freeze();
drawingContext.DrawRectangle(null, pen, new Rect(GetTheLeftTopPoint(), GetTheRightBottomPoint()));
}
}
System.Windows.Point GetTheLeftTopPoint()
{
if (this.StylusPoints == null)
throw new ArgumentNullException("StylusPoints");
StylusPoint tmpPoint = new StylusPoint(double.MaxValue, double.MaxValue);
foreach (StylusPoint point in this.StylusPoints)
{
if ((point.X < tmpPoint.X) || (point.Y < tmpPoint.Y))
tmpPoint = point;
}
return tmpPoint.ToPoint();
}
System.Windows.Point GetTheRightBottomPoint()
{
if (this.StylusPoints == null)
throw new ArgumentNullException("StylusPoints");
StylusPoint tmpPoint = new StylusPoint(0, 0);
foreach (StylusPoint point in this.StylusPoints)
{
if ((point.X > tmpPoint.X) || (point.Y > tmpPoint.Y))
tmpPoint = point;
}
return tmpPoint.ToPoint();
}