CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2000
    Posts
    66

    How to know the mouse moved out of the control?

    I had a text box on a dialog and need to change the border style of it when
    the mouse moved in the text box and out of the text box. I can use Text1_MouseMove
    event to deliver the mouse move in action but found I never know when the user
    moved the mouse out of it! ( cannot write function in the Form_MouseMove because
    it's not always working fine )

    Any good ways?

    //-----------------------------------------\\
    Where there's a wire , there's a way
    \\-----------------------------------------//

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to know the mouse moved out of the control?

    I used to take advantage of those events, but if you say form_mousemove is not
    good enough, you may try with
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

    and with:
    Private Type POINTAPI
    X As Long
    Y As Long
    End Type
    and have a timer on form , and

    Private Sub Timer1_Timer()
    Dim Point As POINTAPI
    ' Get the position of the cursor
    GetCursorPos Point

    then test for
    Point.X
    and Point.Y to see if mouse is over your control.
    But it will not be easy: you will get coordinates of mouse all over the screen
    and in screen coordinates


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood,
    Bruno Paris and all the other wonderful people who made and make Codeguru
    a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Apr 2000
    Posts
    737

    Re: How to know the mouse moved out of the control?

    for control that got window handle, you can try SetCapture, whereby in the X,Y of mouse_move should become negative if you move out of a control (i haven't try it out myself). but, don't forget to call ReleaseCapture.

    for control that doesn't have window handle, you will need to check the container mouse move event handle, if the X, Y is fall within the control. if u need example for this, check out http://vblib.virtualave.net, there should be a function call EnableOnMouseOver for vbLabel.



    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: How to know the mouse moved out of the control?

    You can use the API calls CreateRectRgn and PointInRgn to get this kind of functionality - see the ImageMapVB source code at http://www.merrioncomputing.com/Download/index.htm

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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

    Re: How to know the mouse moved out of the control?

    Here is a working example. Start a new project. Add a command button. Paste this code into the general declarations section. Ru the program and move the mouse over and off the command button

    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 &lt; 0) Or (Y &lt; 0) Or (X &gt; .Width) Or (Y &gt; .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