Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Windows Application moves the mouse programmatically

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.
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

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?????
ASKER CERTIFIED SOLUTION
Avatar of Yurich
Yurich
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wow, your way looks a bit more complicated...
Thank you, Yurich!
any time ;)