Monitoring Window Position.

I am writing an application that needs to continuously monitor the position of another program's window.  So I need to get the position of the main window of this other program and I need to know if that window is moved.  Code samples would be nice.  I am writing my application in C#.  Any suggestion how to do this?

Thanks!
stminfoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

athapaCEO / CTOCommented:
Use FindWindow to find the window by title (if you know the title). If you know the process then you can also get the MainWindowHandle if the one you are intersted is the main window.

Once you find the window, you can use that handle to get it co-ordinates/sizes/etc using GetWindowInfo.

You can find the syntax of using these calls in PInvoke.net and other places. Let me know if you have problems.

AT
athapaCEO / CTOCommented:
You can actually also use GetWindowRect once you have the handle of the window to get its co-ordinates.

AT
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
athapa...those techniques will certainly get you the CURRENT position of the window but the author wants to "continuously monitor the position of another program's window".

Using those methods you would need to poll with a loop...not very pretty.

I think he is looking for some kind of window hook but I'm not sure how to do that with an external application.
YetiNZCommented:
You will need a hook into the other windw. I use this: http://www.vbforums.com/showthread.php?t=322261

download the hookcontrol, and add it to your project

I used it with the code below to have my form follow a 'Notepad' window around the screen as well as resize its width:

            [DllImport("user32")]private static extern int GetWindowRect(IntPtr handle,ref RECT rc);
            [DllImport("user32.dll")]public static extern int FindWindow(string lpClassName,string lpWindowName);

            private struct RECT
            {      public int left;
                  public int top;
                  public int right;
                  public int bottom;
            }
      
            private AxHookCtrl.AxHookControl axHookControl1;

            private const int WM_SIZING = 0x0214;
            private const int WM_MOVING = 0x0216;
            private const int WM_MOVE = 0x3;
            private const int WM_SIZE = 0x5;

            private RECT rc;
            private int iHwnd;

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  rc = new RECT();                  
                  iHwnd = FindWindow("Notepad", null);

                  if (iHwnd!=0)
                  {
                        GetWindowRect(new IntPtr(iHwnd), ref rc);
                        this.Location = new Point(rc.left, rc.bottom);
                        this.Width = rc.right - rc.left;

                        int lWM_MOVING = WM_MOVING;
                        int lWM_SIZING = WM_SIZING;
                        int lWM_MOVE = WM_MOVE;
                        int lWM_SIZE = WM_SIZE;
                        string s = null;

                        this.axHookControl1.TargethWnd=iHwnd;
                        this.axHookControl1.AddMessage(ref lWM_MOVING, ref s);
                        this.axHookControl1.AddMessage(ref lWM_SIZING, ref s);
                        this.axHookControl1.AddMessage(ref lWM_MOVE, ref s);
                        this.axHookControl1.AddMessage(ref lWM_SIZE, ref s);
                        this.axHookControl1.SetHook();
                  }
            }

            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                  this.axHookControl1.RemoveAllHooks();
            }

            private void axHookControl1_SentMessage(object sender, AxHookCtrl.__HookControl_SentMessageEvent e)
            {
                  switch (e.uMsg)
                  {
                        case WM_MOVING:
                              rc = (RECT)Marshal.PtrToStructure(new IntPtr(e.lParam), typeof(RECT));
                    this.Location = new Point(rc.left, rc.bottom);
                              break;
                           case WM_SIZING:
                              rc = (RECT)Marshal.PtrToStructure(new IntPtr(e.lParam), typeof(RECT));
                              this.Width = rc.right - rc.left;
                              break;
                        case WM_MOVE:
                               GetWindowRect(new IntPtr(iHwnd), ref rc);
                              this.Location = new Point(rc.left, rc.bottom);
                              break;
                        case WM_SIZE:
                              GetWindowRect(new IntPtr(iHwnd), ref rc);
                              this.Width = rc.right - rc.left;
                              break;
                  }
            }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.