加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 文件拖拽和pixturBox缩放与拖拽功能

发布时间:2020-12-15 05:47:35 所属栏目:百科 来源:网络整理
导读:文件拖拽: 效果:将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了。 将一个控件的属性AllowDrop设置为true,然后添加DragDrop、DragEnter时间处理函数,如下: private void txtAppPath_DragEn

文件拖拽:

 效果:将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了。

 将一个控件的属性AllowDrop设置为true,然后添加DragDrop、DragEnter时间处理函数,如下:

private void txtAppPath_DragEnter(object sender,System.Windows.Forms.DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.FileDrop))
      {
        e.Effect = DragDropEffects.Link;
      }
      else
      {
        e.Effect = DragDropEffects.None;
      }
    }
    private void txtAppPath_DragDrop(object sender,System.Windows.Forms.DragEventArgs e)
    {
      txtLocalFileName.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
    }

图片的缩放和拖拽:

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

1、设置PixtureBox属性:

  Dock:none

  SizeMode:StretchImage

2、添加事件:

(1)设置绑定图片路径

private void ScrewInfoForm_Shown(object sender,EventArgs e)
    {
      //加载装配图纸
      string drawingPath = Path.Combine(@"192.168.2.136PCS",productCode + ".png");
      try
      {
        pbxDrawing.Load(drawingPath);
      }
      catch (Exception ex)
      {
        MessageBox.Show("加载装配图纸失败,详细:" + ex.Message,"测量",MessageBoxButtons.OK,MessageBoxIcon.Error);
        return;
      }
    }

(2)添加事件1

pbxDrawing.MouseWheel += new MouseEventHandler(pbxDrawing_MouseWheel);
//实现滚轮缩放
    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;
      }
    }

(3)添加事件2

//实现移动图片
    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缩放与拖拽,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读