Click here to Skip to main content
15,923,845 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 ... :( plz help



C#
}
       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
Updated 7-Jan-13 7:50am
v3
Comments
Sergey Alexandrovich Kryukov 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 7-Jan-13 11:05am    
The code posted in your question works, so what's the real problem?
farham_heidari 7-Jan-13 14:01pm    
ok tnx for advice
:)
Sergey Alexandrovich Kryukov 7-Jan-13 14:49pm    
Hope you will accept the answer formally (green button) — thanks.
—SA
Sergey Alexandrovich Kryukov 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

1 solution

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
 
Share this answer
 
Comments
Manas Bhardwaj 7-Jan-13 13:24pm    
Yes +5.
Btw, congratulations on your MVP status!
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900