Open some dialog boxes and the mouse cursor moves to one of the dialog buttons

D

**Developer**

Open some dialog boxes and the mouse cursor moves to one of the dialog
buttons.
I'd like to implement that .
Is that simply something I have to program, i.e. figure out where I want the
cursor and move it.
Or does DotNet help somehow?

Thanks
 
G

Guest

Here you go.
Create a form with a button on it, and then paste in the following code:

Public Declare Function SetCursorPos Lib "user32" (ByVal X As Integer,
ByVal Y As Integer) As Long
Public Declare Function ClientToScreen Lib "user32" (ByVal hWnd As
IntPtr, ByRef point As POINT) As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New POINT
p.x = Button1.Left + (Button1.Width / 2)
p.y = Button1.Top + (Button1.Height / 2)
ClientToScreen(Me.Handle, p)
SetCursorPos(p.x, p.y)
End Sub

- Scott Swigart

P.S. Moving the mouse for the user is generally considered a no-no.
 
G

Guest

In Windows XP, you can set the options for the mouse to jump to the default
button when a form is opened I think.
 
J

Jay B. Harlow [MVP - Outlook]

**Developer**,
First I would like to strongly suggest you don't implement it. As a number
of users find it *rude* for a program to move their mouse for them.


Here is a version of Scott's code that does not use P/Invoke (Declare
statements).

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New Point
p.X = Button1.Left + (Button1.Width \ 2)
p.Y = Button1.Top + (Button1.Height \ 2)
Cursor.Position = Me.PointToScreen(p)
End Sub

Both Win32 APIs are already exposed in the Framework, no need to use
Declare...

Hope this helps
Jay

| Open some dialog boxes and the mouse cursor moves to one of the dialog
| buttons.
| I'd like to implement that .
| Is that simply something I have to program, i.e. figure out where I want
the
| cursor and move it.
| Or does DotNet help somehow?
|
| Thanks
|
|
 
L

Larry Lard

**Developer** said:
Open some dialog boxes and the mouse cursor moves to one of the dialog
buttons.
I'd like to implement that .
Is that simply something I have to program, i.e. figure out where I want the
cursor and move it.
Or does DotNet help somehow?

As others have said, you shoudl carefully consider if you really want
your application to dictate this behaviour.

Since Windows 2000 (I believe), users have had the ability to set this
behaviour on a *global* level (eg in XP it is in Control Panel | Mouse
| Pointer Options | Snap To), and by implication if they have *not* set
this option, it means that they *do not* want this behaviour. For one
application to behave in this way regardless of the global setting
would I think be seen as extremely unfriendly.
 
D

**Developer**

Thanks to all.

As to the suggestions against it I note that MS does it often.
For example, Open Notepad/File/Open
If there is a way for the user to turn it on/off I'd sure like to know how
to read his setting.
How can I check the user settings?

At first I didn't like it but then got used to it.

Something else I don't like is sometimes I want to delete one character or
move one character left and the insertion point moves to the start of the
line. I can't remember exactly what it is but I know when it has happened I
did not like it. Wish I could shut that feature off.


Thanks again
 
D

**Developer**

Larry is correct in XP it can be set in controlpanel.
Then it doesn't have to be implemented.
Simply declare the default button and the rest is automatic.
I didn't try but I'd guess it also works with MessageBox if the default
button is specified.
 
J

Jay B. Harlow [MVP - Outlook]

Larry,
Thanks for the reminder: XP Pro has a similar option available as does
Windows Server 2003.

Hope this helps
Jay


|
| **Developer** wrote:
| > Open some dialog boxes and the mouse cursor moves to one of the dialog
| > buttons.
| > I'd like to implement that .
| > Is that simply something I have to program, i.e. figure out where I want
the
| > cursor and move it.
| > Or does DotNet help somehow?
|
| As others have said, you shoudl carefully consider if you really want
| your application to dictate this behaviour.
|
| Since Windows 2000 (I believe), users have had the ability to set this
| behaviour on a *global* level (eg in XP it is in Control Panel | Mouse
|| Pointer Options | Snap To), and by implication if they have *not* set
| this option, it means that they *do not* want this behaviour. For one
| application to behave in this way regardless of the global setting
| would I think be seen as extremely unfriendly.
|
| --
| Larry Lard
| Replies to group please
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top