4

I have a WPF app that starts another application, I'd like for my application to change the Icon of this second app. I am able to use GetWindowText and SetWindowText to change the title. Is it possible to do this for the Icon as well?

update

I have no control of the second app.

Nate
  • 30,286
  • 23
  • 113
  • 184

2 Answers2

7

To change the window title of another application:

Definitions of Win32 API functions and constants:

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hwnd, int message, int wParam, IntPtr lParam);

private const int WM_SETICON = 0x80;
private const int ICON_SMALL = 0;
private const int ICON_BIG = 1;

Usage:

Process process = Process.Start("notepad");
// If you have just started a process and want to use its main window handle,
// consider using the WaitForInputIdle method to allow the process to finish starting,
// ensuring that the main window handle has been created.
// Otherwise, an exception will be thrown.
process.WaitForInputIdle();
SetWindowText(process.MainWindowHandle, "Hello!");
Icon icon = new Icon(@"C:\Icon\File\Path.ico");
SendMessage(process.MainWindowHandle, WM_SETICON, ICON_BIG, icon.Handle);
  • Yes, I am already doing this successfully. I would like to change the icon displayed at the top left. Is that possible through the windows api? – Nate Feb 08 '12 at 19:02
  • This is very good, thanks for posting this. But I'd just like to point out that this code provides a (presumably) 32x32 icon only, which Windows will then down-sample when it needs a 16x16 image. If possible, it is better to provide a true 16x16 icon as well as the 32x32 icon, using SendMessage twice. See here for example: http://blog.barthe.ph/2009/07/17/wmseticon/ – RenniePet Jul 27 '12 at 15:06
-1

In Windows Forms you would use

Icon ico = Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe");
this.Icon = ico;

So im guessing for WPF it would be similar.

craig1231
  • 3,769
  • 4
  • 31
  • 34