Solved

Capture active app bounds

Posted on 2011-03-03
2
192 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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