.Net通过拖放打开文件

1.实现怎样通过拖放打开一张影像?

PictureBox 也有DragEnter事件 DragDrop事件 但是怎么样都不触发.没有AllowDrag属性。

Form有Allow属性。

只要知道鼠标现在落在PictureBox上,事件触发就行了。

 private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            int x = this.PointToClient(new Point(e.X, e.Y)).X;

            int y = this.PointToClient(new Point(e.X, e.Y)).Y;

            if (x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                pictureBox1.Image = Image.FromFile(files[0]);

            }


        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;


        }

从ListView中拖拽到PictureBox

int x = this.PointToClient(new Point(e.X, e.Y)).X;//获取当前鼠标的坐标X
            int y = this.PointToClient(new Point(e.X, e.Y)).Y;//获取鼠标当前的坐标Y
            if (x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
            {
                ListViewItem LVI = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
                string StrPath = LVI.Text;
                pictureBox1.Image = Image.FromFile(StrPath);
            }

//拖动时显示的效果

 private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            DoDragDrop(e.Item, DragDropEffects.Move);//开始拖放操作
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值