3

基本上,我想在鼠标移动的地方画一条线,就像在油漆中一样,但是,当我在 mousePos 上画一个点时,会发生这种情况:

有时候是这样的

现在,我需要一些帮助把它变成一条线,没有间隙或任何奇怪的东西。感谢您的帮助。

我用来生成线条的代码(这不是图片中的代码!)

        protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        Line newLine = new Line(pixel, point1, point2, 2, Color.White);
        allLines.Add(newLine);

        foreach (Line lines in allLines) 
        {
            lines.Draw(spriteBatch);
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

和线对象:

    public class Line
{
    Texture2D texture;
    Vector2 point1, point2;
    float width;
    Color color;
    float angle, length;

    public Line(Texture2D texture, Vector2 point1, Vector2 point2, float width, Color color) 
    {
        this.texture = texture;
        this.point1 = point1;
        this.point2 = point2;
        this.width = width;
        this.color = color;
        angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
        length = Vector2.Distance(point1, point2);
    }

    public void Draw(SpriteBatch spriteBatch) 
    {
        spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, new Vector2(length, width), SpriteEffects.None, 0);
    }
}
4

3 回答 3

4

你想要做的是这样的(扩展pathfinder666的答案):

private Point _lastPoint;

protected void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    Graphics g = CreateGraphics();
    g.LineTo(_lastPoint.X, _lastPoint.Y, e.X, e.Y);
    _lastPoint = new Point(e.X, e.Y);
}

现在请记住,这可能看起来有点参差不齐。您可能想要使用DrawArc来稍微平滑它。这取决于您最终要完成的工作。

于 2012-10-22T18:39:47.293 回答
2

当我需要根据鼠标移动绘制线条/路径时,我过去所做的是使用 Path 对象而不是线条。

这是一个 XAML/C# 示例。只需在 C# 中启动一个新的 XAML 项目,然后将其粘贴到默认的“MainWindow”中:

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private PathFigure _pathFigure = new PathFigure();
    PathFigureCollection _pathCollection = new PathFigureCollection();
    PathSegmentCollection _segments = new PathSegmentCollection();
    private PathGeometry _pathGeometry = new PathGeometry();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _pathFigure.Segments = _segments;
        _pathCollection.Add(_pathFigure);
        _pathGeometry.Figures = _pathCollection;
        myPath.Data = _pathGeometry;
    }

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            LineSegment segment = new LineSegment();
            segment.Point = e.GetPosition(this);
            _pathFigure.Segments.Add(segment);

        }
    }

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _pathFigure.StartPoint = e.GetPosition(this);
    }
}

MainWindow.xaml:

<Window x:Class="TestPaths.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" MouseMove="Window_MouseMove" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
        <Path Stroke="Black" StrokeThickness="1" Name="myPath" />
    </Grid>
</Window>

输出

PathGeometry 示例中的 MainWindow

此示例不会为您创建任何新路径,因此当您再次按下鼠标左键(到当前位置)时,行会跳转。添加功能以根据需要创建新路径将非常简单。

此示例确实消除了对新点进行滚动跟踪的需要,因为您的最后一个点始终在现有对象中可用。有关路径的更多信息,包括如何设置曲线等,请参阅MSDN 上的 PathGeometry Docs

于 2012-10-22T19:20:46.397 回答
1

您如何将最后一点保留在内存中并从最后一点画线到当前点。您可能必须平滑曲线以使其平滑且具有视觉吸引力。

于 2012-10-22T18:33:17.207 回答