Solved

How do I move the mouse pointer?

Posted on 2000-04-11
12
378 Views
Last Modified: 2013-11-20
In Win98 (using Microsoft C++) I want to move the sliders on another application by controlling the mouse pointer, like marco programs do.

For example:
  move to x=400, y=500, mouse buttons released
  press left mouse
  move to x=440, y=500, left mouse held down.

Ideally this should be by  API, but if that is not possible then C code would be acceptable.

Ideally I would like to use coordinates relative to a named window rather than absolute screen coordinates.

How do I do this?
 
0
Comment
Question by:PC_User321
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2706214
You'd use mouse_event()...
Pay attention to the measure size of it ...
You can use ScreenToClient() to convert ...
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2706220
And you'd also use SetCursoPos() to move the mouse ...
That's the info I can give to you...

Regards
0
 
LVL 5

Author Comment

by:PC_User321
ID: 2706296
Thanks for your reply.

I am new to Windows programming, so please bear with me.

I looked up mouse_event in MSDN, and it appears to only tell you what the human is doing to the mouse, rather than allowing you to control the mouse.  For example, from MSDN:

MOUSEEVENTF_LEFTDOWN
Specifies that the left button is down.

I don't see any parameters like
  MOVE_MOUSE  or SIMULATE_BUTTON_PRESS

If it is possible to do things like that could you explain a bit more.


SetCursorPos looks as if it could be useful, but MSDN says

If the new coordinates are not within the screen rectangle set by the most recent ClipCursor function, Windows CE automatically adjusts the coordinates so the cursor stays within the rectangle.
.... and ...
A window should move the cursor only when the cursor is in its client area.

This makes me think that I may have difficulty moving the mouse anywhere on the screen, like macro programs do.

Any thoughts on this?


Thanks.
0
 
LVL 5

Author Comment

by:PC_User321
ID: 2706942
Sorry Wyn, but you haven't responded to my comments so I will unlock this question and allow others to comment.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 70 total points
ID: 2707091
Hi PC_User321,

this code for example moves the slider 10 pixels to the right when mouse is on the slider button.

POINT pt;
GetCursorPos( &pt );
int xMax = GetSystemMetrics( SM_CXSCREEN );
int yMax = GetSystemMetrics( SM_CYSCREEN );
int x = pt.x + 10; // new x-position in pixels
int y = pt.y; // new y-position in pixels
x = x * 65536 / xMax; // x-position in mickeys
y = y * 65536 / yMax; // x-position in mickeys
      
mouse_event( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0 );

to move the mouse cursor onto the slider button you can either use mouse_event or SetCursorPos. If you use mouse_event you'll have to calculate the mickeys as above.

hope that helps,

ZOPPO
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2707206
I am afraid Wyn is right - mouse_event is the function you want. It is the MS documentation that leaves a lot to be desired.

the dwFlags parameter is an [in] parameter and can therefore only be used to set data not retrieve it.

to move the mouse use the MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE and set the dx,dy values.

to do a mouse click you would have to send a MOUSEEVENTF_LEFTDOWN followed by a MOUSEEVENTF_LEFTUP
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:PC_User321
ID: 2707216
Thanks for your reply.

You are basically saying the same as Wyn, but with more description, which I like.

You set LEFTDOWN and LEFTUP active simultaneously.  Is that going to result in the right thing, that is
   Left down, then
   Move, then
   Left up, ready to be moved to the next place?

If so, then how would I specify
   Left up, then
   Move, then
   Left down?
(I just want to get an understanding of how it works.  MSDN is helpful there.)

Will SetCursorPos and mouse_event let me go anywhere on the screen?

Thanks.
0
 
LVL 5

Author Comment

by:PC_User321
ID: 2707229
My last comment was directed to Zoppo.

Shaun, I only saw your post after I sumbitted mine.  Thinking about it.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2707240
I only saw zoppos after submitting mine :)
0
 
LVL 5

Author Comment

by:PC_User321
ID: 2707251
!! Bad mistake !!
I left out the 'un' in "MSDN is UNhelpful there"  :)

Shaun, you seem to be saying that I send separate mouse_events for
  move with button down
and
  release button.

Makes sense.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2707255
Hi PC_User321,

Well, I've tested it here and it worked, but perhaps it would be better to split it to three calls like this:

mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
mouse_event( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0 );
mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );

Both SetCursorPos and mouse_event use screen coordinates (mouse_event uses 'mickeys', SetCursorPos uses pixels) and can be used to go anywhere on screen (or better anywhere on screen or if cursor clip rectangle is set with ClipCursor() anywhere in this cursor clip rectangle)

ZOPPO
0
 
LVL 5

Author Comment

by:PC_User321
ID: 2707287
I will accept Zoppo's answer and post conolation prizes for Wyn and Shaun in the MFC section.

Thanks to you all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 55
sum13 challenge 24 90
How to convert MFC::CString to UTF8 wchar_t* 10 141
how to split multiple lines delimiter : 8 78
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now