1

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

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

经过数小时的挣扎和搜索,我找到了这个解决方案,所以我保存了位置并使用 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;
    }

}

绘图数据类

   [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;
        }
    }
}

在此处输入图像描述

已解决: 我在鼠标移动方法中缺少这些行:

    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);
4

1 回答 1

1
  1. 你应该把东西搬进去,Form1_MouseMove而不是Form1_MouseUp.
  2. 找到要移动的元素Arraylist并为其分配新位置。
  3. 重绘元素。

你也应该使用IList<DrawingData>而不是ArrayList

于 2013-08-27T20:40:25.060 回答