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

C#中如何实现数据拖动?(拖动图片,到TextBox,并显示)

代码:

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO;

namespace TestKeyPress {     public partial class Form2 : Form     {         public Form2()         {             InitializeComponent();         }

            private void textBox1_DragEnter(object sender, DragEventArgs e)         {             e.Effect = DragDropEffects.Copy;         }

        private void textBox1_DragDrop(object sender, DragEventArgs e)         {          //DataFormats.FileDrop确定你拖动过去的是文件回或者文件夹             if (e.Data.GetDataPresent(DataFormats.FileDrop))             {                 string realpath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();                 //从拖动数据里得到路径                 //MessageBox.Show(realpath);测试路径                 string p = Path.GetExtension(realpath);                 //MessageBox.Show(p);测试路径                 if (p == ".jpg")                 {                     //StreamReader sr = new StreamReader(realpath);读取方式,放弃使用                     Image im = Image.FromFile(realpath);                     int h = im.Height;                     int w = im.Width;                     this.pictureBox1.Height = h;                     this.pictureBox1.Width = w;                     this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;                     this.pictureBox1.BackgroundImage = im;                     //将读取到的图片放到picturebox里                     //this.pictureBox1.Location = new Point(130 - pictureBox1.Width, 85 - pictureBox1.Height);                     Point pnl = new Point(this.p