有没有办法让我的应用程序将其图标推送到其他应用程序?
问题描述:
我有一个启动另一个应用程序的WPF应用程序,我想为我的应用程序更改此第二个应用程序的图标。我可以使用GetWindowText
和SetWindowText
更改标题。是否有可能为Icon做到这一点?有没有办法让我的应用程序将其图标推送到其他应用程序?
更新
我无法控制第二个应用程序。
答
要更改其他应用程序的窗口标题:
的定义Win32 API函数和常量:
[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;
用法:
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);
答
在Windows窗体你可以使用
Icon ico = Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe");
this.Icon = ico;
所以即时猜测为WPF这将是类似的。
+0
我认为他想要设置的图标窗口外部的应用程序。 – 2012-02-08 18:57:11
+0
是的。我无法控制第二个应用程序。 – Nate 2012-02-08 19:01:18
是的,我已经成功地做到了这一点。我想更改左上角显示的图标。这可能通过Windows API吗? – Nate 2012-02-08 19:02:22
@Nate,是的,请参阅更新。 – 2012-02-08 19:07:55
这是非常好的,感谢张贴这个。但是我只想指出,这段代码只提供了一个(可能是)32x32图标,然后Windows会在需要16x16图像时下采样。如果可能,最好使用两次SendMessage提供一个真正的16x16图标以及32x32图标。看到这里例如:http://blog.barthe.ph/2009/07/17/wmseticon/ – RenniePet 2012-07-27 15:06:07