VB如何判断鼠标指针是否指着按钮

我想自己制作按钮,用一个标签控件,鼠标指向它时背景为绿色,点击它为黄色,鼠标不指向它时为红色。(就是跟按钮一样了,只不过用代码体现)其实我最关心的是如何判断鼠标指针是否指... 我想自己制作按钮,用一个标签控件,鼠标指向它时背景为绿色,点击它为黄色,鼠标不指向它时为红色。(就是跟按钮一样了,只不过用代码体现)其实我最关心的是如何判断鼠标指针是否指着它,因为这是自制按钮的关键。谢谢! (就是按钮效果啦!用form1_mousemove是不行的) 标签放在窗口边缘,假如鼠标马上移动出了窗口范围,form1_mousemove就会无效了 展开
 我来答
hantian0739
2009-12-25 · TA获得超过194个赞
知道小有建树答主
回答量:277
采纳率:0%
帮助的人:200万
展开全部
动态判断这个按钮在显示器窗口的位置和范围,如果鼠标在这个位置范围里面,则是指向了这个按钮,变为绿色,如果不在这个范围里面,则显示红色,点击就很简单了,看vb6源码,调试通过:
'=========================================================
'说明,注意,label1控件的高度需要自己事前设置好,大概是标题栏到底部的高度,再用窗体的高度减去label1空间的高度就是指毁如标题栏的高度了,如果
'需要动态修改窗体的高度,则要自己动态修改label1的高度,timer1控件设置20ms,自己看着理解了就好修改了

Dim cmd_x As Long, cmd_y As Long, cmd_width As Long, cmd_height As Long
Dim form_x As Long, form_y As Long
Dim total_x As Long, total_y As Long
Dim mouse_x As Long, mouse_y As Long '鼠标的位置
Dim color_flage As Integer '0-不在按钮范围内,1-在按钮范围内但是没有点唯启击按钮,2-在按钮范围内点击了按钮
'============
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim p As POINTAPI

Private Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
color_flage = 2 '按下标志
End If
End Sub

Private Sub Label2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 0 Then'为0是没有点击鼠标键余稿
Me.Label2.BackColor = &HFF00& '绿色
color_flage = 1 '在按钮范围但是没有按下按钮标志
End If
End Sub

Private Sub Label2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
color_flage = 1 '按下标志
End If
End Sub

Private Sub Form_Load()
total_x = Screen.Width '显示器的分辨率
total_y = Screen.Height
'Me.Label2.Style = 1 '按钮样式,设置1才能改颜色,需要手动修改此属性
End Sub

Private Sub Timer1_Timer()
GetCursorPos p
mouse_x = p.X
mouse_y = p.Y
form_x = Int(Me.Left / 15 + 0.5) '动态调整时的位置
form_y = Int(Me.Top / 15 + 0.5)
cmd_x = Int(Me.Label2.Left / 15 + 0.5) + form_x '按钮坐标
cmd_y = Int(Me.Label2.Top / 15 + 0.5) + form_y + Int((Me.Height - Me.Label1.Height) / 15 + 0.5) '要加上标题栏的高度
cmd_width = Int(Me.Label2.Width / 15 + 0.5) '范围
cmd_height = Int(Me.Label2.Height / 15 + 0.5)
If (mouse_x < cmd_x) Or (mouse_x > (cmd_x + cmd_width)) Then 'x方向范围
color_flage = 0 '不在按钮范围标
End If
If (mouse_y < cmd_y) Or (mouse_y > (cmd_y + cmd_height)) Then 'y方向范围
color_flage = 0 '不在按钮范围标志
End If

If color_flage = 0 Then Me.Label2.BackColor = &HFF& '红色
If color_flage = 1 Then Me.Label2.BackColor = &HFF00& '绿色
If color_flage = 2 Then Me.Label2.BackColor = &HFFFF& '黄色
End Sub
a65702300
2009-12-25 · TA获得超过260个赞
知道小有建树答主
回答量:826
采纳率:0%
帮助的人:282万
展开全部
由于时间问睁腊题没仔细看题冲枯,你先运行看看,不符合散早洞要求再说

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.BackColor = vbRed
End Sub

Private Sub Label1_Click()
Label1.BackColor = vbYellow
End Sub

Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Label1.BackColor = vbYellow Then Exit Sub
Label1.BackColor = vbGreen
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Coo_boi
2009-12-25 · TA获得超过1333个赞
知道大有可为答主
回答量:2209
采纳率:50%
帮助的人:235万
展开全部
Private Sub Form_Load()
With Label1
.BackColor = vbGreen
.Left = 0
.Top = 0
End With
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.BackColor = vbGreen
End Sub

Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.BackColor = vbRed
End Sub

Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.BackColor = vbBlack
End Sub

Private Sub Label1_Click()
Label1.BackColor = vbRed
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式