Download AppActivationDemo.zip, last updated 29/12/2013 (11.64 KB)

Download
  • md5: 3d84cc965982745d60d01044853821d7
using System;
using System.Windows.Forms;

// How to be notified when your application is activated and deactivated
// http://cyotek.com/blog/how-to-be-notified-when-your-application-is-activated-and-deactivated

namespace AppActivationDemo
{
  internal class BaseApplicationForm : Form
  {
    #region Events

    public event EventHandler ApplicationActivated;

    public event EventHandler ApplicationDeactivated;

    #endregion

    #region Overridden Members

    protected override void WndProc(ref Message m)
    {
      if (m.Msg == NativeMethods.WM_ACTIVATEAPP)
      {
        if (m.WParam != IntPtr.Zero)
          this.OnApplicationActivated(EventArgs.Empty);
        else
          this.OnApplicationDeactivated(EventArgs.Empty);
      }

      base.WndProc(ref m);
    }

    #endregion

    #region Members

    protected virtual void OnApplicationActivated(EventArgs e)
    {
      EventHandler handler;

      handler = this.ApplicationActivated;

      if (handler != null)
        handler(this, e);
    }

    protected virtual void OnApplicationDeactivated(EventArgs e)
    {
      EventHandler handler;

      handler = this.ApplicationDeactivated;

      if (handler != null)
        handler(this, e);
    }

    #endregion
  }
}

Donate

Donate