Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#

Sending Input Messages to Other Windows or How To Win a Flash Game By Your Programming Skills

Rate me:
Please Sign up or sign in to vote.
3.12/5 (10 votes)
4 Jul 2006CPOL2 min read 61.1K   2.2K   47  
Library for sending input messages to other windows and links to libraries you need to take full control over any running window.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace TakeOverSample
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private WindowFinder wf;
		private ScreenCapture sc;
		private IntPtr handlePtr;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Timer timer1;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			sc = new ScreenCapture();
			handlePtr = new IntPtr(wf.handle);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.wf = new TakeOverSample.WindowFinder();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// wf
			// 
			this.wf.Location = new System.Drawing.Point(2, 2);
			this.wf.Name = "wf";
			this.wf.Size = new System.Drawing.Size(262, 46);
			this.wf.TabIndex = 0;
			this.wf.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
			this.wf.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown_1);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlDark;
			this.pictureBox1.Location = new System.Drawing.Point(8, 48);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(600, 448);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
			this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
			this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 502);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.wf);
			this.ImeMode = System.Windows.Forms.ImeMode.On;
			this.Name = "Form1";
			this.Text = "TakeOver Sample Application";
			this.TopMost = true;
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown_1);
			this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
			this.Activated += new System.EventHandler(this.Form1_Activated);
			this.ResumeLayout(false);

		}
		#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 (!wf.captured) return;

			if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
			pictureBox1.Image = sc.CaptureWindow(new IntPtr(wf.handle));//handlePtr
		}

		[DllImport("user32.dll", CharSet=CharSet.Auto)]
		private static extern bool SetForegroundWindow(IntPtr hWnd);

		private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (!wf.captured) return;
			if (e.Button == MouseButtons.Left) wf.to.SendLeftButtonDown(e.X,e.Y);
			else if (e.Button == MouseButtons.Right) wf.to.SendRightButtonDown(e.X,e.Y);
		}
 
	private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (!wf.captured) return;
			if (e.Button == MouseButtons.Left) wf.to.SendLeftButtonUp(e.X,e.Y);
			else if (e.Button == MouseButtons.Right) wf.to.SendRightButtonUp(e.X,e.Y);
		}

		private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (!wf.captured) return;
			wf.to.SendMouseMove(e.X,e.Y);
		}

		private void Form1_KeyDown_1(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (!wf.captured) return;
			//wf.to.SendKeyDown(e.KeyValue);
		}

		private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (!wf.captured) return;
			//wf.to.SendKeyUp(e.KeyValue);
		}

		private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (!wf.captured) return;
			wf.to.SendChar((char)e.KeyChar);
		}

		private void Form1_Activated(object sender, System.EventArgs e)
		{
			this.Focus();
		}

		private void axWindowsMediaPlayer1_Enter(object sender, System.EventArgs e)
		{
		
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Slovakia Slovakia
I am one step before graduating Faculty of Mathematics, Physics and Informatics, Commenius University Bratislava, specialization Computer Graphics. Working as C# programmer on large IS. Creator of Ubytovanie na Slovensku site.

Comments and Discussions