ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何窗体判断鼠标位于标签或按钮上并改变相应的提示内容?

如何窗体判断鼠标位于标签或按钮上并改变相应的提示内容?

作者:绿色风 分类: 时间:2022-08-17 浏览:84
楼主
kevinchengcw
Q: 如何窗体判断鼠标位于标签或按钮上并改变相应的提示内容?
A: 打开一个新的excel文档,按Alt + F11进入VBA编辑器,插入窗体,并在窗体上插入一个标签和一个命令按钮,双击标签或按钮,在打开的编辑框内输入如下代码:
  1. Private Sub CommandButton1_Click()   '点击命令按钮后的执行操作
  2. MsgBox "本例主要运用了窗体及控件所" & vbNewLine & "提供的mousemove事件来实现", vbOKOnly, "原理"    '显示一个消息框
  3. End Sub

  4. Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)   '命令按钮的鼠标移动事件(用于当鼠标在其上方时进行相应改变)
  5. With CommandButton1   '设定变化(颜色,字体和文字内容)
  6. .ForeColor = vbRed
  7. .Font.Bold = True
  8. .Caption = "你就不想点点看看"
  9. End With
  10. End Sub

  11. Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)    '标签的鼠标移动事件(用于当鼠标在其上方时进行相应改变)
  12. With Label1   '设定标签的变化内容(颜色,字体和文字内容)
  13. .ForeColor = vbRed
  14. .Font.Bold = True
  15. .Caption = "怎么样,变了吧"
  16. End With
  17. End Sub

  18. Private Sub UserForm_Initialize()   '窗体的初始化代码
  19. Label1.Font.Size = 16   '设定标签的字体大小和颜色
  20. Label1.ForeColor = vbBlue
  21. CommandButton1.Font.Size = 16   '设定命令按钮的字体大小和颜色
  22. CommandButton1.ForeColor = vbBlue
  23. End Sub

  24. Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)   '窗体的鼠标移动事件(用于将改变的文字更改回原样)
  25. If X < Label1.Left Or X > Label1.Left + Label1.Width Or Y < Label1.Top Or Y > Label1.Top + Label1.Height Then   '判断鼠标是否在标签上方
  26. With Label1
  27. .ForeColor = vbBlue
  28. .Font.Bold = False
  29. .Caption = "鼠标移上来,字会变噢"
  30. End With
  31. End If
  32. If X < CommandButton1.Left Or X > CommandButton1.Left + CommandButton1.Width Or Y < CommandButton1.Top Or Y > CommandButton1.Top + CommandButton1.Height Then    '判断鼠标是否在按钮上方
  33. With CommandButton1
  34. .ForeColor = vbBlue
  35. .Font.Bold = False
  36. .Caption = "鼠标移上来,字会变噢"
  37. End With
  38. End If
  39. End Sub
附示例文件。
窗体学习--变换标签及按钮提示内容.rar
2楼
wnianzhong
向你学习

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap