470,588 Members | 2,108 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,588 developers. It's quick & easy.

Window Handle Win32 APIs Under C#

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
3 13494
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
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
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.

Similar topics

2 posts views Thread by Shawn Anderson | last post: by
11 posts views Thread by objectref | last post: by
2 posts views Thread by Jonathan Boivin | last post: by
18 posts views Thread by rdahlstrom | last post: by
3 posts views Thread by =?Utf-8?B?QUEyZTcyRQ==?= | last post: by
9 posts views Thread by =?Utf-8?B?UmFq?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.