作者:绿色风
分类:
时间:2022-08-17
浏览:129
楼主 kevinchengcw |
Q: 如何窗体判断鼠标位于标签或按钮上并改变相应的提示内容? A: 打开一个新的excel文档,按Alt + F11进入VBA编辑器,插入窗体,并在窗体上插入一个标签和一个命令按钮,双击标签或按钮,在打开的编辑框内输入如下代码:- Private Sub CommandButton1_Click() '点击命令按钮后的执行操作
- MsgBox "本例主要运用了窗体及控件所" & vbNewLine & "提供的mousemove事件来实现", vbOKOnly, "原理" '显示一个消息框
- End Sub
- Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) '命令按钮的鼠标移动事件(用于当鼠标在其上方时进行相应改变)
- With CommandButton1 '设定变化(颜色,字体和文字内容)
- .ForeColor = vbRed
- .Font.Bold = True
- .Caption = "你就不想点点看看"
- End With
- End Sub
- Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) '标签的鼠标移动事件(用于当鼠标在其上方时进行相应改变)
- With Label1 '设定标签的变化内容(颜色,字体和文字内容)
- .ForeColor = vbRed
- .Font.Bold = True
- .Caption = "怎么样,变了吧"
- End With
- End Sub
- Private Sub UserForm_Initialize() '窗体的初始化代码
- Label1.Font.Size = 16 '设定标签的字体大小和颜色
- Label1.ForeColor = vbBlue
- CommandButton1.Font.Size = 16 '设定命令按钮的字体大小和颜色
- CommandButton1.ForeColor = vbBlue
- End Sub
- Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) '窗体的鼠标移动事件(用于将改变的文字更改回原样)
- If X < Label1.Left Or X > Label1.Left + Label1.Width Or Y < Label1.Top Or Y > Label1.Top + Label1.Height Then '判断鼠标是否在标签上方
- With Label1
- .ForeColor = vbBlue
- .Font.Bold = False
- .Caption = "鼠标移上来,字会变噢"
- End With
- End If
- If X < CommandButton1.Left Or X > CommandButton1.Left + CommandButton1.Width Or Y < CommandButton1.Top Or Y > CommandButton1.Top + CommandButton1.Height Then '判断鼠标是否在按钮上方
- With CommandButton1
- .ForeColor = vbBlue
- .Font.Bold = False
- .Caption = "鼠标移上来,字会变噢"
- End With
- End If
- End Sub
附示例文件。 窗体学习--变换标签及按钮提示内容.rar |
2楼 wnianzhong |
向你学习 |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一