在 WPF 中绘制动态多边形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20465322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:10:44  来源:igfitidea点击:

Drawing dynamic polygons in WPF

c#wpfdrawingpolygonshapes

提问by David von Tamar

I am not sure if this is the right place to ask for such concept information advice, so I apologise if it's unrelated or off-topic to ask in Stack Overflow.

我不确定这是否是寻求此类概念信息建议的正确地方,因此如果在 Stack Overflow 中询问与此无关或偏离主题,我深表歉意。

I want to develop an application in WPF which has the ability to draw polygons with the functionality of a regular control, they may change shape by adding, removing or moving vertices, change brushes all by run-time, from data-binding or perhaps direct manipulating from C# code (still not sure about that).

我想在 WPF 中开发一个应用程序,它能够使用常规控件的功能绘制多边形,它们可以通过添加、删除或移动顶点来改变形状,通过运行时、数据绑定或直接更改画笔从 C# 代码操作(仍然不确定)。

What I am trying to achieve is an application which draws a map and the shapes on it are the entities with the dynamic borders over the map (for instance say political borders). The polygons also have to be clickable controls with collision test (not just a bounding box, but exactly by the shape of the entity on the map). I can expect the shapes to be very detailed because of borders which found by rivers and mountains or other natural objects which not just a straight line of two vertices, so it's performance should be an important factor here because one polygon may contain hundreds of vertices).

我想要实现的是一个绘制地图的应用程序,其上的形状是地图上具有动态边界的实体(例如边界)。多边形也必须是带有碰撞测试的可点击控件(不仅仅是一个边界框,而是完全按照地图上实体的形状)。我可以期望形状非常详细,因为河流和山脉或其他自然物体发现的边界不仅仅是两个顶点的直线,所以它的性能在这里应该是一个重要因素,因为一个多边形可能包含数百个顶点) .

What I've concluded that it is possible to achieve via WPF such an application. But my uncertainty is on the most efficient way to implement the map drawing, perhaps I should implement D3D hosting like SharpDX but I don't want it, it would make things even more complicated and difficult.

我得出的结论是,可以通过 WPF 这样的应用程序来实现。但是我不确定实现地图绘制的最有效方法,也许我应该像SharpDX一样实现D3D托管但我不想要它,它会使事情变得更加复杂和困难。

I prefer everything in this map to be functional as a regular WPF control with it's data-binding and stylising abilities. I've developed with WPF some several small test projects for months to learn the basics and its main concept. But now comes the main interest of mine to develop with WPF. I need some advice please, because drawing complicated and dynamic shapes is still not really clear to me to just go on and start develop it.

我更喜欢这张地图中的所有内容都可以作为常规 WPF 控件使用,并具有数据绑定和样式化功能。我用 WPF 开发了几个小测试项目几个月来学习基础知识和它的主要概念。但是现在我的主要兴趣是使用 WPF 进行开发。我需要一些建议,因为绘制复杂和动态的形状对我来说仍然不是很清楚,可以继续并开始开发它。

采纳答案by markmnl

I would use WPF, indeed I would say WPF is perfect for this, though there will be considerable amount to learn. WPF uses DirectX so is preformant enough I imagine (provided you have the hardware).

我会使用 WPF,确实我会说 WPF 非常适合这个,尽管会有相当多的东西需要学习。WPF 使用 DirectX,所以我想已经足够了(前提是你有硬件)。

You will need to become familiar with:

您将需要熟悉:

However if you are not already familiar with Dependency Properties, they can be a headache to learn, so rather than creating your own UserControlwith them, you can get away with a Canvasin your Windowand build things programmatically or at design time in XAML.

然而,如果你不熟悉依赖属性,他们可以是一个头痛的学习,这样,而不是创建自己的UserControl他们,你可以逃脱一个Canvas在你Window和构建东西编程方式或在XAML设计时间。

As for actually drawing shapes; if you know ahead of time what the shapes will look like you can draw them ahead of time using tool like Blend (or by yourself in XAML - you will need to become familiar with the Path Markup Syntax) then you can use transforms such as a ScaleTransformto transform them at run-time, or if you want to build them at run-time you can do so programmatically adding points to a Polygon

至于实际绘制形状;如果您提前知道形状的外观,您可以使用 Blend 之类的工具提前绘制它们(或者您自己在 XAML 中 - 您需要熟悉路径标记语法),那么您可以使用变换,例如ScaleTransform在运行时转换它们,或者如果您想在运行时构建它们,您可以通过编程方式将点添加到Polygon

e.g. from (Polygon)

例如来自(多边形

//Add the Polygon Element
myPolygon = new Polygon();
myPolygon.Stroke = System.Windows.Media.Brushes.Black;
myPolygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
myPolygon.StrokeThickness = 2;
myPolygon.HorizontalAlignment = HorizontalAlignment.Left;
myPolygon.VerticalAlignment = VerticalAlignment.Center;
System.Windows.Point Point1 = new System.Windows.Point(1, 50);
System.Windows.Point Point2 = new System.Windows.Point(10,80);
System.Windows.Point Point3 = new System.Windows.Point(50,50);
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(Point1);
myPointCollection.Add(Point2);
myPointCollection.Add(Point3);
myPolygon.Points = myPointCollection;
myGrid.Children.Add(myPolygon);