Click here to Skip to main content
11,629,535 members (64,866 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have this code for change mouse position but i want some thing else if user move mouse 10 cm the mouse move 1 cm on the screen so i know i have to change resolution of screen and change mouse speed ... Frown | :( plz help



}
       public Int32 x = 0;
       public Int32 y = 0;
       public Int32 prevx = 0;
       public Int32 prevy = 0;
       private void Form1_Load(object sender, EventArgs e)
       {
           timer1.Interval = 1;
           timer1.Enabled = true;
           x = Cursor.Position.X;
           y = Cursor.Position.Y;
           prevx = Cursor.Position.X;
           prevy = Cursor.Position.Y;
 
       }
 
       private void timer1_Tick(object sender, EventArgs e)
       {
           x = Cursor.Position.X;
           y = Cursor.Position.Y;
           textBox1.Text = x.ToString();
           textBox2.Text = y.ToString();
           if (x > prevx)
           {
 
               if (x -prevx > 10)
               {
                   SetCursorPos(x - 9, y);
                   prevx = x - 9;
               }
           }
           else if (x < prevx)
           {
               if (prevx-x > 10)
               {
                   SetCursorPos(x + 9, y);
                   prevx = x - 9;
               }
 
           }
           ///////////////////////////////////
           if (y> prevy)
           {
               if (y -prevy > 10)
               {
                   SetCursorPos(x ,y-9);
                   prevy = y - 9;
               }
           }
           else if (y < prevy)
           {
               if (prevy-y > 10)
               {
                   SetCursorPos(x, y+9);
                   prevy = y - 9;
               }
 
           }
 
           //////////////////////////////////
       }
   }
Posted 7-Jan-13 4:37am
Edited 7-Jan-13 7:50am
v3
Comments
Sergey Alexandrovich Kryukov at 7-Jan-13 10:58am
   
No, you don't need P/Invoke at all. It's all in your UI library, look properly.
But which one are you using? You should always tag it.
—SA
CHill60 at 7-Jan-13 11:05am
   
The code posted in your question works, so what's the real problem?
farham_heidari at 7-Jan-13 14:01pm
   
ok tnx for advice
:)
Sergey Alexandrovich Kryukov at 7-Jan-13 14:49pm
   
Hope you will accept the answer formally (green button) — thanks.
—SA
Sergey Alexandrovich Kryukov at 7-Jan-13 14:50pm
   
Not really. If the code is "working" somehow, it does not mean it is even acceptable.
I explained it all in my answer, please see.
—SA
CHill60 at 7-Jan-13 19:17pm
   
Confused! Where did P/Invoke come into this .. did I miss a revision of the question?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Chances are you are using System.Windows.Forms.Cursor. (Again, always, always tag your UI library, like "Forms".)

But it already can be moved, because the property Position is a read/write property: http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx[^].

Please be advised that using P/Invoke kills platform compatibility of your code. If this is a Windows Forms application, it can be executed on many platforms without recompilation, via Mono. A single P/Invoke method can break this feature at once.

—SA
  Permalink  
Comments
Manas Bhardwaj at 7-Jan-13 13:24pm
   
Yes +5.
Btw, congratulations on your MVP status!
Sergey Alexandrovich Kryukov at 7-Jan-13 14:48pm
   
Thank you very much, Manas, you too!
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


Advertise | Privacy | Mobile
Web01 | 2.8.150723.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100