拖动对象时启用其他事件

2023-12-04

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

interface example

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

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

到目前为止,一切都很好!这对于此事来说效果很好。 但是,我试图在将按钮悬停在单元格上时突出显示该单元格,如下所示:

interface example

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

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

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

谁能帮我这个?

但是,如果您不愿意进一步帮助我,我的最后一个目标就是让MoveButton在红细胞位置:

interface example

但请随意不要帮助我完成这部分过程,因为我还没有代码来执行此操作。 任何帮助将不胜感激。一如既往,提前感谢大家。


由于您的鼠标实际上并未位于PictureBox当您拖动按钮时,它永远不会引发任何鼠标事件。你可以做的是调用GetChildAtPoint() method您的表单以获得控制权behind按钮。完成后,只需验证名称是否以“Cell”开头并更改背景颜色即可。

要捕捉到单元格的位置,您需要指出MouseUp我们目前在哪个牢房。只需将单元格控件存储在变量中,然后您就可以设置yourButton.Location = currentCell.Location

以下是我对您的代码所做的更改,为了清楚起见,进行了注释:

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

它就像一个魅力!

Drag & Drop example

Snap example

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

拖动对象时启用其他事件 的相关文章

随机推荐

  • 添加到向量或从向量中删除后,指向向量元素的指针是否保留(在 C++ 中)

    我正在研究碰撞引擎 更具体地说 我正在尝试制作世界上相关物体的向量 为了能够访问世界上代表特定对象的主体向量中的特定主体 我需要知道哪个特定主体代表该对象 为此 当您添加新主体时 我想在主体向量中返回一个指向主体的指针 但是当我删除主体时会
  • 如何检测文件是否被包含或直接运行

    我有一个 php 文件 包含在我的 php 脚本中 但我不希望人们能够直接运行该文件 不包含在内 我怎样才能防止这种情况发生 制作包含的脚本根本无法通过 HTTP 访问 例如 通过保护子文件夹或将它们移动到文档根目录之上 如果你做不到这一点
  • PrimeFaces Extensions CKEditor:无法访问自定义配置

    我需要我的pe ckEditor加载自定义配置 但我在这件事上已经有一段时间不成功了 我将欢迎任何如何使其工作的建议 我需要它的功能 因为我有这个问题 PrimeFaces Extensions CKEditor 尝试将编码设置为 UTF
  • Silverlight 5 AccessViolationException

    我安装了 Silverlight 5 VS 2010 工具和 64 位开发人员运行时 现在当我执行特定操作时 我收到 System AccessViolationException 这些项目仍然是Silverlight 4 我还没有升级它们
  • Entrypoint的exec形式通过shell执行

    我正在构建一个基于 Windows 的 docker 镜像 FROM mcr microsoft com dotnet framework aspnet 4 8 windowsservercore ltsc2019 omitted for
  • 在标签中绑定值

    是否可以将 JavaFX 滑块中的双值绑定到标签 我想要得到这样的东西 m maxSlider new Label Right Slider Val m slider getValue1 m maxSlider textProperty b
  • Google Cloud Messaging 上的最大主题数

    我想避免在应用程序级别管理设备令牌的复杂性 一名用户在 iOS 和 Android 上使用多台设备 多个用户订阅一个应用程序组 将 1 和 2 作为 GCM 的主题显然是一个简单的解决方案 这样我只需在发送通知时处理我的应用程序 UserI
  • 文件无效:错误:转储失败,因为找不到 AndroidManifest.xml

    当我尝试在 Android 市场上传我的 Android 应用程序时 我遇到了这个问题 我去过stackoverflow 的这个问答我相信导出时我的 AndroidManifest xml 位于 apk 文件中 并且该 xml 文件的所有内
  • AJAX上传显示多个文件上传中仅文件的进度

    请帮我更改下面的代码 我有多个从 HTML 上传的文件 ajax 将所有上传的文件发布到一个 php 脚本 该脚本向进度函数发送反馈 问题是所有三个进度条只监听一个文件上传
  • 如何在 JSF 中收集 List 的提交值?

    我有一颗豆子List
  • 是否可以实现 __super 宏?

    请告诉我是否有办法手动实现微软特定的 super宏观 class Base public void func something class Derived public Base public void func Base func ju
  • 带有 Java 线性和对数滤波器的 MFCC

    我正在用Java实现MFCC算法 Java 中有三角滤波器和 MFCC 的示例代码 链接在这里 MFCC Java但是我应该遵循用 Matlab 编写的代码 MFCC MATLAB 我的问题是在 Matlab 代码中 它讨论了线性和对数滤波
  • Java 8 流是原子的吗?

    我读了一些帖子 但我仍然很困惑 我知道并行流将以利用 CPU 的并行方式执行 我相信子作业将作为原子单元执行 对吗 但是常规的 Java 8 流呢 如果我执行下一行代码 users stream map user gt user getUs
  • 在表单提交的网址中间添加问号

    当我单击表单中的提交按钮时 它会添加一个 就在 之前 因此 app pageName 更改为 app pageName 这是正常行为吗 代码只是基本的东西 angular module myApp controller MyCtrl fun
  • 如何在进入 Java 应用程序时更改光标

    我遇到了一些我无法解决的问题 我正在编写一个带有接受拖放操作的 JList 的 Swing Java 应用程序 我想在将文件或文件夹从系统拖到 Java 应用程序上时更改光标 我自己找到了 不过还是感谢克林顿的回答 这是我用过的 首先创建
  • Socket tcp C# 如何清除输入缓冲区?

    我正在为 Windows Phone 编写一个应用程序 我需要与服务器通信并传输数据 SERVER是用C 编写的 我无法修改它 客户是我必须写的 服务器被设计为客户端连接到它并传输数据 连接对于所有传输都保持打开状态 通过用 C 编写代码
  • Infinispan相当于ehcache的copyOnRead和copyOnWrite

    我计划在现有的网络应用程序中实施缓存解决方案 没什么复杂的 基本上是一个支持溢出到磁盘和自动驱逐的并发映射 将来可能需要对缓存进行集群 但不是现在 我喜欢 ehcache 的 copyOnRead 和 copyOnWrite 功能 因为这意
  • 将 Crypto++ 对象保存到 std::vector

    我想将 Crypto 密钥保存到std vector
  • 如何使scrollviewer滚动像素而不是组件(wpf)

    我试图让我的滚动查看器完美地滚动 也就是说 我有一个滚动查看器 它包含一个 stackpanel 堆栈面板包含一个用户制作的用户控件 并且它们可以在运行时动态增加或减少 问题是 假设我的堆栈面板中只有 2 个用户控件 滚动查看器只有 2 级
  • 拖动对象时启用其他事件

    我正在开发一个控制台 我想将按钮拖动到网格中 要拖动按钮 我使用以下过程 Public drag As Boolean False Public ptX As Integer 0 Public ptY As Integer 0 Public