How can a .NET 2.0 DLL get application icon at runtime?

D

DBxGlock

Howdy folks,

I have a library that is included by other applications. The library
provides a pop-up form. I'd like the pop-up form to be able to use the
same icon as the application that's calling it. Because I don't have
control of all the applications that will use this library, I can't
assume that the developers of the applications will be kind enough to
embed an icon as a resource for me.

So...At runtime, with no information about the application that is
calling the library, how can the library retrieve the application's
icon? This has to be possible because Windows knows about the icon (it
appears on the task bar, and with alt-tab). Does anybody know how to
retrieve this icon?

Thanks a bunch,
Dan
 
N

Norman Yuan

I think a better approach is not to force the code in your DLL to try to
grab a unknown icon from host app (You do not even know if there is one or
not), instead, you can provide overloaded methods that pop up the said form.
the overloaded methods cuold look like:

public void PopupTheForm(sometype, somePara)
{
...
//show you default icon or not icon at all
...
}

public void PopupTheForm(sometype, somePara, Image formIcon)
{
...
//show the passed in icon on the form
...
}

If the other developers who use your DLL decide that they want to show their
icon on the form in your DLL, they can simply use the overloaded method that
accept an Image argument (or byte array, or stream....).
 
S

Stoitcho Goutsev \(100\) [C# MVP]

DBxGlock,



I agree with Norman, you should give the programmer option to set up the
icon that needs to be used in the form. You know that biggest frustrations
come from the smartest controls :)

However I'd like to give you one solution for your question.

First of all I'd want to make sure that you know there are two types of icon
resources that winform application holds: One is that you set for the
project and the other is that you set for each form.

The icon on the project level goes as unmanaged resource in the assembly and
this is the icon that Windows uses in the explorer, desktop, etc. This icon
is an unmanaged resource and unless you don't use Win32 API via PInvoke I
don't think you can read it.

The other icons, which you set for each form go as managed resource and is
pretty easy to get as long as you have reference to the form. Once you have
reference to some form you can read its Icon property and the icon is there.
The tricky part is how to get reference to the form and what form we are
talking about. An application may have more than one form displayed on the
screen at a time.
The form class provides ActiveForm static property that returns currently
active form. If the application has only one form this might be what you
want.

The following code is my solution to your problem. The discovery of the main
form is kind of funky , but this is the only one I can think of right now.
Hopefully some one else on the group get give us a better solution.

Add the following code to your Dll's form constructor:

Form f = Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle) as
Form;
if(f != null)
{
Icon icon = (Icon)f.Icon.Clone();
this.Icon = icon;
}

Once again I agree with Norman and I believe some combination between both
would be a good idea. Keep in mind also that form's icon my have some
meaning different than the form affiliation with the application - e.g.
'Find' form might have magnifying glass for an icon. In the latter case it
will be little bit rude if you replace the icon.

HTH
--

Stoitcho Goutsev (100) [C# MVP]
 
D

DBxGlock

Thank you. Getting the icon from the main window handle worked great!
I had been doing it this way:

returnIcon = Application.OpenForms[0].Icon;

I don't know if that would always work, or if the main form would
always be form 0.

Thanks,
Dan
 
S

Stoitcho Goutsev \(100\) [C# MVP]

DBxGlock,

I didn't see that actually we were discussing .NET2.0. I should read the
subjects more carefully.

This property was added to the Application class in 2.0 and you can use it
for that. The porperty contains all visible forms in the order of making
them visible. Since while the application is running the main form will be
the one that has been first visualized and will stay visible throughout the
application's lifetime, I believe you'll get correct results. Ofcourse there
are chances with some strange applications that this might not work, but
these chances are very small.
 

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