using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
// Нужно для Метода 1
using Autodesk.AutoCAD.Internal.Reactors;
 
// Для Метода 1
[assembly: ExtensionApplication(typeof(Rivilis.WinUtils1))]
// Для Метода 2
//[assembly: ExtensionApplication(typeof(Rivilis.WinUtils2))]
 
 
namespace Rivilis
{
  /// <summary>
  /// Для работы по Методу 1 (используется недокументированный 
  /// класс Autodesk.AutoCAD.Internal.Reactors.ApplicationEventManager)
  /// </summary>
  public class WinUtils1 : IExtensionApplication
  {
    ApplicationEventManager eventMngr = ApplicationEventManager.Instance();
 
    void IExtensionApplication.Initialize()
    {
      eventMngr.ApplicationDocumentFrameChanged += eventMngr_ApplicationDocumentFrameChanged;
    }
    void IExtensionApplication.Terminate()
    {
      try {
        eventMngr.ApplicationDocumentFrameChanged -= eventMngr_ApplicationDocumentFrameChanged;
      }
      catch { }
    }
 
    void eventMngr_ApplicationDocumentFrameChanged(object sender, EventArgs e)
    {
      try {
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        ed.WriteMessage("\nМетод 1: Размер окна изменён");
      }
      catch { }
    }
  }
 
  /// <summary>
  /// Для работы по Методу 2 (используется Application.PreTranslateMessage,
  /// в котором проверяется не изменился ли размер окна)
  /// </summary>
  public class WinUtils2 : IExtensionApplication
  {
    DocumentCollection docMan = Application.DocumentManager;
    Dictionary<Document, System.Windows.Size> winColl = new Dictionary<Document, System.Windows.Size>();
 
    void IExtensionApplication.Initialize()
    {
      foreach (Document doc in docMan) {
        winColl.Add(doc, doc.Window.DeviceIndependentSize);
      }
      docMan.DocumentCreated += docMan_DocumentCreated;
      docMan.DocumentToBeDestroyed += docMan_DocumentToBeDestroyed;
      Application.PreTranslateMessage += Application_PreTranslateMessage;
    }
 
    void Application_PreTranslateMessage(object sender, PreTranslateMessageEventArgs e)
    {
      try {
        Document doc = docMan.MdiActiveDocument;
        if (doc != null) {
          System.Windows.Size curSize = doc.Window.DeviceIndependentSize;
          if (curSize != winColl[doc]) {
            doc.Editor.WriteMessage("\nМетод 2: Размер окна изменён");
            winColl[doc] = curSize;
          }
        }
      }
      catch { }
    }
 
    void docMan_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
    {
      try { winColl.Remove(e.Document); } catch { }
    }
 
    void docMan_DocumentCreated(object sender, DocumentCollectionEventArgs e)
    {
      winColl.Add(e.Document, e.Document.Window.DeviceIndependentSize);
    }
 
    void IExtensionApplication.Terminate()
    {
      try {
        Application.PreTranslateMessage -= Application_PreTranslateMessage;
        docMan.DocumentCreated -= docMan_DocumentCreated;
        docMan.DocumentToBeDestroyed -= docMan_DocumentToBeDestroyed;
        foreach (Document doc in docMan) winColl.Remove(doc);
      }
      catch { }
    }
  }
}