【问题标题】:Panel Events Stop After MouseDown event on LineShape面板事件在 LineShape 上的 MouseDown 事件后停止
【发布时间】:2015-06-17 22:03:58
【问题描述】:

在 Visual Studio 2010 中,我将鼠标事件与位于面板内的 LineShapes 一起使用。我创建了一个简单的示例,该示例演示了我无法解释的行为并且正在阻止我的项目。

Public Class Form1
    Public Moused_Down_On_Line As Boolean = False
    Public Moused_Down_On_Panel As Boolean = False
    Public Moused_Move_Count As Integer = 0

    Private Sub Panel1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        Moused_Move_Count += 1
        TextBox1.Text = Moused_Move_Count.ToString() + " (" + Moused_Down_On_Line.ToString + ", " + Moused_Down_On_Panel.ToString + ")"
    End Sub

    Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        Moused_Down_On_Panel = True
    End Sub

    Private Sub LineShape1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LineShape1.MouseDown
        Moused_Down_On_Line = True
    End Sub

    Private Sub LineShape1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LineShape1.MouseUp, Panel1.MouseUp
        Moused_Down_On_Panel = False
        Moused_Down_On_Line = False
    End Sub

End Class

当我将鼠标移到面板上时,TextBox1 显示我正在收到预期的 MouseMove 事件。

当我在面板上单击鼠标按钮并按住它时,TextBox1 仍然显示我正在收到预期的 MouseMove 事件。

但是,当我在 LineShape 上单击鼠标按钮并按住它时,TextBox1 显示我不再获得预期的 MouseMove 事件。此外,如果我在面板上释放鼠标按钮,我也不会得到 MouseUp 事件。

有人可以向我解释我做错了什么吗?单击 LineShape 后,我真的需要继续为面板获取鼠标事件!

已编辑:

我添加了这个事件,但 TextBox 显示当我将鼠标移到 LineShape 上时,我只收到 MouseMove 事件:

Private Sub LineShape1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LineShape1.MouseMove
    Moused_Move_Count += 1
    TextBox1.Text = "LineShape Event" + Moused_Move_Count.ToString() + " (" + Moused_Down_On_Line.ToString + ", " + Moused_Down_On_Panel.ToString + ")"
End Sub

【问题讨论】:

  • 这是完全正常的,你点击的控件捕获了鼠标。也许您想添加 LineShape 的 MouseMove 事件处理程序,也许您想在 LineShape1_MouseDown 事件处理程序中添加ShapeContainer1.Capture = False。当您不解释您要做什么时,非常不清楚哪个是最好的。
  • 感谢您的回复。最终,当我单击 LineShape 并在面板周围移动鼠标时,我希望在面板周围拖动 LineShape。
  • 这使得使用 LineShape 的 MouseMove 事件成为最明显的选择。只需添加它。
  • 当我将鼠标移到 LineShape 上时,我不会只得到 LineShape 的 MouseMove 事件吗?当我将鼠标移出 LineShape 时,我需要获取事件,以便我可以根据鼠标移动的位置修改 X 和 Y 属性。
  • 你唯一能犯的错误就是不去尝试。

标签: .net vb.net panels powerpacks


【解决方案1】:

感谢 Hans 解释了为什么我不再为我的控制面板获取鼠标事件。看来我通过在与 LineShape 相同的 ShapeContainer 中创建一个 RectangleShape 有效地解决了我的问题。 RectangleShape 的大小与面板一样,具有自定义边框样式和透明填充样式。即使单击 LineShape 并按住它,我仍然会收到 RectangleShape 的 MouseMove 事件。

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2014-12-28
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多