移动绘制的项目

时间:2013-08-27 19:00:04

标签: c# .net winforms

我正在创建动态文本字段并在Arraylist中存储位置和字体等细节。因此,例如,如果我在表单上单击3次,则生成3个随机数并在表单上的单击位置显示它。单击时我有一个选择器按钮,然后禁用添加更多文本功能。

现在在单击选择器按钮后,如果我在任何文本上单击(MouseDown事件),那么它应该沿着鼠标移动,直到MouseUp事件未被触发并放置在新的放置位置。

经过几个小时的挣扎和搜索后,我找到了This Solution,所以我保存了位置,并使用IsPointOnLine方法进行了检查,但仍然无法拖动。 谢谢你的帮助。

更新:设法让foreach到位,但仍然没有拖动所选元素。 Form1.Class

public partial class Form1 : Form
{

    private Point mouseDownPosition = new Point(0, 0);
    private Point mouseMovePosition = new Point(0, 0);
    private int mousePressdDown;
    IList drawnItemsList = new List<DrawingData>();
    private bool dragging;
    private bool isSelectorClicked; // if selector button is clicked
    DrawingData draggingText;

    Random rnd;
    int clicked = 0;

    public Form1()
    {
        InitializeComponent();
        this.rnd = new Random();
        this.isSelectorClicked = false;
        dragging = false;
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseMovePosition = e.Location;
        if (e.Button == MouseButtons.Left)
            mousePressdDown = 1;
        if (isSelectorClicked)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                if (a.IsPointOnLine(e.Location, 5))
                {
                    draggingText = a;
                    dragging = true;
                }

            }
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            mouseDownPosition = e.Location;

            if (dragging)
            {
                draggingText.cur = mouseDownPosition;
                this.Invalidate();

            }
        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        if (mousePressdDown == 1 && isSelectorClicked == false)
        {
            label1.Text = "X: " + mouseMovePosition.X.ToString();
            label2.Text = "Y: " + mouseMovePosition.Y.ToString();
            this.Invalidate();
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition, rnd.Next().ToString(), FontStyle.Bold, 30, "Candara", Brushes.Blue);
            drawnItemsList.Add(a);
            this.clicked++;
        }
        mousePressdDown = 0;
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        if(drawnItemsList.Count != 0)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a);

            }

            if (mousePressdDown != 0)
            {
                draw(e.Graphics, mouseDownPosition, mouseMovePosition, FontStyle.Bold, 30, "Candara", Brushes.Blue);
            }

        }


    }
    private void draw(Graphics e, Point mold, Point mcur, FontStyle fontWeight, int fontSize, string fontName, Brush fontColor)
    {
        Pen p = new Pen(Color.Black, 2);

        using (Font useFont = new Font(fontName, fontSize, fontWeight))
        {
            string header2 = rnd.Next().ToString();
            RectangleF header2Rect = new RectangleF();
            int moldX = mold.X - 5;
            int moldY = mold.Y;

            header2Rect.Location = new Point(moldX, moldY);
            header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
            e.DrawString(header2, useFont, fontColor, header2Rect);
        }
    }

    private void draw(Graphics e, DrawingData a)
    {
        Pen p = new Pen(Color.Black, 2);

        using (Font useFont = new Font(a.FontName, a.FontSize, a.FontWeight))
        {
            string header2 = rnd.Next().ToString();
            RectangleF header2Rect = new RectangleF();
            int moldX = a.old.X - 5;
            int moldY = a.old.Y;

            header2Rect.Location = new Point(moldX, moldY);
            header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
            e.DrawString(a.Rand, useFont, a.FontColor, header2Rect);
        }
    }

    private void Select_button_Click(object sender, EventArgs e)
    {
        this.isSelectorClicked = true;
    }

    private void WriteNewText_button_Click(object sender, EventArgs e)
    {
        this.isSelectorClicked = false;
    }

}

DrawingData.Class

   [Serializable]
public class DrawingData
{
    private Point Start; // mouseDown position
    private Point End; // mouseUp poslition
    private string randValue; // random data value
    private FontStyle fontWeight;
    private int fontSize;
    private string fontName;
    private Brush fontColor;

    public DrawingData()
    {
        Start = new Point(0, 0);
        End = new Point(0, 0);
        randValue = String.Empty;
        fontWeight = FontStyle.Bold;

    }

    public DrawingData(Point old, Point cur, string rand, FontStyle fs, int fSize, string fName, Brush fColor)
    {
        Start = old;
        End = cur;
        randValue = rand;
        fontWeight = fs;
        fontSize = fSize;
        fontName = fName;
        fontColor = fColor;
    }
    public float slope
    {
        get
        {
            return (((float)End.Y - (float)Start.Y) / ((float)End.X - (float)Start.X));
        }
    }
    public float YIntercept
    {
        get
        {
            return Start.Y - slope * Start.X;
        }
    }

    public bool IsPointOnLine(Point p, int cushion)
    {
        float temp = (slope * p.X + YIntercept);
        if (temp >= (p.Y - cushion) && temp <= (p.Y + cushion))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public FontStyle FontWeight
    {
        get
        {
            return fontWeight;
        }
        set
        {
            fontWeight = value;
        }
    }
    public int FontSize
    {
        get
        {
            return fontSize;
        }
        set
        {
            fontSize = value;
        }
    }
    public string FontName
    {
        get
        {
            return fontName;
        }
        set
        {
            fontName = value;
        }
    }
    public Brush FontColor
    {
        get
        {
            return fontColor;
        }
        set
        {
            fontColor = value;
        }
    }

    public Point old
    {
        get
        {
            return Start;
        }
        set
        {
            Start = value;
        }
    }

    public Point cur
    {
        get
        {
            return End;
        }
        set
        {
            End = value;
        }
    }

    public string Rand
    {
        get
        {
            return randValue;
        }
        set
        {
            randValue = value;
        }
    }
}

enter image description here

已解决:我在鼠标移动方法中遗漏了这些行:

    m.Start = new Point(deltaStart.X + e.Location.X, deltaStart.Y + e.Location.Y);
    m.End = new Point(deltaEnd.X + e.Location.X, deltaEnd.Y + e.Location.Y);

1 个答案:

答案 0 :(得分:1)

  1. 您应该移动Form1_MouseMove而不是Form1_MouseUp
  2. Arraylist中找到要移动的元素并为其指定新位置。
  3. 重绘元素。
  4. 您还应该使用IList<DrawingData>代替ArrayList