Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Move Window from other window without directly interacting with it in C#.net 2.0.

Author: DevAsp Team
Download Source Code : 689_MoveOtherWindow.zip

In this simple article you will learn how you can move window from other window without directly interacting with it.

Simply just create New Project of C#.

 

Firstly Add Reference of System.Runtime.InteropServices.

 

using System.Runtime.InteropServices;

 

Now drag one Button, two Textbox, two track bars and four Labels on the form

 

First there is concept to interact with any application directly, if we have Handle of any window we can do any thing with that window. Windows (Operating System) do all working through Messages. So in this article we use same technique to interact with other window.

 

We get handle of any window by giving it Class Name and its Text, we take here example of Notepad. Class name of Notepad is Notepad and you can see its text on top of window.

 

Firstly import these DLLs

 

[DllImport("user32.dll")]

        public static extern int FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("user32.dll")]

        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

 

[DllImport("User32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]

        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int cx, int cy, bool repaint);

 

FindWindow(… method return you handle of the window………

 

SendMessage(… is use to send message to Specific window……

 

MoveWindow(... is use to Move any window

 

Define some global Variables these are Predefined Message of Windows (OS)

 

public const int WM_COMMAND = 0x0112;

      public const int WM_CLOSE = 0xF060;

 

Now just write code on Form Load Event

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.button1.Text = "Get Window";

           

            this.Text = "DevAsp Application Controller";

 

            this.button1.Text = "Move";

 

            this.textBox1.Text = "Notepad";

 

            this.textBox2.Text = "Untitled - Notepad";

 

            label1.Text = "Class Name";

 

            label2.Text = "Window Text";

 

            label3.Text = "Move along X-axis";

 

            label4.Text = "Move along Y-axis";

 

 

        }

 

Now write this very simple code on Button Click Event

 

private void button1_Click(object sender, EventArgs e)

        {

            handle = FindWindow(textBox1.Text, textBox2.Text);

        }

Now write code on track bar scroll event

 

private void trackBar1_Scroll(object sender, EventArgs e)

        {

            MoveWindow((IntPtr)handle, (trackBar1.Value*80), 20 , (trackBar1.Value*80)-800, 120, true);

           

        }

 

        private void trackBar2_Scroll(object sender, EventArgs e)

        {

            MoveWindow((IntPtr)handle, 40, (trackBar2.Value * 100), 240, (trackBar2.Value * 100)+200, true);        }

        }

 

When you run application before click on this button first run a new instance of Notepad this application now you can move notepad through trackbar.

Article Comments
good example for MoveWindow

Posted on 12/16/2009 9:18:00 AM by b razvan

Does it work for maximized windows too?

Posted on 3/22/2010 5:26:33 AM by Coder

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Close some other Application without directly interacting with it in C#.net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net