C#中如何实现数据拖动?(拖动图片,到TextBox,并显示)
2012-08-24 18:58
661 查看
![](http://my.csdn.net/uploads/201208/24/1345805844_5849.jpg)
代码:
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://my.csdn.net/uploads/201208/24/1345806730_4451.jpg)
![](http://my.csdn.net/uploads/201208/24/1345806737_4561.jpg)
相关文章推荐
- [ASP.NET教程] C#中如何实现数据拖动?(拖动图片,到TextBox,并显示)
- C#SerialPort如何读取串口数据并显示在TextBox上
- C#如何实现图片的随机生成与显示(转载)
- C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
- C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
- 如何在C#中实现图片缩放
- Easyui Datagrid 如何实现后台交互显示用户数据列表
- C#如何实现图片的剪裁并保存
- C#中如何实现拖动PictureBox控件
- 一起学习水晶报表之拉模式【如何通过代码实现水晶报表显示数据】(课程2)
- Easyui Datagrid 如何实现后台交互显示用户数据列表
- 如何利用TTThumbsViewController实现显示不同数量的图片?
- C# 如何让多行TextBox实现换行
- 老大要求把Word文档加上公司的标识,公司名文字或者logo图片都可以,但要求是用电脑打开(电子文档时)能看见标识,如果打印出来(纸质文档)就不显示出来,如何实现?
- 引用数据库SQL Server 2008中数据时,如何实现C#.NET中的复选框?
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
- 将数据加载时显示的图片和文字提成公共的--实现方法
- Qt 中如何实现对图片设置透明色显示 .
- 如何通过图片实现窗体拖动
- 在ASP.NET中如何在DataGrid中实现数据嵌套显示