icon from window handle ?

  • Thread starter Thread starter steilemoy
  • Start date Start date
S

steilemoy

Hi all ! This is my first post I hope I am posting in the right place !

I am developing an application in .net with visual c# for windows
mobile 5.

I have the list of running programs and their handles. How can I get
their icon so I can populate an imagelist ?

Thanks !
 
You need to send WM_GETICON (0x007f) )message to the application main
window.
IntPtr hIcon = (IntPtr)SendMessage(hWnd, 0x007f, 0, 0);

[DllImport("Coredll")]
extern static int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

Once you have hIcon, you can use Icon.FromHandle to get a .NET Icon object
 
Thank you very much for your reply. Unfortunately the code doesn't
work very well. For example I cannot get the icon of my own application
when the icon is set in the Project Properties. I can only get the icon
when I set it in the Form Properties. As a result I cannot get the icon
of all other applications running on my pocket pc :(

I wonder if it is better to find the executable that launched the
window and get the icon from there using SHGetFileInfo(), which seems
to work very well. But it seems to be dificult.

I would really appreciate any other suggestions. Thanks again !
 
Thank you very much for your guidance. I think I am very close to the
solution but I still have a problem.

I get the process ID using: GetWindowThreadProcessId();

In order to use GetModuleFileName() I need the process handle instead
of the ID.

So I use OpenProcess(), but it always returns 0

This is an example of my code: (handle is the window handle)


string path = new string(' ', 255);
IntPtr ThreadID =GetWindowThreadProcessId(handle, IntPtr.Zero);

IntPtr processHwnd = OpenProcess(0, false, (int)ThreadID);

GetModuleFileName(processHwnd.ToInt32(), path, path.Length);

Is it that OpenProcess() is used in the msdn example so it should be
supported. I don't understand what is wrong ?
 
I forgot to mention that I also tried:

[DllImport("coredll.dll")]
private static extern IntPtr OpenProcess(uint flags, bool
fInherit, int PID);
private const uint PROCESS_ALL_ACCESS = 0x1F0FFF;


IntPtr processHwnd = OpenProcess(PROCESS_ALL_ACCESS, false,
(int)ThreadID);

But the result is the same :( Any help would be greately appreciated.
Thanks !
 
oops it was my error ! It works great now !

Thanks for pointing me to the right direction !
 
Back
Top