Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Windows Application moves the mouse programmatically

Posted on 2005-04-18
5
Medium Priority
?
254 Views
Last Modified: 2008-02-01
I need a C# source code for a Windows Program that, when run...will programmatically move the mouse from Point A to Point B, then from Point B to Point A -- about once a minute.

I have this stupid monitor on my computer that locks my computer if I don't move the mouse periodically.  I cannot disable the monitor....so I want to write this program to fool the OS into thinking I am doing something.
0
Comment
Question by:Tom Knowlton
  • 3
  • 2
5 Comments
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13811305
I found this:



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplicationMoveMouse
{

      

      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            [DllImport("user32.dll")]
            private static extern void mouse_event(
                  UInt32 dwFlags, // motion and click options
                  UInt32 dx, // horizontal position or change
                  UInt32 dy, // vertical position or change
                  UInt32 dwData, // wheel movement
                  IntPtr dwExtraInfo // application-defined information
                  );

            private System.Windows.Forms.Timer timer1;
            private System.ComponentModel.IContainer components;

            private bool changed;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //

                  changed = false;
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  this.timer1 = new System.Windows.Forms.Timer(this.components);
                  //
                  // timer1
                  //
                  this.timer1.Enabled = true;
                  this.timer1.Interval = 3000;
                  this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Name = "Form1";
                  this.Text = "Form1";

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            ///
      

            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void timer1_Tick(object sender, System.EventArgs e)
            {
                  if(changed == false)
                  {
                        SendClick(new System.Drawing.Point(50,50));                  
                  }
                  else
                  {
                        SendClick(new System.Drawing.Point(100,100));                  
                  }

                  changed = !changed;

                  
            }

            public static void SendClick(Point location)
            {
                  Cursor.Position = location;
                  mouse_event(1, 0, 0, 0, new System.IntPtr());
                  mouse_event(1, 0, 0, 0, new System.IntPtr());
            }

      }
}






Is there a better....or easier way?????
0
 
LVL 21

Accepted Solution

by:
Yurich earned 2000 total points
ID: 13811360
wow, that's rather an interesting approach to solve the problem, lol. And that's rather a strange monitor..

But anyway, back to the coding:

a simple windows form as a base application - you can modify it later and make minimized to the system tray or invisible or whatever...

1. add a bool var some where in the class scope, that's to track the mouse moving from a to b and back
bool down = true;    // before your constructor will be a good place

2. drag and drop a timer on your form, set the period you want your cursor to be moved (its in milliseconds), switch to events (still in timer properties) and double-click tick event

3. add the following code in your timer event, and your timer event should look like this:
private void timer1_Tick(object sender, System.EventArgs e)
{
   Point point = Cursor.Position;

   if( down )
      point.Y += 50;
   else
      point.Y -= 50;

   down = !down;

   Cursor.Position = point;
}

4. you can start the timer from on your page_load or you can add two buttons for:

timer1.Start();

and for

timer1.Stop();

that will move your mouse up and down 50 pixels - you can change this value and set it to whatever you want
regards
0
 
LVL 21

Expert Comment

by:Yurich
ID: 13811366
wow, your way looks a bit more complicated...
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13811372
Thank you, Yurich!
0
 
LVL 21

Expert Comment

by:Yurich
ID: 13811466
any time ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

578 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