Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version
Go to top

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

, 4 Jul 2006
Library for sending input messages to other windows and links to libraries you need to take full control over any running window.
take-over_demo.zip
App.ico
Finder.cur
FinderGone.bmp
FinderHome.bmp
resources.rct
TakeOverSample.exe
TakeOverSample.csproj.user
take-over_src.zip
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace TakeOverSample
{
	/// <summary>
	/// Summary description for WindowFinder.
	/// </summary>
	public class WindowFinder : System.Windows.Forms.UserControl
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		private bool _capturing;
		public int handle;
		public bool captured = false;
		public Remo.TakeOver to;
		private Image _finderHome;
		private Image _finderGone;		
		private Cursor _cursorDefault;
		private Cursor _cursorFinder;
		private System.Windows.Forms.PictureBox _pictureBox;
		private System.Windows.Forms.Label label1;
		private IntPtr _hPreviousWindow;

		public WindowFinder()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			_cursorDefault = Cursor.Current;

			_cursorFinder = new Cursor(GetType(),"Finder.cur");
            _finderHome = new Bitmap(GetType(),"FinderHome.bmp");
			_finderGone = new Bitmap(GetType(),"FinderGone.bmp");
			_pictureBox.Image = _finderHome;
			handle = 0;
		}


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

					if (_capturing)
						this.CaptureMouse(false);
				}
			}
			base.Dispose( disposing );
		}

		#region Component 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._pictureBox = new System.Windows.Forms.PictureBox();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// _pictureBox
			// 
			this._pictureBox.Location = new System.Drawing.Point(8, 8);
			this._pictureBox.Name = "_pictureBox";
			this._pictureBox.Size = new System.Drawing.Size(32, 32);
			this._pictureBox.TabIndex = 28;
			this._pictureBox.TabStop = false;
			this._pictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this._pictureBox_MouseDown);
			// 
			// label1
			// 
			this.label1.Enabled = false;
			this.label1.Location = new System.Drawing.Point(56, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(208, 32);
			this.label1.TabIndex = 29;
			this.label1.Text = "Drag the finder over Windows Control you want to control and then release it.";
			// 
			// WindowFinder
			// 
			this.Controls.Add(this.label1);
			this.Controls.Add(this._pictureBox);
			this.Name = "WindowFinder";
			this.Size = new System.Drawing.Size(264, 40);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Processes window messages sent to the Spy Window
		/// </summary>
		/// <param name="m"></param>
		protected override void WndProc(ref Message m)
		{									
			switch(m.Msg)
			{
					/*
					 * stop capturing events as soon as the user releases the left mouse button
					 * */
				case (int)Win32.WindowMessages.WM_LBUTTONUP:
					this.CaptureMouse(false);
					if (this.handle != 0) 
					{
						this.captured = true;
						to = new Remo.TakeOver(this.handle);
					}
					break;
					/*
					 * handle all the mouse movements
					 * */
				case (int)Win32.WindowMessages.WM_MOUSEMOVE:
					this.HandleMouseMovements();
					break;			
			};

			base.WndProc (ref m);
		}

		private void _pictureBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
				this.CaptureMouse(true);
		}

		/// <summary>
		/// Captures or releases the mouse
		/// </summary>
		/// <param name="captured"></param>
		private void CaptureMouse(bool captured)
		{
			// if we're supposed to capture the window
			if (captured)
			{
				// capture the mouse movements and send them to ourself
				Win32.SetCapture(this.Handle);

				// set the mouse cursor to our finder cursor
				Cursor.Current = _cursorFinder;

				// change the image to the finder gone image
				_pictureBox.Image = _finderGone;
			}
				// otherwise we're supposed to release the mouse capture
			else
			{
				// so release it
				Win32.ReleaseCapture();

				// put the default cursor back
				Cursor.Current = _cursorDefault;

				// change the image back to the finder at home image
				_pictureBox.Image = _finderHome;

				// and finally refresh any window that we were highlighting
				if (_hPreviousWindow != IntPtr.Zero)
				{
					WindowHighlighter.Refresh(_hPreviousWindow);
					_hPreviousWindow = IntPtr.Zero;
				}
			}

			// save our capturing state
			_capturing = captured;
		}

		/// <summary>
		/// Handles all mouse move messages sent to the Spy Window
		/// </summary>
		private void HandleMouseMovements()
		{
			// if we're not capturing, then bail out
			if (!_capturing)
				return;
											
				// capture the window under the cursor's position
				IntPtr hWnd = Win32.WindowFromPoint(Cursor.Position);

				// if the window we're over, is not the same as the one before, and we had one before, refresh it
				if (_hPreviousWindow != IntPtr.Zero && _hPreviousWindow != hWnd)
					WindowHighlighter.Refresh(_hPreviousWindow);

				// if we didn't find a window.. that's pretty hard to imagine. lol
				if (hWnd == IntPtr.Zero)
				{
					
				}
				else
				{
					// save the window we're over
					_hPreviousWindow = hWnd;

					this.handle = hWnd.ToInt32();
					// highlight the window
					WindowHighlighter.Highlight(hWnd);
				}
			}

	}
}

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)

Share

About the Author

Michal Remi
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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 4 Jul 2006
Article Copyright 2006 by Michal Remi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid