By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Window Handle Win32 APIs Under C#

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <ge**********@yahoo.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #2

P: n/a
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 [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:Ok*************@TK2MSFTNGP12.phx.gbl...
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" <ge**********@yahoo.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #3

P: n/a
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" <ge**********@yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP14.phx.gbl...
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 [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:Ok*************@TK2MSFTNGP12.phx.gbl...
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" <ge**********@yahoo.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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



Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.