首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#拖动picturebox控件,该怎么处理

2013-07-08 
c#拖动picturebox控件如题,拖动的方法我已经写好了如下bool whetherSelected falsePoint pt new Poin

c#拖动picturebox控件
如题,拖动的方法我已经写好了如下


bool whetherSelected = false;
        Point pt = new Point();
        private void pbBigImage_MouseDown(object sender, MouseEventArgs e)
        {
            whetherSelected = true;
            pt = Cursor.Position;
            this.Cursor = System.Windows.Forms.Cursors.SizeAll;
        }

        private void pbBigImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (whetherSelected)
            {
                Point newPoint = new Point();
                if (pbBigImage.Width > panel2.Width) 
                {
                    newPoint.X = pbBigImage.Left + Cursor.Position.X - pt.X;
                }
                if (pbBigImage.Height > panel2.Height) 
                {
                    newPoint.Y = pbBigImage.Top + Cursor.Position.Y - pt.Y;
                }
                pbBigImage.Location = newPoint; 
                pt = Cursor.Position;
            }
        }

        private void pbBigImage_MouseUp(object sender, MouseEventArgs e)


        {
            whetherSelected = false;
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }



因为我的pic控件在panel里,我可以动态的修改pic控件的大小,当图片放大时比如pic控件的宽度超过了panel的宽度,才可以拖动pic控件,当我网左拓的时候如果pic的右边正好与panel的右边对齐的话不允许在向左拓了,上下左右类似求高手 能给段代码
[解决办法]
你在MouseMove的时候判断一下不就可以了?

[解决办法]
当我网左拓的时候如果pic的右边正好与panel的右边对齐的话不允许在向左拓了

这句话一直听不懂,看了好多遍。。。哎
[解决办法]
  private void pbBigImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (whetherSelected)
            {

            
                Point newPoint = new Point();
                if (pbBigImage.Width > panel2.Width)
                {
                    if (pbBigImage.Right < panel2.Width)
                    {
                        newPoint.X =  panel2.Width- pbBigImage.Width;
                    }
                    else if (pbBigImage.Left>0)
                    {


                        newPoint.X = 0;

                    }
                    else
                    newPoint.X = pbBigImage.Left + Cursor.Position.X - pt.X;
                }
                if (pbBigImage.Height > panel2.Height)
                {
                  
                     if (pbBigImage.Bottom<panel2.Height)
                    {  
                         newPoint.Y=panel2.Height-pbBigImage.Height;

                    }
                    else if (pbBigImage.Top > 0)
                     {
                         newPoint.Y = 0;

                     }
                    else
                    newPoint.Y = pbBigImage.Top + Cursor.Position.Y - pt.Y;
                }
                pbBigImage.Location = newPoint;
                pt = Cursor.Position;


            }
        }




搞定 给分吧!

热点排行