原文: http://zetcode.com/gui/csharpwinforms/dragdrop/

Mono Winforms 教程的这一部分将专门用于拖放操作。

在计算机图形用户界面中,拖放是单击虚拟对象并将其拖动到其他位置或另一个虚拟对象上的动作(或支持以下动作)。 通常,它可用于调用多种动作,或在两个抽象对象之间创建各种类型的关联。 (维基百科)

拖放功能是图形用户界面最明显的方面之一。 拖放操作使您可以直观地完成复杂的事情。

拖动按钮

在第一个示例中,我们将在按钮控件上执行拖放操作。 该示例在拖放&放置协议之外执行作业。

dragbutton.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. public class MForm : Form
  5. {
  6. private bool isDragging = false;
  7. private int oldX, oldY;
  8. private Button button;
  9. public MForm()
  10. {
  11. Text = "Drag & drop button";
  12. Size = new Size(270, 180);
  13. button = new Button();
  14. button.Parent = this;
  15. button.Cursor = Cursors.Hand;
  16. button.Text = "Button";
  17. button.Location = new Point(20, 20);
  18. button.MouseDown += new MouseEventHandler(OnMouseDown);
  19. button.MouseUp += new MouseEventHandler(OnMouseUp);
  20. button.MouseMove += new MouseEventHandler(OnMouseMove);
  21. CenterToScreen();
  22. }
  23. public static void Main()
  24. {
  25. Application.Run(new MForm());
  26. }
  27. private void OnMouseDown(object sender, MouseEventArgs e)
  28. {
  29. isDragging = true;
  30. oldX = e.X;
  31. oldY = e.Y;
  32. }
  33. private void OnMouseMove(object sender, MouseEventArgs e)
  34. {
  35. if (isDragging)
  36. {
  37. button.Top = button.Top + (e.Y - oldY);
  38. button.Left = button.Left + (e.X - oldX);
  39. }
  40. }
  41. private void OnMouseUp(object sender, MouseEventArgs e)
  42. {
  43. isDragging = false;
  44. }
  45. }

该代码示例将一个常规按钮控件放在表单容器上。 通过单击按钮表面并同时用鼠标拖动它,我们可以重新放置按钮。

  1. private bool isDragging = false;
  2. private int oldX, oldY;

这些是我们示例的支持变量。 isDragging变量告诉我们是否正在拖动对象。 oldXoldY变量在拖动过程开始之前存储 x,y 坐标。

  1. button.MouseDown += new MouseEventHandler(OnMouseDown);
  2. button.MouseUp += new MouseEventHandler(OnMouseUp);
  3. button.MouseMove += new MouseEventHandler(OnMouseMove);

我们为按钮插入了三种不同的鼠标处理器。 它们实现了拖放过程的三个不同阶段。 当我们单击按钮时,过程开始。 这由OnMouseDown()方法处理。 第二部分是机芯。 这是当我们将对象移动到新位置时。 它以OnMouseMove()方法处理。 最后一部分是过程停止的时间。 当我们释放鼠标按钮时会发生这种情况。 适当的任务委托给OnMouseUp()方法。

  1. private void OnMouseDown(object sender, MouseEventArgs e)
  2. {
  3. isDragging = true;
  4. oldX = e.X;
  5. oldY = e.Y;
  6. }

OnMouseDown()方法实现了过程的第一部分。 它设置了三个必要的变量。

  1. private void OnMouseMove(object sender, MouseEventArgs e)
  2. {
  3. if (isDragging)
  4. {
  5. button.Top = button.Top + (e.Y - oldY);
  6. button.Left = button.Left + (e.X - oldX);
  7. }
  8. }

OnMouseMove()方法中,我们重新定位按钮。 我们计算存储的 x,y 坐标与鼠标指针的新坐标之间的差。 差异将添加到按钮的TopLeft属性中,从而将其移动到新位置。

Mono Winforms 中的拖放 - 图1

图:拖动按钮

拖动文字

在前面的示例中,我们确实拖放了控件。 接下来,我们将对文本数据进行拖放操作。 在这里,我们将使用 Winforms 库提供的拖放协议。

拖放操作是 Winforms 中的标准通信协议。 我们有两个基本对象。 拖动源和放置目标。

dragtext.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. public class MForm : Form
  5. {
  6. private TextBox textBox;
  7. private Button button;
  8. public MForm()
  9. {
  10. InitForm();
  11. CenterToScreen();
  12. }
  13. private void OnMouseDown(object sender, MouseEventArgs e)
  14. {
  15. TextBox txt = (TextBox) sender;
  16. txt.DoDragDrop(txt.Text, DragDropEffects.Copy);
  17. }
  18. private void OnDragEnter(object sender, DragEventArgs e)
  19. {
  20. e.Effect = DragDropEffects.Copy;
  21. }
  22. private void OnDragDrop(object sender, DragEventArgs e)
  23. {
  24. Button button = (Button) sender;
  25. button.Text = (string) e.Data.GetData(DataFormats.Text);
  26. }
  27. private void InitForm()
  28. {
  29. Text = "Drag & drop";
  30. button = new Button();
  31. textBox = new TextBox();
  32. SuspendLayout();
  33. button.AllowDrop = true;
  34. button.Location = new Point(150, 50);
  35. textBox.Location = new Point(15, 50);
  36. button.DragDrop += new DragEventHandler(OnDragDrop);
  37. button.DragEnter += new DragEventHandler(OnDragEnter);
  38. textBox.MouseDown += new MouseEventHandler(OnMouseDown);
  39. ClientSize = new Size(250, 200);
  40. Controls.Add(button);
  41. Controls.Add(textBox);
  42. ResumeLayout();
  43. }
  44. public static void Main(string[] args)
  45. {
  46. Application.Run(new MForm());
  47. }
  48. }

我们在表单上有两个控件。 一个按钮和一个文本框。 我们将文本从文本框中拖放到按钮上。

  1. InitForm();

表单的设置委托给InitForm()方法。 这通常在较大的应用中完成。

  1. SuspendLayout();
  2. ...
  3. ResumeLayout();

我们在这两个方法调用之间布置控件。 这是一个优化。 它应该消除闪烁。

  1. button.AllowDrop = true;

我们将AllowDrop属性设置为true。 默认情况下不启用删除。

  1. button.DragDrop += new DragEventHandler(OnDragDrop);
  2. button.DragEnter += new DragEventHandler(OnDragEnter);
  3. textBox.MouseDown += new MouseEventHandler(OnMouseDown);

同样,拖放过程分为三个步骤。 对于每个特定步骤,我们有三种方法。

  1. private void OnMouseDown(object sender, MouseEventArgs e)
  2. {
  3. TextBox txt = (TextBox) sender;
  4. txt.DoDragDrop(txt.Text, DragDropEffects.Copy);
  5. }

OnMouseDown()方法中,我们初始化了拖放过程。 我们使用DoDragDrop()方法启动该过程。 DragDropEffects.Copy参数指定操作的类型。 实质上,我们可以在拖放操作期间复制文本或移动文本。

  1. private void OnDragEnter(object sender, DragEventArgs e)
  2. {
  3. e.Effect = DragDropEffects.Copy;
  4. }

当鼠标指针进入放置目标控件的区域时,将启动DragEnter事件。 必须设置Effect属性。 拖动源和放置目标的DragDropEffects必须相等。 否则,该操作将无法进行。

  1. private void OnDragDrop(object sender, DragEventArgs e)
  2. {
  3. Button button = (Button) sender;
  4. button.Text = (string) e.Data.GetData(DataFormats.Text);
  5. }

最后,我们有OnDragDrop()方法。 在这里,我们从事件对象获取数据并将其设置为按钮Text属性。

Mono Winforms 中的拖放 - 图2

图:文本拖放

拖动图像

在最后一个示例中,我们将拖放图像拖到窗体上。

dragimage.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. public class ImageDragDrop : Form
  5. {
  6. private bool isDragging;
  7. private int oldX, oldY;
  8. private Rectangle dropRect;
  9. private PictureBox picBox;
  10. private Bitmap image;
  11. private Brush brush;
  12. public ImageDragDrop()
  13. {
  14. ClientSize = new Size(350, 250);
  15. Text = "Dragging Image";
  16. Paint += new PaintEventHandler(OnPaint);
  17. isDragging = false;
  18. dropRect = new Rectangle(10, 10, 200, 160);
  19. brush = Brushes.Gray;
  20. picBox = new PictureBox();
  21. loadImage();
  22. picBox.Parent = this;
  23. picBox.Location = new Point(100, 50);
  24. picBox.Size = new Size(image.Width, image.Height);
  25. picBox.Image = image;
  26. picBox.Cursor = Cursors.Hand;
  27. picBox.MouseDown += new MouseEventHandler(OnMouseDown);
  28. picBox.MouseUp += new MouseEventHandler(OnMouseUp);
  29. picBox.MouseMove += new MouseEventHandler(OnMouseMove);
  30. CenterToScreen();
  31. }
  32. void loadImage() {
  33. try {
  34. image = new Bitmap("image.jpg");
  35. } catch {
  36. Console.WriteLine("Error reading image");
  37. Environment.Exit(1);
  38. }
  39. }
  40. public static void Main()
  41. {
  42. Application.Run(new ImageDragDrop());
  43. }
  44. private void OnMouseDown(object sender, MouseEventArgs e)
  45. {
  46. isDragging = true;
  47. oldX = e.X;
  48. oldY = e.Y;
  49. }
  50. private void OnMouseMove(object sender, MouseEventArgs e)
  51. {
  52. if (isDragging)
  53. {
  54. picBox.Top = picBox.Top + (e.Y - oldY);
  55. picBox.Left = picBox.Left + (e.X - oldX);
  56. }
  57. }
  58. private void OnMouseUp(object sender, MouseEventArgs e)
  59. {
  60. isDragging = false;
  61. if(dropRect.Contains(picBox.Bounds)) {
  62. brush = Brushes.Gold;
  63. } else {
  64. brush = Brushes.Gray;
  65. }
  66. Refresh();
  67. }
  68. private void OnPaint(object sender, PaintEventArgs e)
  69. {
  70. Graphics g = e.Graphics;
  71. g.FillRectangle(brush, dropRect);
  72. }
  73. }

在我们的示例中,我们有一个PictureBox,并绘制了一个灰色矩形。 如果将图片放在矩形内,则矩形的颜色变为金色。

  1. brush = Brushes.Gray;

brush变量保存矩形的笔刷。 默认情况下为灰色。

  1. void loadImage() {
  2. try {
  3. image = new Bitmap("image.jpg");
  4. } catch {
  5. Console.WriteLine("Error reading image");
  6. Environment.Exit(1);
  7. }
  8. }

loadImage()加载PictureBox控件的位图。

  1. if (dropRect.Contains(picBox.Bounds)) {
  2. brush = Brushes.Gold;
  3. } else {
  4. brush = Brushes.Gray;
  5. }

OnMouseUp()方法中,我们确定矩形的笔刷。 如果图片框的边界在矩形内,则画笔为金色;否则,画笔为金色。 否则为灰色。

  1. Refresh();

我们必须调用Refresh()方法来激活新的画笔颜色。

本章专门使用 Mono Winforms 库拖放操作。