Results 1 to 3 of 3

Thread: >Important Issue: GUI Questions<

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    99

    Exclamation

    How could you make something change color when the mouse is over it without having to set the MouseMove of everything else on the form to the non-hover color?
    ___________________________
    Chris

  2. #2
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    There is a way without having to use MouseMove, but it is quite complex.

    Code:
    'BAS Module:
    Option Explicit
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const GWL_WNDPROC = -4
    
    Private Const WM_SETCURSOR = &H20
    Private Const WM_MOUSEMOVE = &H200
    
    Private saveHWnd As Long        ' The handle of the subclassed window.
    Private oldProcAddr As Long     ' The address of the original window procedure
    Private ChangehWnd As Long
    
    Sub StartSubclassing(ByVal hWnd As Long, hWndChange As Long)
        ChangehWnd = hWndChange
        saveHWnd = hWnd
        oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
    End Sub
    
    Sub StopSubclassing()
        SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
    End Sub
    
    Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        
        WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
        
        Select Case uMsg
        
        Case WM_SETCURSOR
            Dim ctrlHWnd As Long
            Dim mouseAction As Long
            Dim ctrl As Control
            
            ' wParam holds the handle of the control under the cursor
            ctrlHWnd = wParam
            ' code for mouse action is in high word of lParam
            mouseAction = (lParam \ &H10000)
            
            If mouseAction = WM_MOUSEMOVE Then
                If wParam = ChangehWnd Then
                    Form1.BackColor = vbYellow
                Else: Form1.BackColor = vbBlue
                End If
            End If
                
        End Select
    
    End Function
    Code:
    Form called Form1, with a textbox called Text1:
    Private Sub Form_Load()
        StartSubclassing Me.hWnd, Text1.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopSubclassing
    End Sub
    I hope this is near to what you need.

    Me
    Courgettes.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    As I understand your question you want to change color of a control when the mouse hover over the control and then change it back when the mouse leaves.
    And you don't want to check in the Mouse_Move event of all other controls on the form if the mouse have left the control you changed color on.
    If this is a correct interpretation of your question then you don't actually have to sub-class the form but rather use the SetCaption API function.

    The SetCaption takes the handle of the window (or control) you want to use so you can't use this code on a Label, an Image, a Shape or a Line control.

    This sample code uses a PictureBox called Picture1. To try it out start a new Standard EXE project and add a PictureBox and paste the following code in the form module.
    Code:
    Private Declare Function SetCapture _
     Lib "user32" ( _
     ByVal hwnd As Long) As Long
    
    Private Declare Function ReleaseCapture _
     Lib "user32" () As Long
    
    Private blnHasCapture As Boolean
    
    Private Sub Form_Load()
        Picture1.BackColor = vbRed
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If blnHasCapture = False Then
            SetCapture Picture1.hwnd
            blnHasCapture = True
        End If
        If (X < 0 Or X > Picture1.ScaleWidth) Or (Y < 0 Or Y > Picture1.ScaleHeight) Then
            'Elvis has left the building
            ReleaseCapture
            blnHasCapture = False
            Picture1.BackColor = vbRed
        Else
            Picture1.BackColor = vbGreen
        End If
    End Sub
    
    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        'When a mouse button is pressed VB
        'automatically calles ReleaseCapture
        'we don't want that
        If blnHasCapture = True Then
            SetCapture Picture1.hwnd
        End If
    End Sub
    Good luck!

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