191.md 10.0 KB
Newer Older
W
wizardforcel 已提交
1
# Mono Winforms 中的拖放
W
wizardforcel 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

> 原文: [http://zetcode.com/gui/csharpwinforms/dragdrop/](http://zetcode.com/gui/csharpwinforms/dragdrop/)

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

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

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

## 拖动按钮

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

`dragbutton.cs`

```
using System;
using System.Drawing;
using System.Windows.Forms;

public class MForm : Form
{
  private bool isDragging = false;
  private int oldX, oldY;
  private Button button;

  public MForm()
  {
    Text = "Drag & drop button";
    Size = new Size(270, 180);

    button = new Button();
    button.Parent = this;
    button.Cursor = Cursors.Hand;
    button.Text = "Button";
    button.Location = new Point(20, 20);

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

    CenterToScreen();
  }

  public static void Main() 
  {
    Application.Run(new MForm());
  }

  private void OnMouseDown(object sender, MouseEventArgs e) 
  {
    isDragging = true;
    oldX = e.X;
    oldY = e.Y;
  }

  private void OnMouseMove(object sender, MouseEventArgs e) 
  {
    if (isDragging) 
    {
        button.Top = button.Top + (e.Y - oldY); 
        button.Left = button.Left + (e.X - oldX); 
    }
  }

  private void OnMouseUp(object sender, MouseEventArgs e) 
  {
    isDragging = false;
  }
}

```

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

```
private bool isDragging = false;
private int oldX, oldY;

```

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

```
button.MouseDown += new MouseEventHandler(OnMouseDown);
button.MouseUp += new MouseEventHandler(OnMouseUp);
button.MouseMove += new MouseEventHandler(OnMouseMove);

```

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

```
private void OnMouseDown(object sender, MouseEventArgs e) 
{
  isDragging = true;
  oldX = e.X;
  oldY = e.Y;
}

```

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

```
private void OnMouseMove(object sender, MouseEventArgs e) 
{
  if (isDragging) 
  {
      button.Top = button.Top + (e.Y - oldY); 
      button.Left = button.Left + (e.X - oldX); 
  }
}

```

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

![Dragging a button](img/c5d743923cf6bed9b2026716ff8cbb3c.jpg)

Figure: Dragging a button

W
wizardforcel 已提交
124
## 拖动文字
W
wizardforcel 已提交
125

W
wizardforcel 已提交
126
在前面的示例中,我们确实拖放了控件。 接下来,我们将对文本数据进行拖放&拖放操作。 在这里,我们将使用 Winforms 库提供的拖放&拖放协议。
W
wizardforcel 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

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

`dragtext.cs`

```
using System;
using System.Drawing;
using System.Windows.Forms;

public class MForm : Form
{

    private TextBox textBox;
    private Button button;

    public MForm()
    {
        InitForm();
        CenterToScreen();
    }

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

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

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

    private void InitForm()
    {
        Text = "Drag & drop";
        button = new Button();
        textBox = new TextBox();
        SuspendLayout();

        button.AllowDrop = true;
        button.Location = new Point(150, 50);
        textBox.Location = new Point(15, 50);

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

        ClientSize = new Size(250, 200);
        Controls.Add(button);
        Controls.Add(textBox);
        ResumeLayout();
    }

    public static void Main(string[] args)
    {
        Application.Run(new MForm());
    }

}

```

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

```
InitForm();

```

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

```
SuspendLayout();
...
ResumeLayout();

```

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

```
button.AllowDrop = true;

```

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

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

```

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

```
private void OnMouseDown(object sender, MouseEventArgs e)
{
    TextBox txt = (TextBox) sender;
    txt.DoDragDrop(txt.Text, DragDropEffects.Copy);
}

```

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

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

```

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

```
private void OnDragDrop(object sender, DragEventArgs e)
{
    Button button = (Button) sender;
    button.Text = (string) e.Data.GetData(DataFormats.Text);
}

```

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

![Drag & drop of text](img/0dbb8ce142052045886d6e4f660fbdd5.jpg)

Figure: Drag & drop of text

W
wizardforcel 已提交
266
## 拖动图像
W
wizardforcel 已提交
267

W
wizardforcel 已提交
268
在最后一个示例中,我们将拖放图像拖到窗体上。
W
wizardforcel 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

`dragimage.cs`

```
using System;
using System.Drawing;
using System.Windows.Forms;

public class ImageDragDrop : Form
{
  private bool isDragging;
  private int oldX, oldY;

  private Rectangle dropRect;
  private PictureBox picBox;
  private Bitmap image;
  private Brush brush;

  public ImageDragDrop()
  {
    ClientSize = new Size(350, 250);
    Text = "Dragging Image";
    Paint += new PaintEventHandler(OnPaint);

    isDragging = false;
    dropRect = new Rectangle(10, 10, 200, 160);
    brush = Brushes.Gray;
    picBox = new PictureBox();
    loadImage();

    picBox.Parent = this;
    picBox.Location = new Point(100, 50);
    picBox.Size = new Size(image.Width, image.Height);
    picBox.Image = image;
    picBox.Cursor = Cursors.Hand;

    picBox.MouseDown += new MouseEventHandler(OnMouseDown);
    picBox.MouseUp += new MouseEventHandler(OnMouseUp);
    picBox.MouseMove += new MouseEventHandler(OnMouseMove);

    CenterToScreen();
  }

  void loadImage() {
      try {
          image = new Bitmap("image.jpg");
      } catch {
          Console.WriteLine("Error reading image");
          Environment.Exit(1);
      }
  }

  public static void Main() 
  {
    Application.Run(new ImageDragDrop());
  }

  private void OnMouseDown(object sender, MouseEventArgs e) 
  {
    isDragging = true;
    oldX = e.X;
    oldY = e.Y;
  }

  private void OnMouseMove(object sender, MouseEventArgs e) 
  {
    if (isDragging) 
    {
      picBox.Top = picBox.Top + (e.Y - oldY);
      picBox.Left = picBox.Left + (e.X - oldX);
    }
  }

  private void OnMouseUp(object sender, MouseEventArgs e) 
  {
    isDragging = false;

    if(dropRect.Contains(picBox.Bounds)) {
        brush = Brushes.Gold;
    } else {
        brush = Brushes.Gray;
    }

    Refresh();
  }

  private void OnPaint(object sender, PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.FillRectangle(brush, dropRect);
  }
}

```

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

```
brush = Brushes.Gray;

```

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

```
void loadImage() {
    try {
        image = new Bitmap("image.jpg");
    } catch {
        Console.WriteLine("Error reading image");
        Environment.Exit(1);
    }
}

```

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

```
if (dropRect.Contains(picBox.Bounds)) {
    brush = Brushes.Gold;
} else {
    brush = Brushes.Gray;
}

```

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

```
Refresh();

```

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

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