Solved

Capture active app bounds

Posted on 2011-03-03
2
196 Views
Last Modified: 2013-12-17
On a windows system.  I need to be able to capture the bounds of the currently active application.  So for example I have three applications open in Windows 7.  Photoshop has been used last and is the active application on the desktop.  How do I locate that application in C# and capture it's current bounds?
0
Comment
Question by:Phil5780
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35033291
Use the GetForegroundWindow() and GetWindowRect() APIs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32", EntryPoint = "GetWindowRect", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int GetWindowRect(IntPtr handle, ref RECT lpRect);

        private void timer1_Tick(object sender, EventArgs e)
        {
            RECT R = new RECT();
            GetWindowRect(GetForegroundWindow(), ref R);
            label1.Text = "(" + R.Left.ToString() + ", " + R.Top.ToString() + ") --> (" + R.Right.ToString() + ", " + R.Bottom.ToString() + ")";
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Phil5780
ID: 35300455
Works great, thanks!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question