问题
I'm trying to detect some events that VSTO doesn't provide such as WM_MOVE, WM_SIZE, etc in order to adjust the position of a window. This window is created by the add-in I'm working on and should react when the Word window is changed. I've basically manage to do my task but a very annoying problem remains. Whenever I close Word it pops up its crash handler. Obviously, this has something to do with the improper disposal of the NativeWindow-based object that I use. I've placed a button in the Ribbon to be able to dispose the object by hand and it worked perfectly fine. My suspicion is that the garbage collector is not doing its job properly for some reason. Furthermore, even calling ReleaseHandle() on WM_CLOSE or WM_DESTROY does not prevent the crash. Here's the code of my interceptor object:
public class OfficeWindow : NativeWindow, IDisposable
{
public OfficeWindow(IntPtr handle)
{
this.AssignHandle(handle);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case (int)WindowMessages.WM_MOVE:
MessageBox.Show("Move");
break;
//other cases
}
base.WndProc(ref m);
}
#region IDisposable Members
~OfficeWindow()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Free other state (managed objects).
}
ReleaseHandle();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
I'm using VS 2008, VSTO 3.0 and the add-in targets Word 2007.
来源:https://stackoverflow.com/questions/7766534/subclassing-ms-words-window-from-a-vsto-add-in