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?
Printable View
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?
you can do it using an API function
vb Code:
Public Class Form1 Private r1 As New Rectangle(10, 10, 100, 100) Private Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Integer Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove Dim pt As New Point(e.Location.X, e.Location.Y) If r1.Contains(pt) Then SetCursorPos(Me.Left + e.Location.X + 100, Me.Top + e.Location.Y + 100) End If End Sub end class
vb.net Code:
Private Sub Form1_MouseHover(ByVal sender As Object, _ ByVal e As EventArgs) Handles Me.MouseHover 'Move the cursor to the top left corner of 'the screen when it hovers over the form. Cursor.Position = Point.Empty End Sub
Quote:
Originally Posted by jmcilhinney
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.
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.Quote:
Originally Posted by jmcilhinney
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.
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.
Does .empty always point to (0,0) on the screen? If I am using 2 montiors, will that position always be on monitor 1 ?Quote:
Originally Posted by jmcilhinney
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.
don't you mean top left?Quote:
Originally Posted by jmcilhinney
Yeah, I do. I put top left in a couple of other places so I'm not quite sure why I made that mistake.Quote:
Originally Posted by .paul.