| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 660 人关注过本帖
标题:C#拖动控件问题
只看楼主 加入收藏
宇智波曌
Rank: 2
等 级:论坛游民
帖 子:98
专家分:15
注 册:2011-4-17
结帖率:100%
收藏
 问题点数:0 回复次数:3 
C#拖动控件问题
C#怎么让一个button拖动后top,left不要出去panel1,只能在panel1里面。下面的代码怎么修改????
程序代码:
private bool isMove = false;
private int MouseDownX;
private int MouseDownY;


 private void lblCode_MouseMove(object sender, MouseEventArgs e)
  {
  if (isMove)//如果左键按下的话
  {
  ((Button)sender).Location = new Point(((Button)sender).Left + e.X - MouseDownX, ((Button)sender).Top + e.Y - MouseDownY);
  }


 private void lblCode_MouseDown(object sender, MouseEventArgs e)
  {
  isMove = true;
    
  MouseDownX = e.X;
  MouseDownY = e.Y;
    

 
  }
  private void lblCode_MouseUp(object sender, MouseEventArgs e)
  {
  isMove = false;
  }


[ 本帖最后由 宇智波曌 于 2012-5-12 15:49 编辑 ]
搜索更多相关主题的帖子: top button 
2012-05-12 15:45
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
收藏
得分:0 
回复 楼主 宇智波曌
在释放鼠标的时候判断一下坐标是否超出,如果超出,重新弹回去。

欢迎加入.Net技术交流群:90925122
2012-05-17 00:32
宇智波曌
Rank: 2
等 级:论坛游民
帖 子:98
专家分:15
注 册:2011-4-17
收藏
得分:0 
回复 2楼 zwffff
怎么判断?能加点代码吗?
2012-05-20 21:08
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
收藏
得分:0 
大致的写了一下
private Point oldPoint;
        private void btnMove_MouseDown(object sender, MouseEventArgs e)
        {
            oldPoint = e.Location;
            Console.WriteLine("startX={0},startY={1},btnX={2},btnY={3}", e.Location.X, e.Location.Y, btnMove.Location.X, btnMove.Location.Y);
        }

        private void btnMove_MouseUp(object sender, MouseEventArgs e)
        {
            btnMove.Cursor = Cursors.Default;
            if (e.Button == MouseButtons.Left)
            {
                Point newPosition = btnMove.Location;
                newPosition.Offset(e.Location.X - oldPoint.X, e.Location.Y - oldPoint.Y);
                if (new Rectangle(0, 0, panel1.Width - btnMove.Width, panel1.Height - btnMove.Height).Contains(newPosition))
                {
                    btnMove.Location = newPosition;
                }
                else
                {
                    //MessageBox.Show("Out range!");
                }
            }
        }

        private void btnMove_MouseMove(object sender, MouseEventArgs e)
        {
            btnMove.Cursor = Cursors.Arrow;
            if (e.Button == MouseButtons.Left)
            {
                Point newPosition = btnMove.Location;
                newPosition.Offset(e.Location.X - oldPoint.X, e.Location.Y - oldPoint.Y);
                if (new Rectangle(0, 0, panel1.Width - btnMove.Width, panel1.Height - btnMove.Height).Contains(newPosition))
                {
                    btnMove.Location = newPosition;
                }
                else
                {
                    //MessageBox.Show("Out range!");
                }
            }
        }

欢迎加入.Net技术交流群:90925122
2012-05-21 23:47
快速回复:C#拖动控件问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025658 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved