Solved

Capture active app bounds

Posted on 2011-03-03
2
178 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
Comment Utility
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
Comment Utility
Works great, thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
combo box display member not working 11 19
Problem to Popup 2 48
asp.net mvc5 3 35
Please explain: Aspect Oriented Programming 2 50
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now