VB如何使Command的BackColor改变
在Command控件上写什么代码,使Command当鼠标移动到这个控件上时,它的BackColor为绿色,当鼠标离开这个控件时,它的BackColor恢复原来的颜色?(注...
在Command控件上写什么代码,使Command当鼠标移动到这个控件上时,它的BackColor为绿色,当鼠标离开这个控件时,它的BackColor恢复原来的颜色?
(注:1.只在Command这个一个控件上写代码完成上述操作) 展开
(注:1.只在Command这个一个控件上写代码完成上述操作) 展开
2个回答
展开全部
楼上的方法很好,但有些不足,没说清楚前提下要在设计时将Style属性设置为VbButtonGraphical,并且与楼主的要求不一样,不能恢复原来Command的颜色,根据楼上的方法修改下就可以了:
必须在设计时设置Command1.Style=VbButtonGraphical(Style只能在设计时赋值,运行后是只读的)
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
Command1.Tag = CStr(Command1.BackColor)
End Sub
Private Sub Command1_Click()
Command1.BackColor = RGB(0, 255, 0)
...... '其它代码
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Left(Command1.Tag, 2) = "In" Then
If X < 0 Or Y < 0 Or X > Command1.Width Or Y > Command1.Height Then
ReleaseCapture
Command1.BackColor = Mid(Command1.Tag, 3)
Command1.Tag = Mid(Command1.Tag, 3)
Else
SetCapture Command1.hwnd
Command1.BackColor = RGB(0, 255, 0)
End If
Else
Command1.Tag = "In" & CStr(Command1.Tag)
SetCapture Command1.hwnd
Command1.BackColor = RGB(0, 255, 0)
End If
End Sub
必须在设计时设置Command1.Style=VbButtonGraphical(Style只能在设计时赋值,运行后是只读的)
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
Command1.Tag = CStr(Command1.BackColor)
End Sub
Private Sub Command1_Click()
Command1.BackColor = RGB(0, 255, 0)
...... '其它代码
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Left(Command1.Tag, 2) = "In" Then
If X < 0 Or Y < 0 Or X > Command1.Width Or Y > Command1.Height Then
ReleaseCapture
Command1.BackColor = Mid(Command1.Tag, 3)
Command1.Tag = Mid(Command1.Tag, 3)
Else
SetCapture Command1.hwnd
Command1.BackColor = RGB(0, 255, 0)
End If
Else
Command1.Tag = "In" & CStr(Command1.Tag)
SetCapture Command1.hwnd
Command1.BackColor = RGB(0, 255, 0)
End If
End Sub
展开全部
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
Command1.Tag = ""
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Command1.Tag = "In" Then
If X < 0 Or Y < 0 Or X > Command1.Width Or Y > Command1.Height Then
Command1.Tag = ""
ReleaseCapture
Command1.BackColor = Form1.BackColor
End If
Else
Command1.Tag = "In"
SetCapture Command1.hwnd
Command1.BackColor = QBColor(2)
End If
end sub
改进了。这个比较好
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
Command1.Tag = ""
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Command1.Tag = "In" Then
If X < 0 Or Y < 0 Or X > Command1.Width Or Y > Command1.Height Then
Command1.Tag = ""
ReleaseCapture
Command1.BackColor = Form1.BackColor
End If
Else
Command1.Tag = "In"
SetCapture Command1.hwnd
Command1.BackColor = QBColor(2)
End If
end sub
改进了。这个比较好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询