tags:

views:

26

answers:

2

How can I get the icon of a running application provided I know the Hwnd?

A: 

I you have the hwnd you can get the process ID by using the WINAPI GetWindowThreadProcessId. With that you can create a C# Process object. Next, you can iterate through the process' ProcessModule Collection to get the filename of the executable. Finally, you can use the WINAPI function ExtractIconEx to get the icon from the path

Pinvoke has information on the two WINAPI methods

http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html

http://www.pinvoke.net/default.aspx/shell32/ExtractIconEx.html

OG
+1  A: 

If you have the handle to the window, you can use GetClassLong:

HICON icon = (HICON)GetClassLong(window, GCL_HICON);

If you're interested in bigger example, I've written a sample app (more detailed description) that uses it.

PiotrLegnica