[英]Autostart application and open it in taskbar with tray icon
我在.net中有一個應用程序,我希望它始終打開而無需任何手動交互。
在這個應用程序中,我使用了NotifyIcon
因此它始終在任務欄托盤中啟動,但通知圖標僅在我手動打開該.exe
。
所以我所做的就是在下面的幫助下將它添加到Autostart應用程序注冊表項中:
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
所以這工作正常,重啟后它成功打開系統任務欄進程列表,但不作為任務欄托盤圖標。
誰能幫我。?
我也在使用NotifyIcon,並且存在一些問題。 首先,您需要為NotifyIcon設置一個Icon,並確保除了Visibility.Visible之外沒有將其設置為Visibility。
然后,NotifyIcon只是NotifyIcon Windows API的包裝器,並且存在一個已知問題,即無法始終創建它。 因此,當您初始化NotifyIcon時,由於Windows中的錯誤, 它可能會拋出異常 (如果無法創建WinApi,則返回false,並且在源代碼中,它們會在那里拋出異常)。 當它這樣做時,您可以在循環中重新創建NotifyIcon,直到可以創建它。
我還在某個時候看到了一個問題,當時NotifyIcon沒有在app.xaml中創建為XAML對象,但是在代碼中,從那時起我總是在XAML而不是代碼中創建它。 此外,我現在從CodeProject導入了整個NotifyIcon項目,以便能夠調試它的內部。 所以現在我用這種方式創建它:
<NotifyIcon1:NotifyIcon x:Key="NotifyIcon" x:Name="notifyicon"
ToolTipText="" Visibility="Visible" IconSource="/Images/Icons/bulb.ico"/>
如果無法在NotifyIcon庫的代碼的這一部分中創建圖標,則應拋出異常:
/// <summary>
/// Creates the taskbar icon. This message is invoked during initialization,
/// if the taskbar is restarted, and whenever the icon is displayed.
/// </summary>
private void CreateTaskbarIcon()
{
lock (this)
{
if (!IsTaskbarIconCreated)
{
const IconDataMembers members = IconDataMembers.Message
| IconDataMembers.Icon
| IconDataMembers.Tip;
//write initial configuration
var status = Util.WriteIconData(ref iconData, NotifyCommand.Add, members);
if (!status)
{
throw new Win32Exception("Could not create icon data");
}
//set to most recent version
SetVersion();
messageSink.Version = (NotifyIconVersion) iconData.VersionOrTimeout;
IsTaskbarIconCreated = true;
}
}
}
您可以根據需要直接編輯代碼,也可以嘗試在出現異常時重新創建notifyicon。
我想這將是問題,因為它對我們來說是相同的,因為有時在啟動Windows尚未准備好創建圖標之后。 如果您有其他問題,請發布您用於創建發布問題的notifyicon和系統(XP?64bit?)的代碼。
我使用圖標的方式存在問題。
我們在NotifyIcon
使用的“icon”文件可能存在問題,所以我只是通過替換方式解決了這個問題
// START: Creating a red graphic instead of image
Bitmap b = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(b);
g.Clear(Color.Transparent);
SolidBrush sb = new SolidBrush(Color.Red);
g.FillEllipse(sb, 0, 0, 16, 16);
// END: Creating a red graphic instead of image
m_notifyicon.Visible = true;
m_notifyicon.Icon = Icon.FromHandle(b.GetHicon());
現在,即使重新啟動計算機,我也能看到紅色圖標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.