Window Handle Win32 APIs Under C#

T

TC

Hey Folks,

I am using the following 4 Win32 APIs with a C# AddIn:

FindWindow
SetWindowLong
SetForegroundWindow
EnableWindow

Within the AddIn, there are some winforms. I use these APIs to set them as
the top window in modeless form but only from within the parent app (i.e.
while in the AddIn's parent app, the winforms are on top and modeless but
when moving to another app, the newly activated app is the top window).

For example, on the 'Load' event, I do the following:

SetWindowLong(this.Handle, GWL_HwndParent, this.ParentApp.Handle);

// Disable Outlook window for modal functionality
EnableWindow((IntPtr)this.ParentApp.Handle,false);
EnableWindow(this.Handle,true);

I know that one can set a winform to be a top window but it does it for the
entire desktop.

Is there a way under the .Net Framework to easily achieve the same behavior
accomplished via the APIs?

Thanks & Regards,

TC
 
D

Dmytro Lapshyn [MVP]

Hi TC,

In an add-in, I assume the host application's main window should be the
parent of all winforms created by the add-in.
If the host application is Visual Studio, you can get the handle of the main
window from the root automation object and then create a very simple
implementation of IWin32Window to return the obtained handle as the parent
window to forms you are creating from your add-in.
 
T

TC

Hey Dmytro,

You are correct in that the Office Suite application's main window is the
parent to my addin and I can get a handle to this window.

What I seem to be missing is how to use this handle to set a parent or owner
property to a C# winform. Both the 'Owner' property and the 'ParentForm'
property return 'Form' objects and not handles.

Regards,

TC



Dmytro Lapshyn said:
Hi TC,

In an add-in, I assume the host application's main window should be the
parent of all winforms created by the add-in.
If the host application is Visual Studio, you can get the handle of the
main window from the root automation object and then create a very simple
implementation of IWin32Window to return the obtained handle as the parent
window to forms you are creating from your add-in.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


TC said:
Hey Folks,

I am using the following 4 Win32 APIs with a C# AddIn:

FindWindow
SetWindowLong
SetForegroundWindow
EnableWindow

Within the AddIn, there are some winforms. I use these APIs to set them
as the top window in modeless form but only from within the parent app
(i.e. while in the AddIn's parent app, the winforms are on top and
modeless but when moving to another app, the newly activated app is the
top window).

For example, on the 'Load' event, I do the following:

SetWindowLong(this.Handle, GWL_HwndParent, this.ParentApp.Handle);

// Disable Outlook window for modal functionality
EnableWindow((IntPtr)this.ParentApp.Handle,false);
EnableWindow(this.Handle,true);

I know that one can set a winform to be a top window but it does it for
the entire desktop.

Is there a way under the .Net Framework to easily achieve the same
behavior accomplished via the APIs?

Thanks & Regards,

TC
 
D

Dmytro Lapshyn [MVP]

You are right - this works only for ShowDialog which accepts IWin32Window as
a parent.
For non-modal forms, you can try the SetParent API call.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


TC said:
Hey Dmytro,

You are correct in that the Office Suite application's main window is the
parent to my addin and I can get a handle to this window.

What I seem to be missing is how to use this handle to set a parent or
owner property to a C# winform. Both the 'Owner' property and the
'ParentForm' property return 'Form' objects and not handles.

Regards,

TC



Dmytro Lapshyn said:
Hi TC,

In an add-in, I assume the host application's main window should be the
parent of all winforms created by the add-in.
If the host application is Visual Studio, you can get the handle of the
main window from the root automation object and then create a very simple
implementation of IWin32Window to return the obtained handle as the
parent window to forms you are creating from your add-in.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


TC said:
Hey Folks,

I am using the following 4 Win32 APIs with a C# AddIn:

FindWindow
SetWindowLong
SetForegroundWindow
EnableWindow

Within the AddIn, there are some winforms. I use these APIs to set them
as the top window in modeless form but only from within the parent app
(i.e. while in the AddIn's parent app, the winforms are on top and
modeless but when moving to another app, the newly activated app is the
top window).

For example, on the 'Load' event, I do the following:

SetWindowLong(this.Handle, GWL_HwndParent,
this.ParentApp.Handle);

// Disable Outlook window for modal functionality
EnableWindow((IntPtr)this.ParentApp.Handle,false);
EnableWindow(this.Handle,true);

I know that one can set a winform to be a top window but it does it for
the entire desktop.

Is there a way under the .Net Framework to easily achieve the same
behavior accomplished via the APIs?

Thanks & Regards,

TC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top