码迷,mamicode.com
首页 > Windows程序 > 详细

C#之PixturBox控件实现缩放和拖动图片

时间:2017-05-13 11:14:08      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:error   void   image   one   height   mouse   rgs   window   nbsp   

一、实现鼠标滚轮控制图片缩放;

1、设置PixtureBox属性:

  Dock:none

  SizeMode:StretchImage

2、添加事件:

pbxDrawing.MouseWheel += new MouseEventHandler(pbxDrawing_MouseWheel);

 

private void ScrewInfoForm_Shown(object sender, EventArgs e)
        {
            //加载装配图纸
            string drawingPath = Path.Combine(@"\\192.168.2.136\PCS", productCode + ".png");
            try
            {
                pbxDrawing.Load(drawingPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载装配图纸失败,详细:" + ex.Message, "测量", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
 //实现滚轮缩放
        private void pbxDrawing_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Delta < 0)
            {
                this.pbxDrawing.Width = this.pbxDrawing.Width * 9 / 10;
                this.pbxDrawing.Height = this.pbxDrawing.Height * 9 / 10;
            }
            else
            {
                this.pbxDrawing.Width = this.pbxDrawing.Width * 11 / 10;
                this.pbxDrawing.Height = this.pbxDrawing.Height * 11 / 10;
            }
        }
 //实现移动图片
        int xPos;
        int yPos;
        bool MoveFlag;
        private void pbxDrawing_MouseDown(object sender, MouseEventArgs e)
        {
            this.pbxDrawing.Focus();
            MoveFlag = true;//已经按下.
            xPos = e.X;//当前x坐标.
            yPos = e.Y;//当前y坐标.
        }

        //在picturebox的鼠标按下事件里.
        private void pbxDrawing_MouseUp(object sender, MouseEventArgs e)
        {
            MoveFlag = false;
        }

        //在picturebox鼠标移动
        private void pbxDrawing_MouseMove(object sender, MouseEventArgs e)
        {
            if (MoveFlag)
            {
                pbxDrawing.Left += Convert.ToInt16(e.X - xPos);//设置x坐标.
                pbxDrawing.Top += Convert.ToInt16(e.Y - yPos);//设置y坐标.
            }
        }

 

C#之PixturBox控件实现缩放和拖动图片

标签:error   void   image   one   height   mouse   rgs   window   nbsp   

原文地址:http://www.cnblogs.com/lfxiao/p/6848234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!