Results 1 to 10 of 10

need help with mousemove event

This is a discussion on need help with mousemove event within the Excel Questions forums, part of the Question Forums category; I am using the mousemove event to change the forecolor of a command button when the pointer is moved over ...

  1. #1
    Board Regular
    Join Date
    Jan 2003
    Posts
    76

    Default

    I am using the mousemove event to change the forecolor of a command button when the pointer is moved over the button. How do chage the color back when the pointer is moved off of the button and onto the worksheet? The worksheet does not have a mousemove event.

  2. #2
    MrExcel MVP tusharm's Avatar
    Join Date
    May 2002
    Posts
    10,530

    Default

    The only way to guarantee detection of a mouse leaving a control is with a 'mouseout' event -- something that no XL control supports. There might be some API calls that might help; but I don't know of any.

    None of the ways that come to mind can guarantee detection of a mouse leaving a control. You could try:

    (1) if the x or y coordinate is at (or close to) an edge, reset the control's format

    (2) embed the real control inside a fake control. The fake control's mousemove event resets the format of the real control.

    However, rapid mouse movement will not be detected. In my tests, I could fake XL out with either of the above techniques.

  3. #3
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    4,806

    Default

    Hi tphillips,

    I have come up with a way round your problem.

    Let's assume the commandbutton you have is CommandButton1.
    Now,create another commandbutton (CommanButton2)and resize it so that its size is bigger than CommandButton1.

    Move CommandButton1 and place it on top of CommandButton2.Right click CommandButton1 and choose Order¦Bring Forward.

    The result you have is CommandButton1 is now located Within CommandButton2.

    Now,you can write a MouseMove event for CommandButton2 to restore the Black color of CommandButton1.This is because when you move the mouse away from CommandButton1,the mouse first moves over CommandButton2 before moving over the Worksheet as the former is within the latter.

    Below are the Codes for the 2 CommandButtons MouseMove Events:

    Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    'Change the color
    CommandButton1.ForeColor = vbRed
    End Sub


    Private Sub CommandButton2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    'Restore the color
    CommandButton1.ForeColor = vbBlack
    End Sub

    Change the Fore Colors to suit your needs.

    Let me know if any problems.

    Jaafar.


  4. #4
    MrExcel MVP tusharm's Avatar
    Join Date
    May 2002
    Posts
    10,530

    Default

    On 2003-01-29 22:47, rafaaj2000 wrote:
    {snip}
    Let me know if any problems.

    Jaafar.

    What you suggested is #2 in my list. However, you can fake it out by moving the mouse so quickly from inside the inner button to outside of the outer button that XL triggers no mousemove events for either button!


  5. #5
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    4,806

    Default

    Hi Tusharm,

    Sorry,I didn't actually read your poste properly.My solution was indeed already Given in your poste.

    However,Tusharm,You said if the mouse is moved very quickly,no MouseMove event is detected.I have tested this technic and it does work perfectly well and the event is fired everytime even when moving the mouse very quicly.

    Regards.

    Jaafar.

  6. #6
    Board Regular
    Join Date
    Jan 2003
    Posts
    76

    Default

    I tried using a several different controls under the command button, but they only work if the mouse is moved slowly. As you said, a fast mouse movement was not detected. I tried using a large image control behind the button, but the image control becomes visible when clicked on. Can you tell me how to write code that will not allow an object to be clicked on, and will leave the cells under it usable. Thanks

  7. #7
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default

    Here is another method which seems to work well. This is set up for two controls but could easily be edited to account for more.
    In any standard module

    Option Explicit

    Private Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long

    Dim z As POINTAPI ' Declare variable
    Public blnInControl1_ExitMouse As Integer
    Public blnInControl2_ExitMouse As Integer

    Type POINTAPI
    X As Long
    Y As Long
    End Type

    Type CursorMoveThreshHold
    Left As Long
    Right As Long
    Top As Long
    Bottom As Long
    End Type

    Sub GetCursor(intWidth As Long, intHeight As Long, _
    intPosX As Single, intPosY As Single, ctl As OLEObject)
    Dim cmt As CursorMoveThreshHold
    GetCursorPos z
    cmt.Top = z.Y - (1.35 * intPosY)
    cmt.Bottom = z.Y + (1.35 * (intHeight - intPosY))
    cmt.Left = z.X - (1.35 * intPosX)
    cmt.Right = z.X + (1.35 * (intWidth - intPosX))
    Do
    GetCursorPos z
    DoEvents
    If z.Y < cmt.Top _
    Or z.Y > cmt.Bottom _
    Or z.X < cmt.Left _
    Or z.X > cmt.Right Then
    blnInControl1_ExitMouse = 0
    blnInControl2_ExitMouse = 0
    ctl.Object.BackColor = &H80000000
    Exit Sub
    End If
    Loop
    End Sub

    place this in the worksheet which contains your controls

    Private Sub CommandButton1_MouseMove(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    If blnInControl1_ExitMouse <> 0 Then
    Exit Sub
    Else
    blnInControl1_ExitMouse = 1
    With CommandButton1
    .BackColor = &H80000001
    Call GetCursor(.Width, .Height, X, Y, Me.OLEObjects("CommandButton1"))
    End With
    End If

    End Sub

    Private Sub CommandButton2_MouseMove(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    If blnInControl2_ExitMouse <> 0 Then
    Exit Sub
    Else
    blnInControl2_ExitMouse = 1
    With CommandButton2
    .BackColor = &H80000001
    Call GetCursor(.Width, .Height, X, Y, Me.OLEObjects("CommandButton2"))
    End With
    End If

    End Sub


    This method is more accurate than the Control on the Control method which has caused me problems frequently in worksheets. To test, simply draw out two commandbuttons named CommandButton1,2.

    Tom


    _________________
    Using Office 8, 9, & 10 on Windows Millenium

    [ This Message was edited by: TsTom on 2003-01-30 04:47 ]

  8. #8
    Board Regular
    Join Date
    Nov 2002
    Posts
    83

    Default

    You can also use the UserForm_MouseMove event to reset the format, but it suffers from high speed movement syndrome



    [ This Message was edited by: Expel on 2003-01-30 04:48 ]

  9. #9
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default

    We're dealing with a worksheet here, I think. Just like Tuscharm said, there is no fool proof way using standard events. The method I gave can be tweaked to "sqeeze" the frame of the control if the controls are located very close to one another. Also, you can change the properties of the activecontrol using events in the surrounding controls.

  10. #10
    Board Regular
    Join Date
    Nov 2002
    Posts
    83

    Default

    Oops. Didn't read the post properly.

    The method employed by the mousepointer property would be ideal.... whatever that might be.


Bookmarks

Posting Permissions

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


DMCA.com