[ASP.NET教程] C#中如何实现数据拖动?(拖动图片,到TextBox,并显示)

来源:互联网 发布:要怎么加盟农村淘宝 编辑:程序博客网 时间:2024/05/22 06:25

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

代码:

  1. <p>using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;</p>
  9. <p>namespace TestKeyPress
  10. {
  11. public partial class Form2 : Form
  12. {
  13. public Form2()
  14. {http://www.kmnk03.com/hxpfk/bptx/349.html
  15. InitializeComponent();
  16. }</p>
  17. <p>
  18. private void textBox1_DragEnter(object sender, DragEventArgs e)
  19. {
  20. e.Effect = DragDropEffects.Copy;
  21. }</p>
  22. <p> private void textBox1_DragDrop(object sender, DragEventArgs e)
  23. {http://www.kmnk03.com/hxpfk/bptx/350.html
  24. //DataFormats.FileDrop确定你拖动过去的是文件回或者文件夹
  25. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  26. {
  27. string realpath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
  28. //从拖动数据里得到路径
  29. //MessageBox.Show(realpath);测试路径
  30. string p = Path.GetExtension(realpath);
  31. //MessageBox.Show(p);测试路径
  32. if (p == ".jpg")
  33. {http://www.kmnk03.com/hxpfk/bptx/351.html
  34. //StreamReader sr = new StreamReader(realpath);读取方式,放弃使用
  35. Image im = Image.FromFile(realpath);
  36. int h = im.Height;
  37. int w = im.Width;
  38. this.pictureBox1.Height = h;
  39. this.pictureBox1.Width = w;
  40. this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
  41. this.pictureBox1.BackgroundImage = im;
  42. //将读取到的图片放到picturebox里
  43. //this.pictureBox1.Location = new Point(130 - pictureBox1.Width, 85 - pictureBox1.Height);
  44. Point pnl = new Point(this.panel1.Width / 2, this.panel1.Height / 2);
  45. Point pic = new Point();http://www.kmnk03.com/hxpfk/bptx/352.html
  46. pic.X = pnl.X - pictureBox1.Width / 2;
  47. pic.Y = pnl.Y - pictureBox1.Height / 2;
  48. this.pictureBox1.Location = pic;
  49. //上面的代码是将图片的中心和panel的中心对齐
  50. //this.pictureBox1.Location = new Point(85 ,76);
  51. }
  52. }
  53. else
  54. {http://www.kmnk03.com/hxpfk/bptx/353.html
  55. MessageBox.Show("please select jpg");
  56. }
  57. //MessageBox.Show(e.Data.GetType().ToString());
  58. }
  59. //放大功能
  60. private void button1_Click(object sender, EventArgs e)
  61. {
  62. Image im = this.pictureBox1.BackgroundImage;
  63. //im.Mhttp://www.kmnk03.com/hxpfk/bptx/354.html
  64. pictureBox1.Height = (int)(pictureBox1.Height * 1.2);
  65. pictureBox1.Width = (int)(pictureBox1.Width * 1.2);
  66. Point pnl = new Point(this.panel1.Width / 2, this.panel1.Height / 2);
  67. Point pic = new Point();
  68. pic.X = pnl.X - pictureBox1.Width / 2;
  69. pic.Y = pnl.Y - pictureBox1.Height / 2;
  70. this.pictureBox1.Location = pic;
  71. }
  72. //缩小功能
  73. private void button2_Click(object sender, EventArgs e)
  74. {http://www.kmnk03.com/hxpfk/bptx/355.html
  75. Image im = this.pictureBox1.BackgroundImage;
  76. //im.M
  77. pictureBox1.Height = (int)(pictureBox1.Height * 0.85);
  78. pictureBox1.Width = (int)(pictureBox1.Width * 0.85);
  79. Point pnl = new Point(this.panel1.Width / 2, this.panel1.Height / 2);
  80. Point pic = new Point();
  81. pic.X = pnl.X - pictureBox1.Width / 2;
  82. pic.Y = pnl.Y - pictureBox1.Height / 2;
  83. this.pictureBox1.Location = pic;
  84. }
  85. private Point start;
  86. private Point end;http://www.kmnk03.com/hxpfk/bptx/356.html
  87. //下面的函数实现,图片在panel里可以拖动
  88. private void panel1_MouseDown(object sender, MouseEventArgs e)
  89. {
  90. if (e.Button == MouseButtons.Left)
  91. {
  92. start = new Point(e.X, e.Y);
  93. end = start;
  94. }
  95. }
  96. private void panel1_MouseUp(object sender, MouseEventArgs e)
  97. {http://www.kmnk03.com/hxpfk/bptx/358.html
  98. end = new Point(e.X, e.Y);
  99. int picx = this.pictureBox1.Location.X;
  100. int picy = this.pictureBox1.Location.Y;
  101. picx += end.X - start.X;
  102. picy += end.Y - start.Y;
  103. this.pictureBox1.Location = new Point(picx, picy);
  104. http://www.kmnk03.com/hxpfk/bptx/362.html
  105. }
  106. }
  107. }</p>
  108. kmnk03.com
  109. www.kmnk03.com

数据拖动,主要是在接受数据的地方添加DragEnter和DragDrop事件,必须将接受数据的地方设置为AllowDrop=true,如果在一个窗体内拖动,选择数据的地方也要设置AllowDrop=true,

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

0 0