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.panel1.Width / 2, this.panel1.Height / 2); Point pic = new Point(); pic.X = pnl.X - pictureBox1.Width / 2; pic.Y = pnl.Y - pictureBox1.Height / 2; this.pictureBox1.Location = pic; //上面的代碼是將圖片的中心和panel的中心對齊 //this.pictureBox1.Location = new Point(85 ,76); } } else { MessageBox.Show("please select jpg"); } //MessageBox.Show(e.Data.GetType().ToString()); } //放大功能 private void button1_Click(object sender, EventArgs e) { Image im = this.pictureBox1.BackgroundImage; //im.M pictureBox1.Height = (int)(pictureBox1.Height * 1.2); pictureBox1.Width = (int)(pictureBox1.Width * 1.2); Point pnl = new Point(this.panel1.Width / 2, this.panel1.Height / 2); Point pic = new Point(); pic.X = pnl.X - pictureBox1.Width / 2; pic.Y = pnl.Y - pictureBox1.Height / 2; this.pictureBox1.Location = pic; } //縮小功能 private void button2_Click(object sender, EventArgs e) { Image im = this.pictureBox1.BackgroundImage; //im.M pictureBox1.Height = (int)(pictureBox1.Height * 0.85); pictureBox1.Width = (int)(pictureBox1.Width * 0.85); Point pnl = new Point(this.panel1.Width / 2, this.panel1.Height / 2); Point pic = new Point(); pic.X = pnl.X - pictureBox1.Width / 2; pic.Y = pnl.Y - pictureBox1.Height / 2; this.pictureBox1.Location = pic; } private Point start; private Point end; //下面的函數實現,圖片在panel里可以拖動 private void panel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { start = new Point(e.X, e.Y); end = start; } } private void panel1_MouseUp(object sender, MouseEventArgs e) { end = new Point(e.X, e.Y); int picx = this.pictureBox1.Location.X; int picy = this.pictureBox1.Location.Y; picx += end.X - start.X; picy += end.Y - start.Y; this.pictureBox1.Location = new Point(picx, picy); } } }
數據拖動,主要是在接受數據的地方添加DragEnter和DragDrop事件,必須將接受數據的地方設置為AllowDrop=true,如果在一個窗體內拖動,選擇數據的地方也要設置AllowDrop=true,
轉自:http://blog.csdn.net/luolunz/article/details/7905193