.Net and Win32 API Calls

T

TC

Hello,

I am in the process of migrating a VB6 component over to C#. This component
uses Win32 API calls such as FindWindow, SetForegroundWindow, SetWindowLong
and EnableWindow.

Should I be using these APIs to achieve the same results in C# or should I
be using something unbeknownst to me, up to this point, in the .Net
Framework.

Thanks & Regards,

TC
 
T

Tim Wilson

I guess it all depends on how you're using the API calls today. You can
certainly continue to pinvoke the API functions. A good number of pinvoke
declarations can be found at the link below.
http://www.pinvoke.net/
 
T

TC

Hey Tim,

Basically, I have a COM addin that has userforms. When one of these forms
is opened, I use the APIs to keep this form on top of its parent app's
window. It attaches the userform as a child of the parent app. This allows
the user to move between applications and the userform is only on top when
the parent app is active.

When the userform is closed, the parent app is brought to the top and all
continues as normal.

Before I imported this API functionality into my .Net version, I wanted to
be sure that there was not something in the .Net Framework that provided the
functionality making the API implementation obsolete.

Regards,

Tc
 
T

Tim Wilson

In .Net, Forms have a property called "TopMost" that allows you to specify
that a window is a top most window. So, speaking strickly from a .Net
application perspective, you can show a child Form and tie it to the parent
with the following code. This code is called from within the scope of
another Form (parent).

Form2 childForm = new Form2();
childForm.TopMost = true;
childForm.Owner = this;
childForm.Show();

So I suppose if you can get a reference to the parent Form you *should* be
ok.
 
T

T-Man

Hey Tim,

The issue would be, how to cast the Office Application, either ActiveWindow,
etc., to a "Form".

Certainly if it is all contained within a custom application, the method
listed below works great because the developer has control over all "Forms".

Any ideas?

Regards,

TC
 
T

Tim Wilson

You could try the static FromHandle method in the Control class, but I have
my doubts on that. There are .Net ways to accomplish the manipulation of
windows but I don't know if you can grab a window anywhere in the system and
cast it to a managed control.
 
T

T-Man

Yeah. I had similar thoughts. It's basically compounded by the fact that
Office does not have the same types of properties and methods exposed.
Consequently, it's probably easier just to make the 2-3 necessary API calls.

Ultimately, I just wondered if there was some simple but less than obvious
way to do it in .Net and, it appears, there is not.
 
T

Tim Wilson

I was going to recommend that maybe sticking with the pinvokes (or
unmanaged) solution at this point might be best, but I see that you're
already headed that way.
 

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