這是在通知圖標上呈現(更新)文本的腳本。
如你所願自定義「Get-NotifyIconText」函數。
#Requires -Version 3.0
function Get-NotifyIconText {
[DateTime]::Now.Second.ToString()
# ((Get-Counter '\\mypc\network interface(Intel[R] 82579V Gigabit Network Connection)\bytes total/sec').countersamples).cookedvalue*8/102400000*100
}
Add-Type -ReferencedAssemblies @("System.Windows.Forms"; "System.Drawing") -TypeDefinition @"
using System;
using System.Drawing;
using System.Windows.Forms;
public static class TextNotifyIcon
{
// it's difficult to call DestroyIcon() with powershell only...
[System.Runtime.InteropServices.DllImport("user32")]
private static extern bool DestroyIcon(IntPtr hIcon);
public static NotifyIcon CreateTrayIcon()
{
var notifyIcon = new NotifyIcon();
notifyIcon.Visible = true;
return notifyIcon;
}
public static void UpdateIcon(NotifyIcon notifyIcon, string text)
{
using (var b = new Bitmap(16, 16))
using (var g = Graphics.FromImage(b))
using (var font = new Font(FontFamily.GenericMonospace, 8))
{
g.DrawString(text, font, Brushes.Black, 0, 0);
var icon = b.GetHicon();
try
{
notifyIcon.Icon = Icon.FromHandle(icon);
} finally
{
DestroyIcon(icon);
}
}
}
}
"@
$icon = [TextNotifyIcon]::CreateTrayIcon()
while ($true) {
$text = Get-NotifyIconText
[TextNotifyIcon]::UpdateIcon($icon, $text)
[Threading.Thread]::Sleep(1000)
}
你需要創建一個WinFroms GUI,所以你可以使用['NotifyIcon']實例(http://msdn.microsoft.com/en-us/library/system.windows.forms .notifyicon.aspx)。然後使用計時器獲取計數器的新值並更新圖標。 – Richard
@Richard我不太熟悉.Net編程。我是否需要編寫和編譯諸如[this](http://www.codeproject.com/Articles/74/Adding-Icons-to-the-System-Tray)? –
看看[這篇博客文章](http://www.sapien.com/blog/2012/05/08/spotlight-on-the-notifyicon-control/)。 –