c# - 使用鼠标事件绘制折线

标签 c# wpf

我正在尝试使用 WPF 创建绘图应用程序。

我使用 Canvas 并在触发 MouseMove 事件的位置绘制折线。 但是在此过程中会创建一些工件:

4 处的 StrokeThickness:

With StrokeThickness at 4

15 处的 StrokeThickness:

With StrokeThickness at 15

红色点表示触发 MouseMove 的位置,灰色线当然是包含所有红色点的折线。

为什么我得到这个有什么想法吗?

最佳答案

如果 StrokeLineJoin=Miter 那么您可以使用 StrokeMiterLimit 来控制斜接延伸的距离。

(StrokeLineJoin=MitrePolyLine 的默认值)

或者,您可以使用 StrokeLineJoin=Round 在段之间实现良好的过渡。

如果您想要不同的末端,请使用 StrokeStartLineCapStrokeEndLineCap

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="500">
    <Window.Resources>
        <PointCollection x:Key="points">0,0 10,30 15,0 18,60 23,30 35,30 40,0 43,60 48,30 100,30</PointCollection>
    </Window.Resources>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
    <StackPanel Orientation="Horizontal">
        <Polyline Stroke="Gray" StrokeThickness="4" Points="{StaticResource points}" />
        <Polyline Stroke="Gray" StrokeThickness="4" StrokeMiterLimit="10" Points="{StaticResource points}" />
        <Polyline Stroke="Gray" StrokeThickness="4" StrokeMiterLimit="5"  Points="{StaticResource points}" />
        <Polyline Stroke="Gray" StrokeThickness="4" StrokeMiterLimit="1"  Points="{StaticResource points}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="0,50,0,0">
        <Polyline Stroke="Gray" StrokeThickness="10" Points="{StaticResource points}" />
        <Polyline Stroke="Gray" StrokeThickness="10" StrokeMiterLimit="10" Points="{StaticResource points}" />
        <Polyline Stroke="Gray" StrokeThickness="10" StrokeMiterLimit="5"  Points="{StaticResource points}" />
        <Polyline Stroke="Gray" StrokeThickness="10" StrokeMiterLimit="1"  Points="{StaticResource points}" />
    </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,50,0,0">
            <Polyline Stroke="Gray" StrokeThickness="10" StrokeLineJoin="Miter" StrokeStartLineCap="Round" StrokeEndLineCap="Round" Points="{StaticResource points}" />
            <Polyline Stroke="Gray" StrokeThickness="10" StrokeLineJoin="Bevel" StrokeStartLineCap="Round" StrokeEndLineCap="Round" Points="{StaticResource points}" />
            <Polyline Stroke="Gray" StrokeThickness="10" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round" Points="{StaticResource points}" />
        </StackPanel>
    </StackPanel>
</Window>

enter image description here

关于c# - 使用鼠标事件绘制折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129987/

相关文章:

c# - 先读取 IEnumerable 或等待其完成更改

c# - 压缩和utf8编码

c# - MarkupExtension.ProvideValue——是否实际使用了 IServiceProvider?

wpf - 如何对每个 WCF 调用进行身份验证和授权?

c# - 尝试理解 Thread.MemoryBarrier() 和上下文切换之间的关系

c# - 以编程方式获取子元素 selenium c#

c# - GroupPrincipal 重命名组

c++ - 为什么 WPF 不像 WinForms 那样支持 C++.NET?

c# - 删除导航服务上的前向条目?

c# - 我已经启动了一个流程,我该如何调用该流程中的方法