heisetoufa 发表于 2013-1-27 05:13:15

c# winform listView鼠标拖拽到pictureBox问题的解决办法,鼠标拖动

题描述:我现在想拖动listView里的数据,摄像机1到一个pictureBox里
我一共有16个pictureBox,拖到哪个pictureBox里就在哪个里边进行判断
判断拖动的是哪个摄象机,然后再在pictureBox判断需要显示出哪个视频

一直有个问题在困扰着我,因为pictureBox没有AllowDrop属性,需要自己重写这个属性,但是我重写了,可这个属性一直在我的项目里不生效,汗````今天终于搞定,现把从listView鼠标拖拽到pictureBox中显示为可以接收数据的方法记在这里,不包括接收后的显示
首先窗体名字为Form1
拖个listView和个pictureBox到窗体,她们分别叫listView1和pictureBox1
然后双击窗体,在加载窗体时重写pictureBox的AllowDrop方法
private void Form1_Load(object sender, EventArgs e)      {          //在Load的时候把picturebox的AllowDrop属性设置为true。         // ps:pictureBox的属性只能通过写代码设置,并且还没有智能感知            this.pictureBox1.AllowDrop = true;      } 然后找到listView1的事件中的ItemDrag事件,在事件名里填上listView1_ItemDrag这个名称
再在代码里写上
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)    {          //在listView的ItemDrag里把摄像机名字传送      DoDragDrop(e.Item.ToString(), DragDropEffects.Copy);    } 然后找到pictureBox的DragDrop事件
写上相应的代码
 
private void pictureBox1_DragDrop(object sender, DragEventArgs e)    {//处理dragdrop事件      string path = string.Empty ;      if (e.Data.Equals(typeof(string)))            path = e.Data.GetData(typeof(string)).ToString();      if (path.Length > 0)            pictureBox1.Load(path);    } 

 
最后再找到pictureBox的DragEnter事件
写上代码
 

private void pictureBox1_DragEnter(object sender, DragEventArgs e)    {//判断是不是可以接收的数据类型      if (e.Data.GetDataPresent(typeof(string)))            e.Effect = DragDropEffects.Copy;      else            e.Effect = DragDropEffects.None;    } 

 
到此可以实现鼠标从listView拖拽数据到pictureBox里鼠标显示为可以放下状态了,但不是把数据显示在pictureBox里
 
黑色头发  http://heisetoufa.iteye.com
页: [1]
查看完整版本: c# winform listView鼠标拖拽到pictureBox问题的解决办法,鼠标拖动