【发布时间】:2016-03-12 20:22:19
【问题描述】:
我在面板和面板中拖放按钮以识别它并显示带有按钮名称的消息时遇到了一些麻烦
到目前为止,我管理了拖放和识别的部分,但是我错过了拖动的视觉样式,当我用鼠标按下时,它只会坐在同一个地方,它不会跟随光标。如何让它跟随鼠标?
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
button1.MouseDown += button1_MouseDown;
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
button1.DoDragDrop(button1.Text, DragDropEffects.Copy | DragDropEffects.Move);
button1.Location= new Point(e.X, e.Y);
}
private void panel_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void panel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
MessageBox.Show(e.Data.GetData(DataFormats.Text).ToString());
}
【问题讨论】:
-
让按钮的位置跟随鼠标移动
-
@Plutonix 你能写一个代码示例吗,我知道我必须做到这一点,但仍然没有成功
标签: c# winforms drag-and-drop