[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Owned forms in Excel add-in

Tom

6/15/2007 7:41:00 AM

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();
}

base.Dispose(disposing);
}
}

Usage:
ExcelForm excelForm = new ExcelForm();
excelForm.Show(new IntPtr(Application.Hwnd));

2 Answers

Ben Voigt [C++ MVP]

6/15/2007 5:50:00 PM

0


"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));
>

Tom

6/21/2007 9:47:00 AM

0


The native window I'm passing is the main window of Excel.
So I guess the answer is yes, I need the window to stay open after
my .NET child form closes.