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;
        }
    }
}
 

2011/02/23

[WPF] [C#] How-to : Make application blink in the taskbar like MSN Messenger dose.

Here are codes showed how to make the application blink in taskbar like MSN Messenger dose.

I have read a discussing thread at stackOverFlow. And then fit it into WPF version. The only different is how to get the handle of the current window. In Windows Forms, you can use this.Handle property to get it. In WPF, you have do it this way: new WindowInteropHelper(this).Handle. Be ware of using System.Runtime.InteropServices and System.Windows.Interop.

There are two buttons on the window. Click Button1 to start flash. Button2 for stopping. The only different is the fInfo.dwFlags property. 3 for starting flash. 0 for stopping.

Edit these codes to fit your scenario and wish you coding fun.

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.Interop;

 

namespace FlashTaskbar {

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window {

        [DllImport("user32.dll")]

        [return: MarshalAs(UnmanagedType.Bool)]

        static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

 

        [StructLayout(LayoutKind.Sequential)]

        public struct FLASHWINFO {

            public UInt32 cbSize;

            public IntPtr hwnd;

            public UInt32 dwFlags;

            public UInt32 uCount;

            public UInt32 dwTimeout;

        }

 

        public const UInt32 FLASHW_ALL = 3;

 

        public MainWindow() {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, RoutedEventArgs e) {

            FLASHWINFO fInfo = new FLASHWINFO();

 

            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));

            fInfo.hwnd = new WindowInteropHelper(this).Handle;

            fInfo.dwFlags = FLASHW_ALL;

            fInfo.uCount = UInt32.MaxValue;

            fInfo.dwTimeout = 0;

 

            FlashWindowEx(ref fInfo);

        }

 

        private void button2_Click(object sender, RoutedEventArgs e) {

            FLASHWINFO fInfo = new FLASHWINFO();

 

            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));

            fInfo.hwnd = new WindowInteropHelper(this).Handle;

            fInfo.dwFlags = 0;

            fInfo.uCount = UInt32.MaxValue;

            fInfo.dwTimeout = 0;

 

            FlashWindowEx(ref fInfo);

        }

    }

}

2011/02/14

C#: How-to open a file using its default application.

Here is a code sample for opening a file by the default application in your OS.

string folder = AppDomain.CurrentDomain.BaseDirectory;
string pdf = "an_arcobat_pdf_file.pdf";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = pdf;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
System.Diagnostics.Process.Start(psi);