I am subclassing a Win32 window in managed code using NativeWindow. However, I'm encountering a bug in either my code or with NativeWindow that throws an exception when the parent is closed. The code I'm using is this:
public partial class ThisAddIn
{
private VisioWindow visioWindow;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
visioWindow = new VisioWindow();
visioWindow.AssignHandle(new IntPtr(this.Application.Window.WindowHandle32));
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
visioWindow.ReleaseHandle();
}
#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
public class VisioWindow : NativeWindow
{
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}
}
}
On exit of the main program, I get this error:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.Windows.Forms.dll A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.Windows.Forms.dll
And the "This program has encountered an error" shows up saying the parent encountered an error.
Is there a different way to override the parent's WndProc than using NativeWindow? Or is there a bug in this code that could be worked out?
Thanks.