2012-02-08 76 views

回答

7

要更改其他應用程序的窗口標題:

的定義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); 
+0

是的,我已經成功地做到了這一點。我想更改左上角顯示的圖標。這可能通過Windows API嗎? – Nate 2012-02-08 19:02:22

+0

@Nate,是的,請參閱更新。 – 2012-02-08 19:07:55

+0

這是非常好的,感謝張貼這個。但是我只想指出,這段代碼只提供了一個(可能是)32x32圖標,然後Windows會在需要16x16圖像時下采樣。如果可能,最好使用兩次SendMessage提供一個真正的16x16圖標以及32x32圖標。看到這裏例如:http://blog.barthe.ph/2009/07/17/wmseticon/ – RenniePet 2012-07-27 15:06:07

-1

在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

相關問題