Ben Voigt [C++ MVP]
6/15/2007 5:50:00 PM
"Tom" <tom.kostiainen@saunalahti.fi> wrote in message
news:1181893283.556881.10200@w5g2000hsg.googlegroups.com...
> Hi,
>
> I want to use forms in my Excel application-level add-in that have the
> main Excel window set as their owner. Could someone who knows these
> thins tell me, am I disposing the handle correctly?
>
> public partial class ExcelForm : Form
> {
> NativeWindow nativeWindow;
>
> public void Show(IntPtr handle)
> {
> nativeWindow = new NativeWindow();
> nativeWindow.AssignHandle(handle);
> base.Show(nativeWindow);
> }
>
> protected override void Dispose(bool disposing)
> {
> if (disposing && (components != null)) // Windows Form
> Designer generated code
> {
> components.Dispose();
> }
>
> // Is this the correct way and place?
> if (nativeWindow != null)
> {
> nativeWindow.ReleaseHandle();
Are you trying to leave the window open after your .NET form is disposed?
That's what ReleaseHandle does, detaches the Windows object from the .NET
object. If you want the window to close just do nothing, base.Dispose()
below will cleanup the window.
> }
>
> base.Dispose(disposing);
> }
> }
>
> Usage:
> ExcelForm excelForm = new ExcelForm();
> excelForm.Show(new IntPtr(Application.Hwnd));
>