vba MouseMove - 什么是反向事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12200618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:31:48  来源:igfitidea点击:

MouseMove - What is the reverse event?

excelvba

提问by Alegro

Private Sub framePDF_MouseMove(ByVal... )
framePDF.BackColor = &H80000012&  

So, the frame's color is changing.
I can't find the event to return the color back - when the cursor is away from the frame ?

因此,框架的颜色正在发生变化。
我找不到返回颜色的事件 - 当光标远离框架时?

采纳答案by Matt Wilko

In vba and VB6 there is no MouseLeave event.

在 vba 和 VB6 中没有 MouseLeave 事件。

The best way to achieve this is to start a timer when the mouse enters the frame.

实现此目的的最佳方法是在鼠标进入框架时启动计时器。

Then in the timer code check to see if the mouse pointer is still within the bounds of the frame. If not change the colour back and stop the timer

然后在计时器代码中检查鼠标指针是否仍在帧的边界内。如果没有改变颜色并停止计时器

Put this code in a module:

将此代码放入模块中:

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

Public Type POINTAPI
        x As Long
        y As Long
End Type

Create a timer on your form, set interval =10Enbaled = False

在您的表单上创建一个计时器,设置 interval =10Enbaled = False

Then the code looks something like this:

然后代码看起来像这样:

Private Sub frameTest_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    frameTest.BackColor = vbRed
    tmrMouseLeave.Enabled = True
End Sub

Private Sub tmrMouseLeave_Timer()
    Dim pt As POINTAPI
    Call GetCursorPos(pt)
    Dim xValue As Long, yValue As Long
    xValue = pt.x * Screen.TwipsPerPixelX
    yValue = pt.y * Screen.TwipsPerPixelY

    If (xValue > (Me.Left + frameTest.Left)) And _
       (xValue < (Me.Left + frameTest.Left + frameTest.width)) And _
       (yValue > (Me.Top + frameTest.Top)) And _
       (yValue < (Me.Top + frameTest.Top + frameTest.height)) Then
        'we are still inside the frame
    Else
        'mouse is outside the frame
        frameTest.BackColor = vbBlue
        tmrMouseLeave.Enabled = False
    End If
End Sub

回答by Dick Kusleika

On a Userform? The Userform also has a MouseMove event that doesn't fire when you're in the Frame.

在用户表单上?用户窗体还有一个 MouseMove 事件,当您在框架中时不会触发该事件。

Private Sub Frame1_MouseMove(ByVal ...)

    Me.Frame1.BackColor = vbRed

End Sub

Private Sub UserForm_MouseMove(ByVal ...)

    Me.Frame1.BackColor = vbWhite

End Sub

will turn the frame red when you're over it and white when you're not. These events fire constantly, so use them judiciously.

当你超过它时会将框架变成红色,当你不在时将它变成白色。这些事件会不断触发,因此请谨慎使用它们。

回答by Simon

Easier way: in your MouseMove event, test the X and Y arguments against the control's width and height (minus a margin, say 5) - if the mouse is in the margin, consider it a "Mouse out" and change the control's colours accordingly. No need for concurrent buttons, z-order manipulation, frames, etc.

更简单的方法:在您的 MouseMove 事件中,针对控件的宽度和高度(减去边距,比如 5)测试 X 和 Y 参数 - 如果鼠标在边距中,将其视为“鼠标移出”并相应地更改控件的颜色. 不需要并发按钮、z 顺序操作、框架等。