VBA 表单 - 带图像的按钮

问题描述 投票:0回答:1

我正在尝试在表单上使用网络样式按钮,并使用一些图片(确定关闭按钮、确定按下按钮和确定按钮)。

我正在尝试做与网站中相同的事情。鼠标滑过时更改按钮颜色,单击时再次更改颜色。

但是我在这里遗漏了一些东西。我已经实现了当鼠标悬停在按钮上时更改按钮图像,但是当我单击它时,仅更改图片(通过程序

MouseMove
),但是当我释放鼠标按钮时,事件无法转到
mouseUp
事件。我错过了什么?

Private Sub okpress_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If okpress.Visible = True Then
        okoff.Visible = False
        okpress.Visible = True
        ok.Visible = False
    End If
    MsgBox "ha entrado", vbOKOnly, "Prueba"
End Sub

Private Sub okoff_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If okoff.Visible = True Or okpress.Visible = True Then
        okoff.Visible = False
        okpress.Visible = False
        ok.Visible = True
    End If
End Sub

Private Sub ok_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If okpress.Visible = False Then
        okoff.Visible = False
        okpress.Visible = True
        ok.Visible = False
    End If
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ok.Visible = False
    okpress.Visible = False
    okoff.Visible = True
End Sub
image forms vba button
1个回答
0
投票

MouseUP 事件发生在同一窗体上的 MouseDOWN 之后,而不是鼠标所在的位置。当您隐藏表单时,您也会停止事件链。

针对您的情况的解决方案是,不隐藏该按钮,而是在其前面显示另一个按钮。因此,当您释放鼠标时,MouseUP 事件仍然会发生,结果是相同的。

-- 按以下顺序从后到前放置按钮/图像:确定(c1)、关闭(c2)、按(c3)

代码:

Private Sub c1_Click()
c1.Visible = False
c2.Visible = True
End Sub

Private Sub c1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
c3.Visible = True
End Sub

Private Sub c1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
c3.Visible = False
End Sub

Private Sub c2_Click()
c1.Visible = True
c2.Visible = False
End Sub

Private Sub c2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
c3.Visible = True
End Sub

Private Sub c2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
c3.Visible = False
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
c1.Visible = False
c3.Visible = False
c2.Visible = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.