如何使用鼠标移动功能移动我的对象



我有一个类包含一个标签和一个LineShape:

public class jalon
{
    public Label lab = new Label();
    public LineShape line = new LineShape();
    public string date;
}

我动态创建了这个类的实例。

            int count;
            OleDbCommand cmd = new OleDbCommand();
            cnx.Open();
            cmd.Connection = cnx;
            string mois = cmbMat.SelectedValue.ToString();
            cmd.CommandText = "select count(*) from TableJalon Where Month(Date_Jalon)=" + mois;
            count = (int)(cmd.ExecuteScalar());
            OleDbDataReader reader = cmd.ExecuteReader();
            int i =count-1;
            int j = 300;
            jalon dr;
            for(int i =0; i<count;i++)
            {

                dr = new Jalon();
                dr.lab.Text = reader.GetValue(1).ToString();
                dr.lab.Location = new Point(j, j);
                dr.lab.MouseClick += new System.Windows.Forms.MouseEventHandler(this.labMouseClick);
                dr.lab.MouseDown += new System.Windows.Forms.MouseEventHandler(this.labMouseDown);
                dr.lab.MouseMove += new System.Windows.Forms.MouseEventHandler(this.LabMouseMove);
                dr.lab.DoubleClick += new System.EventHandler(this.ChangColor);
                this.Controls.Add(dr.lab);
                this.Controls.Add(dr.line);
            }

现在我想移动标签和我创建的 MouseMovelab 函数的相同线条形状,其中包含:

private void LabMouseMove(object sender, MouseEventArgs e)
        {
        Label jal = (Label)sender;
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            jal.Left = e.X + jal.Left - MouseDownLocation.X;
            jal.Top = e.Y + jal.Top - MouseDownLocation.Y;
            lineShape1.X1 = jal.Left;// i don't now what i use here
            lineShape1.Y1 = jal.Top + jal.Height;
        }
    }

所以我想同时移动我的标签和线条形状

您可以在全局变量中维护类 jalon 的列表。在鼠标移动时,识别相应的线条形状并移动它。

添加全局变量

 //global variable
 private List<jalon> jalons;

维护 Jalons 列表

        jalons = new List<jalon>();
        jalon dr;
        for(int i =0; i<count;i++)
        {

            dr = new Jalon();
            dr.lab.Text = reader.GetValue(1).ToString();
            dr.lab.Location = new Point(j, j);
            dr.lab.MouseClick += new System.Windows.Forms.MouseEventHandler(this.labMouseClick);
            dr.lab.MouseDown += new System.Windows.Forms.MouseEventHandler(this.labMouseDown);
            dr.lab.MouseMove += new System.Windows.Forms.MouseEventHandler(this.LabMouseMove);
            dr.lab.DoubleClick += new System.EventHandler(this.ChangColor);
            this.Controls.Add(dr.lab);
            this.Controls.Add(dr.line);
            jalons.Add(dr);
        }

最后,在鼠标移动中,从此列表中找到相应的线条形状。

private void LabMouseMove(object sender, MouseEventArgs e)
    {
    Label jal = (Label)sender;
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        jal.Left = e.X + jal.Left - MouseDownLocation.X;
        jal.Top = e.Y + jal.Top - MouseDownLocation.Y;
        LineShape lineShape1 = jalons.Where(j => j.lab == jal).FirstOrDefault().line;
        lineShape1.X1 = jal.Left;// i don't now what i use here
        lineShape1.Y1 = jal.Top + jal.Height;
    }
}

最新更新