日期:2014-05-18  浏览次数:20762 次

如何实现从TreeView控件拖拽数据到PictureBox中显示?
大家好,我现在想实现从TreeView控件拖拽数据到PictureBox中显示出来?

比如我TreeView上是各个图像的名称,我现在想把名称拖拽到PictureBox,然后根据名字来设置它的Image。请问高手如何实现。

------解决方案--------------------
一定要拖拽吗?点击不行吗
------解决方案--------------------
友情帮顶
------解决方案--------------------
探讨
一定要拖拽吗?点击不行吗

------解决方案--------------------
关注中
------解决方案--------------------
用API可以实现
------解决方案--------------------
C# code

void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListViewItem item = listView1.GetItemAt(e.X,e.Y);
                item.Selected = true;
            }
        }

        Point pos = new Point();
        void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pos.X = e.X;
            pos.Y = e.Y;
        }
       
        void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] str = e.Data.GetFormats();
            if (str.Length == 0)
                return;
            Label lab = e.Data.GetData(str[0]) as Label;
            lab.BackColor = Color.Gray;
            pictureBox1.Controls.Add(lab);
            lab.Location = pos;
        }

        void pictureBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Label)))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;

        }

------解决方案--------------------
原则上说用点击就可以实现了

如果要用拖的话就得用API去捕捉了,没必要费这功夫啊
------解决方案--------------------
7楼的已经说得很清楚了
------解决方案--------------------
收藏