sifenwang
2017-03-01 06:13
采纳率: 94.7%
浏览 1.7k

c# form上放置几个lable,每个lable之间用画直线连接,如何实现鼠标拖拽联动?求解

RT~
创建几个lable,然后每个lable之间画线连接,如何实现拖拽lable的同时,直线也跟着联动~~~急急急求解,lable的拖拽移动问题我已经解决了,关键是怎么让线跟着联动~~~
lable拖拽代码如下是:

                 lable1.MouseDown += new MouseEventHandler(Switch_MouseDown);
                lable1.MouseMove += new MouseEventHandler(Switch_MouseMove);
         private Point m_lastPoint;
        private Point m_lastMPoint;
        private void lable1_MouseDown(object sender, MouseEventArgs e)
        {
            m_lastMPoint = Control.MousePosition;
            m_lastPoint = (sender as Label).Location;
        }
        private void lable1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ((Label)sender).Location = new Point(m_lastPoint.X + Control.MousePosition.X - m_lastMPoint.X, m_lastPoint.Y + Control.MousePosition.Y - m_lastMPoint.Y);
            }
        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 菜鸟的挣扎 2017-03-01 07:22
    已采纳

    提供个简单的方法:把这个控件全部画到一个panel里面,拖动panel,所有控件都会跟着联动了。。。
    也可以直接用代码控制panel位置,这样也还是联动的

    点赞 打赏 评论
  • sifenwang 2017-03-01 06:20

    窗体载入时,我在Paint添加下面的事件,这样窗体载入时,几个lable就连上线了,关键不知道怎么联动,求大神解答一二

             private void DrawLine(object sender, PaintEventArgs e)
            {
                Graphics g = this.CreateGraphics();
    
                foreach (Line one in GetAllLines())
                {
                    int x1 = GetSwitch(one.Linetop).Loc_x;
                    int y1 = GetSwitch(one.Linetop).Loc_y;
                    int x2 = GetSwitch(one.Lineend).Loc_x;
                    int y2 = GetSwitch(one.Lineend).Loc_y;
    
                    g.DrawLine(GlobalVariable.penGX, new Point(x1, y1), new Point(x2, y2));
                }
            }
    
    点赞 打赏 评论
  • sifenwang 2017-03-02 00:01

    不是太明白楼上的意思,求大牛给点思路和细节,跪谢

    点赞 打赏 评论

相关推荐 更多相似问题