拖动对象时启用其他事件 [英] Enable other event while dragging an object

查看:80
本文介绍了拖动对象时启用其他事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个控制台,我想在其中将按钮拖动到网格:

I'm developing a console where I want to drag a button to a grid:

要拖动按钮,请使用以下过程:

To drag the button, I use the following procedure:

Public drag As Boolean = False
Public ptX As Integer = 0
Public ptY As Integer = 0
Public btn As Button

Private Sub MoveButton_MouseDown(sender As Object, e As MouseEventArgs) Handles MoveButton.MouseDown
    drag = True
    btn = CType(sender, Button)
    ptX = e.X : ptY = e.Y
End Sub

Private Sub MoveButton_MouseMove(sender As Object, e As MouseEventArgs) Handles MoveButton.MouseMove
    If drag Then
        btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
        Me.Refresh()
    End If
End Sub

Private Sub MoveButton_MouseUp(sender As Object, e As MouseEventArgs) Handles MoveButton.MouseUp
    drag = False
End Sub

到目前为止,太好了!在这个问题上,这很好用. 但是,我试图在将单元格悬停在该单元格上的同时突出显示该单元格,如下所示:

So far, so good! This works fine for that matter. However, I'm trying to highlight the cell while hoovering the button on it like this:

为此,我尝试执行以下操作:

To do so, I tried to do the following:

Private Sub CellA1_MouseHover(sender As Object, e As EventArgs) Handles CellA1.MouseHover
    If drag Then
        CellA1.BackColor = Color.Red
    End If
End Sub

我当然不能这样做,除非我以某种方式在拖动MoveButton时启用了CellA1.MouseHover事件.

Of course I can't do that, unless I, somehow, enable the CellA1.MouseHover event while dragging the MoveButton.

有人可以帮我吗?

但是,如果您有苦苦挣扎的意愿来进一步帮助我,那么我的最后一个目标是将MoveButton放在红细胞位置:

If, however, you're having a struggling will to help me further, my last goal is to place the MoveButton on the red cell place:

但是,由于我还没有代码可以执行此操作,因此请在过程的这一部分完全不帮我. 任何帮助将不胜感激.而且,一如既往,谢谢大家.

But feel free to don't help me at all with this part of the procedure since I have no code to perform this yet. Any help will be very appreciated. And, as always, thank you all in advance.

推荐答案

由于在拖动按钮时鼠标实际上不在PictureBox上,因此它永远不会引发任何鼠标事件.您可以做的是调用 表单中的GetChildAtPoint()方法 ,以获取按钮 后面的控件.确认后,只需确认名称以"Cell"开头,然后更改背景色即可.

Since your mouse is not actually on the PictureBox when you drag the button it will never raise any mouse events. What you can do instead is to call the GetChildAtPoint() method of your form to get the control behind the button. Once you have that just verify that the name starts with "Cell" and change the back color.

要捕捉到该单元格的位置,您需要向MouseUp指示我们当前所在的单元格.只需将单元格控件存储在变量中,然后就可以设置yourButton.Location = currentCell.Location

To snap to the cell's location you'll need to indicate to MouseUp which cell we're currently at. Simply store the cell control in a variable and you can then just set yourButton.Location = currentCell.Location

以下是我对您的代码所做的更改,为清楚起见,发表了评论:

Here are the changes I've made to your code, commented for clarity:

Public drag As Boolean = False
Public ptX As Integer = 0
Public ptY As Integer = 0
Public btn As Button
Public prevCtrl As Control = Nothing 'Store the previous Cell the button was dragged over.
'                                     We need this to be able to reset the BackColor of the Cell, 
'                                     and also so that you can snap to its location once you drop the button.

Private Sub MoveButton_MouseDown(sender As Object, e As MouseEventArgs) Handles MoveButton.MouseDown
    'No changes made here.
    drag = True
    btn = CType(sender, Button)
    ptX = e.X : ptY = e.Y
End Sub

Private Sub MoveButton_MouseMove(sender As Object, e As MouseEventArgs) Handles MoveButton.MouseMove
    If drag Then
        btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)

        'Go 1 pixel up, or else GetChildAtPoint() will return the button instead of the control behind it.
        Dim LookPoint As Point = Point.Subtract(btn.Location, New Size(0, 1))

        'Get the control located below/behind the button.
        Dim ControlBelow As Control = Me.GetChildAtPoint(LookPoint, GetChildAtPointSkip.Invisible Or GetChildAtPointSkip.Disabled) 'Ignore invisible or disabled controls.

        'Check so that the previous cell is not also the current cell. If they're the same then we won't change anything.
        If prevCtrl IsNot ControlBelow Then

            'Ok, the current cell and the previous cell are not the same.
            'Now check if there was any previous cell at all.
            If prevCtrl IsNot Nothing Then
                'There was a previous cell, but since the button 
                'is no longer hovering over it we reset its BackColor.
                prevCtrl.BackColor = Color.White
                prevCtrl = Nothing
            End If

            'Check that there infact is a control behind the button,
            'and also check that its name starts with "Cell".
            If ControlBelow IsNot Nothing AndAlso ControlBelow.Name.StartsWith("Cell", StringComparison.OrdinalIgnoreCase) Then
                'The control behind the button is a valid Cell. Change its BackColor.
                ControlBelow.BackColor = Color.Red
                prevCtrl = ControlBelow 'The previous cell is now the current cell.
            End If

        End If

        'Me.Refresh() - this is a very unnecessary call, it will just eat CPU. The form does not need to be redrawn at this point.
    End If
End Sub

Private Sub MoveButton_MouseUp(sender As Object, e As MouseEventArgs) Handles MoveButton.MouseUp
    'Check if we dragged the button. At this point prevCtrl is the current cell (if it's not Nothing).
    If drag = True AndAlso prevCtrl IsNot Nothing Then
        btn.Location = prevCtrl.Location 'Snap to the cell's location.
        prevCtrl.BackColor = Color.White 'Reset the cell's BackColor.
        prevCtrl = Nothing 'Reset this since we're no longer dragging the button.
    End If

    drag = False
End Sub

它就像一种魅力!

这篇关于拖动对象时启用其他事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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