在 ActiveX 复选框上使用 MouseMove 创建工具提示 [英] Create Tooltip using MouseMove on ActiveX Checkbox

查看:39
本文介绍了在 ActiveX 复选框上使用 MouseMove 创建工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让工具提示显示在我的 Excel 电子表格上的 ActiveX 复选框上.

I am trying to get a tooltip to display over an ActiveX Checkbox on my excel spreadsheet.

下面的代码确实显示和隐藏了工具提示,但与预期不同.如果您过快地将鼠标移到复选框上,工具提示(标签)将保留在工作表上.

The below code does display and hide the tooltip but not as expected. If you move the mouse over the checkbox too quickly, the tooltip (label) will remain on the sheet.

Private Sub chkPrice_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

With sht
     If .lblTooltip.Visible = False Then
          .lblTooltip.Visible = True
     ElseIf .lblTooltip.Visible = True Then
          .lblTooltip.Visible = False
     End If
End With

为了使上面的代码工作,如果有类似的东西:

To make the above code work, if there is something along the lines of:

If mousehovers for 1 second Then display the tooltip

有没有办法检查鼠标停留在控件上的时间?

Is there a way to check for amount of time the mouse remains on a control?

还有其他方法可以做到这一点吗?

Is there another way to do this?

推荐答案

您可以为此做一些非常棘手的事情,因此您可以在下面找到具体答案的起点.

There is something pretty tricky that you could do for that, so below you can find a starting point for your specific answer.

直接回答问题

在您的模块之上,您声明了获取鼠标坐标的 Microsoft API:

On top of your module, you declare the Microsoft API getting the mouse coordinates:

Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

注意:如果您使用的是 32 位系统,请从声明中删除 PtrSafe 关键字.此外,在模块的顶部,添加以下类型:

Note: if you're using a 32-bit system, remove the PtrSafe keyword from the declaration. Also, on top of the module, you add this type:

Type POINTAPI
   Xcoord As Long
   Ycoord As Long
End Type

因此,在您的宏 chkPrice_MouseMove 中,您可以执行以下操作:

Hence, inside your macro chkPrice_MouseMove, you do something like this:

  • 当宏被触发时,获取鼠标坐标
  • 请稍等,说半秒
  • 因此,再次获取鼠标坐标.如果它们与以前相同,则表示用户将鼠标保持在同一点上,以便您可以触发事件.

在代码中:

Private Sub chkPrice_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

Dim llCoordBefore As POINTAPI
Dim llCoordAfter As POINTAPI

GetCursorPos llCoordBefore '<-- get first time
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now())+0.5)
GetCursorPos llCoordAfter '<-- get second time

If llCoordBefore.Xcoord = llCoordAfter.Xcoord And llCordBefore.Ycoord = llCoordAfter.Ycoord Then '<-- compare

    With sht
         If .lblTooltip.Visible = False Then
              .lblTooltip.Visible = True
         ElseIf .lblTooltip.Visible = True Then
              .lblTooltip.Visible = False
         End If
    End With
End If

为什么我不使用它

我认为最好的办法是将 ActiveX 控件放在用户不会误将鼠标悬停的位置.

I think that your best shot is to put the ActiveX control in a place where the user doesn't hover by mistake.

您的代码在鼠标悬停时表示:

Your code, on mouse hover, says that:

1) 如果工具提示不可见,则使其可见2) 如果工具提示可见,则隐藏它

1) If the tooltip is not visible, then make it visible 2) If the tooltip is visible, then hide it

如果用户快速通过控件,并且隐藏了工具提示,则预计它会显示并且不会隐藏.用户应该返回控件以使其再次隐藏.

If the user passes quickly on the control, and the tooltip is hidden, it's expected that it will show up and don't hide. The user should pass back on the control to get it hidden again.

无论如何,以下是您所考虑的上述方法不是我推荐的一些原因:

In any case, here are some reasons why the above method you thought about is not my recommendation:

  • 应用程序将等待 X 秒.如果用户错误地悬停,他的 Excel 将等待 X 秒,然后他才能再次控制.在用户体验方面可能会很烦人.
  • API 以非常敏感的方式获取坐标.您将需要实现一个近似值(在我的示例中,我仅在悬停前后坐标完全相同时才继续进行;但是,在现实生活中,您需要留出一些余量,因为鼠标可能会在第一次和第二次获取坐标).

这篇关于在 ActiveX 复选框上使用 MouseMove 创建工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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