CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2001
    Location
    Newcastle Ontario, Canada
    Posts
    118

    MouseMove ColorChange

    I have the mousemove event of my control set to change the color of a button residing inside it, so I know when the mouse is over it(the button is resized to fit the control in the Resize Event btw).
    But im not sure howto have the color of the button change back when the mouse isnt over it. As I understand it when the mouse leaves the control the form starts using it's mousemove event (not the control). And I want it to be independant.

    Please Help,
    Steve


  2. #2
    tdeltax Guest

    Re: MouseMove ColorChange

    i guess u cannot use any event but u can try and fool the user using the following code
    i hope this suffices ur need

    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.Caption = "Xpos = " & X & " Ypos = " & Y
    Command1.BackColor = vbGreen
    If X > Command1.Width - 50 Or Y > Command1.Width - 50 Or X < 50 Or Y < 50 Then
    Command1.BackColor = vbBlue
    End If
    End Sub




  3. #3
    Join Date
    Oct 2001
    Posts
    55

    Re: MouseMove ColorChange

    i guess u cannot use any event but u can try and fool the user using the following code
    i hope this suffices ur need

    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.Caption = "Xpos = " & X & " Ypos = " & Y
    Command1.BackColor = vbGreen
    If X > Command1.Width - 50 Or Y > Command1.Width - 50 Or X < 50 Or Y < 50 Then
    Command1.BackColor = vbBlue
    End If
    End Sub




  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: MouseMove ColorChange

    When the mouse moves over the button, use the GetCapture function to see
    what window has the mouse captured. If it is not the button, use
    SetCapture to capture mouse events.
    When the MouseMove event occurs and the cursor has left the button's area,
    use ReleaseCapture to release the capture.



    'on the form array of buttons and array of Chekboxes

    Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long

    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function GetCapture Lib "user32" () As Long

    Private Sub Check1_Click(Index As Integer)

    'stop check control being indented
    Check1(Index).Value = 0
    End Sub

    Private Sub Check1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If (X < 0) Or (Y < 0) Or (X > Check1(Index).Width) Or (Y > Check1(Index).Height) Then
    ' ' the MOUSELEAVE pseudo-event
    ReleaseCapture
    ' ' in this example revert the caption to normal
    Check1(Index).Font.Bold = False
    Check1(Index).ForeColor = vbBlack
    '
    ElseIf GetCapture() <> Check1(Index).hwnd Then
    ' ' the MOUSEENTER pseudo-event
    SetCapture Check1(Index).hwnd

    Check1(Index).Font.Bold = True
    Check1(Index).ForeColor = vbRed
    End If
    End Sub

    Private Sub Command1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

    If (X < 0) Or (Y < 0) Or (X > Command1(Index).Width) Or (Y > Command1(Index).Height) Then
    ' ' the MOUSELEAVE pseudo-event
    ReleaseCapture
    ' ' in this example revert the caption to normal

    Command1(Index).Font.Bold = False
    '
    ElseIf GetCapture() <> Command1(Index).hwnd Then
    ' ' the MOUSEENTER pseudo-event
    SetCapture Command1(Index).hwnd

    Command1(Index).Font.Bold = True
    End If
    End Sub



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: MouseMove ColorChange

    Another way of doing it to check if mouse over the control or over the form

    Control array of 3 textboxes 0-3
    When mouse is over Text1(index) - the text is red
    When mouse leaves text box the color is black

    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As Integer
    For i = 0 To 2
    Text1(i).ForeColor = vbBlack
    Next i
    End Sub

    Private Sub Text1_Click(Index As Integer)
    MsgBox Text1(Index)
    End Sub

    Private Sub Text1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text1(Index).ForeColor = vbRed
    End Sub


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: MouseMove ColorChange

    Here is a simple example of MouseOver not Over
    Start a new project. Add a command button to the form. Paste this code into the general declarations section of the form. Run it. Move the mouse over then not over the command button. This code can actually be used with any control that has a MouseMove event

    option Explicit

    private Declare Function SetCapture Lib "user32" (byval hwnd as Long) as Long
    private Declare Function ReleaseCapture Lib "user32" () as Long


    'Code:
    'Put this code in MouseMove event. In this example, I put a
    'CommandButton on a
    'form with the name Command1

    private Sub Command1_MouseMove(Button as Integer, Shift as Integer, X _
    as Single, Y as Single)
    static CtrMov as Boolean
    static Counter as Long
    Counter = Counter + 1
    With Command1 'Change this 'Command1' to your control name
    If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) then
    ReleaseCapture
    CtrMov = false
    Command1.BackColor = &HFF&
    Command1.Caption = " Not Over " & Counter
    Command1.Refresh
    'Put here your code to LostMouseFocus
    'for example:
    me.print "LostMouseFocus"

    else
    SetCapture .hwnd
    If CtrMov = false then ' do this only once per "over"
    CtrMov = true
    Command1.BackColor = &HFFFFFF
    Command1.Caption = " Over " & Counter
    Command1.Refresh
    'Put here your code to GetMouseFocus
    'for example:
    me.print "GetMouseFocus"

    End If
    End If
    End With
    End Sub






    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured