2011/02/24

[WPF] [C#] How-to : Detect if another application is running in full screen mode.

When an application is running in full screen mode, MSN Messenger can change your status to "Busy". Here are codes showed how to detect a application is running in full screen mode.

If you want to learn more, please read this article by Richard Banks. I copied the most part from his blog and then write my WPF sample.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace IsAnotherApplicationFullScreen {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
 
        Timer timer;
        public MainWindow() {
            InitializeComponent();
            //
            // timer
            //
            timer = new Timer();
            timer.Interval = 500;
            timer.Tick += new EventHandler(timer_Tick);
        }
 
        void timer_Tick(object sender, EventArgs e) {
            label1.Content = ScreenDetectionn.AreApplicationFullScreen();
        }
 
        void button1_Click(object sender, RoutedEventArgs e) {
            timer.Start();
        }
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
 
    public class ScreenDetectionn {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        private static extern IntPtr GetShellWindow();
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
 
        private static IntPtr desktopHandle;
        private static IntPtr shellHandle;
 
        public static bool AreApplicationFullScreen() {
            desktopHandle = GetDesktopWindow();
            shellHandle = GetShellWindow();
 
            bool runningFullScreen = false;
            RECT appBounds;
            System.Drawing.Rectangle screenBounds;
            IntPtr hWnd;
            hWnd = GetForegroundWindow();
 
            if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) {
                if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle))) {
                    GetWindowRect(hWnd, out appBounds);
                    screenBounds = System.Windows.Forms.Screen.FromHandle(hWnd).Bounds;
                    if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height
                        && (appBounds.Right - appBounds.Left) == screenBounds.Width) {
                        runningFullScreen = true;
                    }
                }
            }
 
            return runningFullScreen;
        }
    }
}
 

No comments: