Results 1 to 11 of 11

Thread: [2005] Reset Mouse Pointer Location On Hover

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    [2005] Reset Mouse Pointer Location On Hover

    At runtime, if a user hovers his mouse over a specific form, i want to reset the pointer location somewhere else. Any suggestions on best way to implement this?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] Reset Mouse Pointer Location On Hover

    you can do it using an API function

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private r1 As New Rectangle(10, 10, 100, 100)
    4.  
    5.     Private Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Integer
    6.  
    7.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    8.         Dim pt As New Point(e.Location.X, e.Location.Y)
    9.         If r1.Contains(pt) Then
    10.             SetCursorPos(Me.Left + e.Location.X + 100, Me.Top + e.Location.Y + 100)
    11.         End If
    12.     End Sub
    13.  
    14. end class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reset Mouse Pointer Location On Hover

    vb.net Code:
    1. Private Sub Form1_MouseHover(ByVal sender As Object, _
    2.                              ByVal e As EventArgs) Handles Me.MouseHover
    3.     'Move the cursor to the top left corner of
    4.     'the screen when it hovers over the form.
    5.     Cursor.Position = Point.Empty
    6. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [2005] Reset Mouse Pointer Location On Hover

    Quote Originally Posted by jmcilhinney
    vb.net Code:
    1. Private Sub Form1_MouseHover(ByVal sender As Object, _
    2.                              ByVal e As EventArgs) Handles Me.MouseHover
    3.     'Move the cursor to the top left corner of
    4.     'the screen when it hovers over the form.
    5.     Cursor.Position = Point.Empty
    6. End Sub

    For some reason it is not working as anticipated. Sometimes it moves, but other times it lets me hover and even trigger the click event. I'm actually trying to prevent the click event on the specific form from occurring.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reset Mouse Pointer Location On Hover

    The form will not raise a MouseHover event if you hover over a child control. Also, the MouseHover event is not raised until a hover is detected, which means that the cursor will have to have been stationary for a small period of time. That's what hovering means.

    I don't quite understand all this anyway. You don't have to hover over a control to click it. You can click without the mouse cursor being stationery. Why would you want to prevent a Click event anyway? Can you please provide a proper description of what you're trying to accomplish?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [2005] Reset Mouse Pointer Location On Hover

    Quote Originally Posted by jmcilhinney
    The form will not raise a MouseHover event if you hover over a child control. Also, the MouseHover event is not raised until a hover is detected, which means that the cursor will have to have been stationary for a small period of time. That's what hovering means.

    I don't quite understand all this anyway. You don't have to hover over a control to click it. You can click without the mouse cursor being stationery. Why would you want to prevent a Click event anyway? Can you please provide a proper description of what you're trying to accomplish?
    You tried to help me with a bug in my program. I use two monitors. I have a form on the 2nd monitor with a TLP. The TLP refreshes data by use of a timer. For some reason, when clicked the TLP shifts. I tried your suggestion of handling the locationchanged event of the tlp but it did not help me locate the error.

    I am guessing the bug is in how .Net is handling the display calculations relating to the 2 monitors. So rather than drive myself crazy trying to figure out that bug, I figured if I could prevent the user from clicking on that form, then I would avoid the bug. I know its not the best way to solve a problem but I figured why not give it a shot.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reset Mouse Pointer Location On Hover

    You'd have to handle the MouseHover event of the TLP rather than the form, or as well as the form, if you want to detect when the user hovers over the TLP.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [2005] Reset Mouse Pointer Location On Hover

    Quote Originally Posted by jmcilhinney
    vb.net Code:
    1. Private Sub Form1_MouseHover(ByVal sender As Object, _
    2.                              ByVal e As EventArgs) Handles Me.MouseHover
    3.     'Move the cursor to the top left corner of
    4.     'the screen when it hovers over the form.
    5.     Cursor.Position = Point.Empty
    6. End Sub
    Does .empty always point to (0,0) on the screen? If I am using 2 montiors, will that position always be on monitor 1 ?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reset Mouse Pointer Location On Hover

    When you have two monitors your desktop bounds is one big Rectangle spread across the two. Point.Empty is equivalent to New Point(0, 0). This will always be the top, left corner of the desktop bounds. With multiple monitors that will generally be the top, left corner of the primary monitor, but not necessarily. If you have two monitors at different resolutions then part of the desktop bounds will not be visible. Depending on your arrangement (0, 0) may not be visible.

    With multiple monitors you may want to use the Screen class to determine the appropriate location. For instance, Screen.PrimaryScreen.WorkingArea.Location will always give you the top, left corner of the desktop on the primary monitor, regardless of monitor resolutions, arrangement and it also takes into account the possibility of the Windows Taskbar beoing at the top of the screen.
    Last edited by jmcilhinney; Jan 11th, 2008 at 08:52 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] Reset Mouse Pointer Location On Hover

    Quote Originally Posted by jmcilhinney
    This will always be the top right corner of the desktop bounds.
    don't you mean top left?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reset Mouse Pointer Location On Hover

    Quote Originally Posted by .paul.
    don't you mean top left?
    Yeah, I do. I put top left in a couple of other places so I'm not quite sure why I made that mistake.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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