Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mouse move simulation

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
Thanks for the reply of my last question.

I need to do a mouse move simulation: the mouse doesn't physically move but on screen the mouse cursor is moving, like some demoes are doing.

My thinking is to get the current mouse cursor position and increment it to the right end of the screen and loop back again.(don't know if this is the right way to implement it). The problem is I don't know how to get the mouse cursor position since I think it will involve the mouseclick event. On the other hand, if i need mouseclick to do that, that means I need to physically touch the mouse, then the program is nonsense.

Any idea or samples on how to do that are appreciated.
 
Hi

I assume your are using a dialog box ( for simplicity)
Also you need two variables, named here m_cx and m_cy, that contains teh size of the window as get in the function OnSize
Also a variable called m_nIncr that store how the mouse is moving. Here is does only a simple flipflop. This must be improved according to required behaviour ...

Add a button labelled 'Start' and a handler for this button as this:
void CDlg::OnStart()
{
m_nIncr = 1;
// Set Initial Position a top left of dialogbox
CPoint pt( 0, 0);
ClientToScreen( &pt);
SetCursorPos( pt.x, pt.y);

// Start a Timer with a 25 msec periodicity
SetTimer( 100, 25, NULL);
}

Add a Timer ticks handler as this;

void CTestrichedDlg::OnTimer(UINT nIDEvent)
{
CPoint pt;
// Get Current Mouse Pos
// ( required if user moves the mouse !!!)
GetCursorPos( &pt);
ScreenToClient( &pt);

// Increment Position
pt.x = pt.x + m_nIncr;
pt.y = pt.y + m_nIncr;

// Check if mouse is getting out of window
// if so, inverse increment
// this part could be made more interesting
if (( pt.x == m_cx) || (pt.x == 0))
m_nIncr = -m_nIncr;
if (( pt.y == m_cy) || (pt.y == 0))
m_nIncr = -m_nIncr;

// Do not move mouse if outside window
if (( pt.x < m_cx) && (pt.x > 0) &&
(pt.y < m_cy) && (pt.y > 0))
{
ClientToScreen( &pt);
SetCursorPos( pt.x, pt.y);
}
}

HTH

Thierry
EMail: Thierry.Marneffe@swing.be
 
Thanks Thierry's quick reply! It helped to clarify things.

I followed your hints to make this but the timer looks like doesn't work. I don't know why.

What I did is to use the &quot;scribble.c&quot; in MSDN as a template and then make change to a buttondown function as a trigger to the timer, see below:

void CScribbleView::OnLButtonDown(UINT, CPoint point)
{
m_pStrokeCur = GetDocument()->NewStroke();
// Add first point to the new stroke
m_pStrokeCur->m_pointArray.Add(point);
SetCapture();// Capture the mouse until button up.
CPoint pt(0,0);
ClientToScreen(&pt);
SetCursorPos(pt.x,pt.y);
SetTimer( 100, 25, NULL);
return;
}

void CWnd::OnTimer(UINT nIDEvent)
{
int m_nIncr=1;
int m_cx=100,m_cy=100;
CPoint pt;
GetCursorPos( &pt);
ScreenToClient( &pt);
// Increment Position
pt.x = pt.x + m_nIncr;
pt.y = pt.y + m_nIncr;
// delete other boundary checking...
}

I can make it compile though an error showing that OnTimer is not a CView function by modifying Project->Setting.

The program works but everytime I clicked left mouse button the cursor goes back to left upper corner and doesn't move until I moved the mouse.

Any thing wrong with this? why the timer didn't work?

Thanks again.
 
I have made this one work without MFC. Thanks your help.

The other relative trivial problem is GetCursorPos() and SetCursorPos() are coarse. I don't what parameter can make the cursor movement more smooth.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top