{
// the Outlook Inspectors collection
Outlook.Inspectors _Inspectors
// the Outlook Explorers collection
Outlook.Explorers _Explorers
// a collection of wrapped objects
Dictionary<guid,WrappedObject>_WrappedObjects
/// <summary>
/// The entrypoint for the application
/// </summary>
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
_WrappedObjects = new Dictionary<guid,WrappedObject>()
// Inspectors stuff
_Inspectors = this.Application.Inspectors
// Any open Inspectors after startup ?
for (int i = _Inspectors.Counti >= 1i--)
{
// wrap the Inspector
WrapInspector(_Inspectors[i])
}
// get notified for new inspectors
_Inspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector)
// Explorer stuff
_Explorers = this.Application.Explorers
// Are there any open Explorers after Startup ?
for (int i = _Explorers.Counti >= 1i--)
{
/升隐历/ Wrap the Explorer and do something useful with it
WrapExplorer(_Explorers[i])
}
// get notified for new application windows
_Explorers.NewExplorer += new
Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer)
}
/// <summary>
/// Event sink for the NewExplorer event.
/// </summary>
/// <param name=""""Explorer"吵搜""" />The new Explorer instance</param />
void _Explorers_NewExplorer(Outlook.Explorer Explorer)
{
WrapExplorer(Explorer)
}
/// <summary>
/// The Explorer is "wrapped" and used in the application.
/// </summary>
/// <param name=""""explorer"""" />The new Explorer instance<携碰/param />
void WrapExplorer(Outlook.Explorer explorer)
{
ExplorerWrapper wrappedExplorer = new ExplorerWrapper(explorer)
wrappedExplorer.Closed += new WrapperClosedDelegate(wrappedObject_Closed)
_WrappedObjects[wrappedExplorer.Id] = wrappedExplorer
}
/// <summary>
/// Event sink for the NewInspector event.
/// </summary>
/// <param name=""""Inspector"""" />The new Inspector instance</param />
void _Inspectors_NewInspector(Outlook.Inspector Inspector)
{
WrapInspector(Inspector)
}
/// <summary>
/// The Inspector is "wrapped" and used in the application.
/// </summary>
/// <param name=""""inspector"""" />The new Inspector instance</param />
void WrapInspector(Outlook.Inspector inspector)
{
InspectorWrapper wrappedInspector = new InspectorWrapper(inspector)
wrappedInspector.Closed += new WrapperClosedDelegate(wrappedObject_Closed)
_WrappedObjects[wrappedInspector.Id] = wrappedInspector
}
/// <summary>
/// Event sink for the WrappedInstanceClosed event.
/// </summary>
/// <param name=""""id"""" />The unique ID of the closed object</param />
void wrappedObject_Closed(Guid id)
{
_WrappedObjects.Remove(id)
}
/// <summary>
/// Exitpoint for the application, do the cleanup here.
/// </summary>
/// <param name=""""sender"""" /></param />
/// <param name=""""e"""" /></param />
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
_WrappedObjects.Clear()
_Inspectors.NewInspector -= new
Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector)
_Inspectors = null
_Explorers.NewExplorer -= new
Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer)
_Explorers = null
GC.Collect()
GC.WaitForPendingFinalizers()
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup)
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown)
}
#endregion
}
The abstract WrapperClass:
/// <summary>
/// Delegate signature to inform the application about closed objects.
/// </summary>
/// <param name=""""id"""" />The unique ID of the closed object.</param />
public delegate void WrapperClosedDelegate(Guid id)
/// <summary>
/// The Wrapperclass itself has a unique ID and a closed event.
/// </summary>
internal abstract class WrapperClass
{
/// <summary>
/// The event occurs when the monitored item has been closed.
/// </summary>
public event WrapperClosedDelegate Closed
/// <summary>
/// The unique ID of the wrapped object.
/// </summary>
public Guid Id { getprivate set}
protected void OnClosed()
{
if (Closed != null) Closed(Id)
}
/// <summary>
/// The constructor creates a new unique ID.
/// </summary>
public WrapperClass()
{
Id = Guid.NewGuid()
}
}
The Inspector wrapper class:
/// <summary>
/// The InspectorWrapper used to monitor the state of an Inspector during its lifetime.
/// </summary>
internal class InspectorWrapper : WrapperClass
{
/// <summary>
/// The Outlook Inspector Instance.
/// </summary>
public Outlook.Inspector Inspector { getprivate set}
/// <summary>
/// Construction code.
/// </summary>
/// <param name=""""inspector"""" />The Inspector Object</param />
public InspectorWrapper(Outlook.Inspector inspector)
{
Inspector = inspector
ConnectEvents()
}
/// <summary>
/// Register the events to get notified of Inspector statechanges within the application.
/// </summary>
void ConnectEvents()
{
((Outlook.InspectorEvents_10_Event)Inspector).Close +=
new Outlook.InspectorEvents_10_CloseEventHandler(InspectorWrapper_Close)
((Outlook.InspectorEvents_10_Event)Inspector).Activate +=
new Outlook.InspectorEvents_10_ActivateEventHandler(InspectorWrapper_Activate)
((Outlook.InspectorEvents_10_Event)Inspector).Deactivate +=
new Outlook.InspectorEvents_10_DeactivateEventHandler(InspectorWrapper_Deactivate)
}
/// <summary>
/// Unregister the events / cleanup.
/// </summary>
void DisconnectEvents()
{
((Outlook.InspectorEvents_10_Event)Inspector).Close -=
new Outlook.InspectorEvents_10_CloseEventHandler(InspectorWrapper_Close)
((Outlook.InspectorEvents_10_Event)Inspector).Activate -=
new Outlook.InspectorEvents_10_ActivateEventHandler(InspectorWrapper_Activate)
((Outlook.InspectorEvents_10_Event)Inspector).Deactivate -=
new Outlook.InspectorEvents_10_DeactivateEventHandler(InspectorWrapper_Deactivate)
}
/// <summary>
/// Event sink for the Close event. Memory Cleanup and inform the application.
/// </summary>
void InspectorWrapper_Close()
{
DisconnectEvents()
Inspector = null
GC.Collect()
GC.WaitForPendingFinalizers()
// inform the application to release al references.
OnClosed()
}
/// <summary>
/// Event sink for the Activate event
/// </summary>
void InspectorWrapper_Activate()
{
}
/// <summary>
/// Event sink for the deactivate event
/// </summary>
void InspectorWrapper_Deactivate()
{
}
}
详细请见http://www.codeproject.com/KB/office/CustomAddressDialog.aspx?display=Print
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)