1

私はこれをしなければなりません:キャンバスから2つの画像の間に矢印を描くため、矢印が付いたボタンをクリックすると、1つの画像をクリックして矢印を貼り付け、矢印を描く限り矢印を描きます2番目の画像に貼り付ける必要があります。

4

3 に答える 3

1

2点で円弧を描くには、事前定義された角度が必要です。私はあなたがその部分を理解していると思います。

これを行うには、各画像に1つずつ、合計2つの円弧を描く必要があります。円弧は各画像よりも大きくなりますが、最初の画像では、円弧が画像を出て2番目の点に向かっているところにクリップするだけです。

2番目の画像では、2つの画像の原点間のxとyの距離だけ円弧をオフセットする必要があります。次に、2番目の画像の最初の点から2番目の点まで円弧を描き、画像の外側にある部分をクリップします。

ラバーバンドアークが必要な場合は、マウスを動かすたびにそれを消去して再描画する必要があります。画像間のフォーム上のスペースに描画したい場合は、適切なオフセットを使用してそれを行うことができます。

于 2010-12-06T01:20:27.233 に答える
1

点が 2 つあるので、線を引くことができます。これを試して:

public class Shape
{
    public float X { get; set; }
    public float Y { get; set; }
    public Image Image { get; set; }
}

public class Line
{
    public Shape A { get; set; }
    public Shape B { get; set; }
}

そしてコード:

private string _currentTool;
private readonly List<Shape> _shapes;
private readonly List<Line> _lines;
private Line _currentLine;

private void Button1Click(object sender, EventArgs e)
{
    _currentTool = "img";
}

private void Button2Click(object sender, EventArgs e)
{
    _currentTool = "line";
}

private void PictureBox1MouseDown(object sender, MouseEventArgs e)
{
    switch (_currentTool)
    {
        case "img":
            _shapes.Add(new Shape { Image = button1.Image, X = e.X, Y = e.Y });
            pictureBox1.Invalidate();
            break;
        case "line":
                var selectedShapes = _shapes.Where(shape => (shape.X - 10 < e.X) && (e.X < shape.X + 10) &&
                                                           (shape.Y - 10 < e.Y) && (e.Y < shape.Y + 10));
                if (selectedShapes.Count() > 0)
                {
                    var selectedShape = selectedShapes.First();
                    _currentLine = new Line {A = selectedShape};
                    pictureBox1.Invalidate();
                }
            break;
    }
}

private void PictureBox1MouseUp(object sender, MouseEventArgs e)
{
    switch (_currentTool)
    {
        case "line":
                var selectedShapes = _shapes.Where(shape => (shape.X - 10 < e.X) && (e.X < shape.X + 10) &&
                                                           (shape.Y - 10 < e.Y) && (e.Y < shape.Y + 10));
                if (selectedShapes.Count() > 0)
                {
                    var selectedShape = selectedShapes.First();
                    _currentLine.B = selectedShape;
                    _lines.Add(_currentTool);
                    pictureBox1.Invalidate();
                }
            break;
    }
}

private void PictureBox1Paint(object sender, PaintEventArgs e)
{
    foreach (var shape in _shapes)
    {
        e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
    }
    foreach (var line in _lines)
    {
        e.Graphics.DrawLine(new Pen(Color.Black), line.A.X, line.A.Y, line.B.X, line.B.Y);
    }
}
于 2010-12-06T04:27:35.887 に答える
0
public class Shape
{
public float X { get; set; }
public float Y { get; set; }
public Image Image { get; set; }

public bool Test_int(int x, int y)
    {
        if (((x <= this.x + (float)image.Width) && (x >= this.x)) && ((y <= this.y + (float)image.Height) && (y >= this.y)))
            return true;
        else
            return false;
    }
}



public class Line
{
    public Shape A { get; set; }
    public Shape B { get; set; }
}

そしてコード

private string currentTool;
private readonly List<Shape> shapes;
private readonly List<Line> lines;
private Line currentLine;

private void Button1Click(object sender, EventArgs e)
{
    currentTool = "img";
}

private void Button2Click(object sender, EventArgs e)
{
    currentTool = "line";
}


private void PictureBox1MouseDown(object sender, MouseEventArgs e)
{

    switch (currentTool)
    {
        case "img":
            shapes.Add(new Shape { Image = button1.Image, X = e.X, Y = e.Y });
            pictureBox1.Invalidate();
            break;
        case "line":
            foreach (Shape shape1 in shapes)
                {

                    if (shape1.Test_int(e.X, e.Y))
                    {

                        currentLine = new Line { A = shape1 };

                    }

                }
                drawArea1.Invalidate();
        break;
    }
}

private void PictureBox1MouseUp(object sender, MouseEventArgs e)
{

    switch (currentTool)
    {
        case "line":
            foreach (Shape shape1 in shapes)
            {

                if (shape1.Test_int(e.X, e.Y))
                {


                    currentLine.B = shape1;

                    }

                }
                lines.Add(currentLine);
                drawArea1.Invalidate();

            break;
    }
}

private void PictureBox1Paint(object sender, PaintEventArgs e)
{

    foreach (var shape in shapes)
    {
        e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
    }
    foreach (var line in lines)
    {
        Pen p = new Pen(Color.Gray, 1);
        Pen p2 = new Pen(Color.Black, 5);

        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        p2.StartCap = System.Drawing.Drawing2D.LineCap.Round;
        p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

        float x1 = line.A.X+line.A.Image.Width ;
        float y1 = line.A.Y+line.A.Image.Height/2;
        float x2 = line.B.X;
        float y2 = line.B.Y+line.B.Image.Height/2;
        double angle = Math.Atan2(y2 - y1, x2 - x1);

        e.Graphics.DrawLine(p, x1, y1, x2, y2);
        e.Graphics.DrawLine(p2, x2, y2, x2 + (float)(Math.Cos(angle)), y2 + (float)(Math.Sin(angle)));

    }
}
于 2010-12-07T23:53:55.023 に答える