在Winform窗体中通过鼠标拖动,改变控件的位置。在拖动过程中,跟随鼠标显示一个与被拖动控件大小一样的黑框,用以模拟拖动效果。如下图:
以下是源代码。这里拖动了一个Button控件。如果需要,还可以在拖动时改变光标。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsApplication1
- {
- public partial class FormDrag : Form
- {
- //将被拖动的控件
- private Control control;
- public FormDrag()
- {
- InitializeComponent();
- this.Paint += new System.Windows.Forms.PaintEventHandler(this.FormDrag_Paint);
- control = new Button();
- control.MouseDown += new

本文介绍了如何在.NET Winform应用程序中实现用户可以通过鼠标任意拖动窗体内的控件,包括添加事件处理程序,记录鼠标按下位置,更新控件位置,并在窗体上绘制拖动效果。
最低0.47元/天 解锁文章
4633

被折叠的 条评论
为什么被折叠?



