当鼠标单击它时以及当鼠标键向上然后停止在组框内移动时,我们如何在Windows窗体中移动标签 [英] How we move a label in windows forms when mouse click on it and when mouse key up then stop moving inside a group box

查看:112
本文介绍了当鼠标单击它时以及当鼠标键向上然后停止在组框内移动时,我们如何在Windows窗体中移动标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此示例,但它不起作用

I'm Trying with this example but it's not working

public frmChequeFormat()
    {
        InitializeComponent();
        gbCheque.MouseMove += gbCheque_MouseMove;
    }
    bool mDown = false;
    private void gbCheque_MouseMove(object sender, MouseEventArgs e)
    {
        if (mDown)
        {
            label13.Location = e.Location;
        }
    }
    private void label13_MouseDown(object sender, MouseEventArgs e)
    {
        mDown = true;
    }
    private void label13_MouseUp(object sender, MouseEventArgs e)
    {
        mDown = false;
    }

推荐答案

不确定为什么要处理组框鼠标移动。处理标签鼠标移动是否合理,例如

Not sure why you handle the group box mouse move. Would it be resonable to handle the labels mouse move, for example
private void label13_MouseDown(object sender, MouseEventArgs e) {
   mDown = true;
}

private void label13_MouseUp(object sender, MouseEventArgs e) {
   mDown = false;
}

private void label13_MouseMove(object sender, MouseEventArgs e) {
   if (mDown) {
      label1.Location = new Point(label1.Location.X + e.Location.X, label1.Location.Y + e.Location.Y);
   }
}


你需要MouseDown MouseUp 的MouseMove 。所有这些都在 GroupBox 中。因此,您需要将其添加到表单构造函数中:
You need MouseDown, MouseUp and MouseMove. All of them within the GroupBox. So you need to add this to your form constructor:
public frmChequeFormat()
{
    label13.MouseDown += label13_MouseDown;
    label13.MouseUp += label13_MouseUp;
    label13.MouseMove += label13_MouseMove;  // You already have this one.
}

然后重命名相应的 gbCheque _ * 方法,以便它们被调用。





重命名上面的事件和方法名称从gbCheque *到label13 *



该标签可以防止你的群组获取这些事件。它们被路由到标签。因此重命名与我的第一次尝试形成鲜明对比。



事件应仍然被路由到标签,但现在从那里调用处理程序。至少在用户将鼠标快速移动到左上方之前,它会离开标签,从而导致标签事件停止发射。

[/ Edit]

Then rename your gbCheque_* methods accordingly so they get called.


Renamed event and method names above from gbCheque* to label13*

The label shields your groupbox from getting those events. They are routed to the label instead. Hence the renaming in contrast to my first attempt.

Events should still be routed to the label, but now call handlers from there. At least until user moves the mouse very quickly to the upper left so it leaves the label and therefore causes label events to stop firing.
[/Edit]


男人,如果只有人写了一篇关于这样做的文章。



创建自己的运行时可移动Windows窗体控件 [ ^ ]
Man, if only someone wrote an article on doing just this.

Create your Own Runtime Movable Windows Forms Controls[^]


这篇关于当鼠标单击它时以及当鼠标键向上然后停止在组框内移动时,我们如何在Windows窗体中移动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆