2017-04-03 109 views
0

我想要更改任务栏图标以在收到新邮件时收到类似Outlook的新邮件时通知用户。在部署的应用程序运行时更改任务栏图标

我已经搜索的网络解决方案,它是所有关于改变这样的窗口图标:

Uri iconUri = new Uri("Resources/envelop.ico", UriKind.Relative); 
this.Icon = BitmapFrame.Create(iconUri); 

它是在Visual Studio的工作很好,但我发现它没有改变我的任务栏上部署的应用程序,因为它是一个只读变量。更糟的是,它只是改变了我不想改变的左上角的图标。

那么有没有办法做到这一点? Outlook做到了,Chrome也是如此,所以一定有办法。

UPDATE

要强制对我的部署应用程序我要锁定/解锁图标我的任务栏的图标刷新,不幸的是这是一个用户命令只所以我不能在没有WPF开发一些这样做编程肮脏的举止会太不稳定。

其实我试图找到一种方法来刷新图标缓存,而不是为每个Windows操作系统或版本。

+0

您是否尝试过加入你的2个图标到你'你的项目Properties.Resources',然后设置'MainWindow.Icon'到? - 对我来说,这是工作.. –

+0

@FelixD。 “Properties.Resources”是什么意思?我在我的项目属性面板中的应用程序(选项卡) - >资源(部分) - >图标也许这阻止更改? – Safe

+0

在你的项目中展开'Properties'并双击'Resources.resx',然后你可以点击'Add Resource - > Add Existing File'并添加你的图标。这使得它们可以通过'YourProjectName.Properties.Resources.NameOfIcon'在代码中使用。它们会自动添加到项目中的资源文件夹中。确保将“BuildOption”更改为“Resource”。 –

回答

-1

对我来说,这个解决方案工作。

与代码Write text on bitmap in C#

private Icon DrawIcon(Brush brush) 
    { 
     //https://stackoverflow.com/questions/6311545/c-sharp-write-text-on-bitmap  
     Bitmap bmp = new Bitmap(MyNameSpace.Properties.Resources.desktop_windows_48px); 

     // Create a rectangle for the entire bitmap 
     RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height); 

     // Create graphic object that will draw onto the bitmap 
     Graphics g = Graphics.FromImage(bmp); 

     // The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing). One exception is that path gradient brushes do not obey the smoothing mode. Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property. 
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 

     // The interpolation mode determines how intermediate values between two endpoints are calculated. 
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 

     // Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object. 
     g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 

     // This one is important 
     g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; 

     // Draw the text onto the image 
     g.FillRectangle(brush, new Rectangle(2, 4, 20, 12)); 

     // Flush all graphics changes to the bitmap 
     g.Flush(); 

     Bitmap temp = bmp; 

     // Get an Hicon for myBitmap. 
     IntPtr Hicon = temp.GetHicon(); 

     // Create a new icon from the handle. 
     Icon newIcon = Icon.FromHandle(Hicon); 

     return newIcon;    
    } 

我也使用NotifyIcon但不会为你改变很多创建此。

_notifyIcon.Icon = DrawIcon(Brushes.Blue); 
MainWindow.Icon = Imaging.CreateBitmapSourceFromHIcon(_notifyIcon.Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 

Lateron我改变这样的时候才改变图标源(可能是类似

private void App_StateChanged(object sender, StateChangedEventArgs e) 
    { 
     if(e.State == ApplicationState.Defective) 
     { 
      //Show window on error ! 
      ShowMainWindow(); 
      //(sender as Window).Activate(); 
     } 
     _notifyIcon.Icon = DrawIcon(e.StateBrush); 
     MainWindow.Icon = Imaging.CreateBitmapSourceFromHIcon(_notifyIcon.Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
     _notifyIcon.Visible = true; 
    } 

它看起来与此类似:

默认:

enter image description here

出错:

enter image description here

希望这对您有所帮助!

-1

这是一个小的工作演示做这个工作!

添加图标:

enter image description here enter image description here

之所以选择2个图标(例如Outlook常用&展望MailReceived):

这对代码添加的您MainWindow

背后
public partial class MainWindow : Window 
{ 
    private int _imgId; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.Loaded += MainWindow_Loaded; 
    } 

    private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     //Start timer to periodically change the App Icon: 
     new System.Threading.Thread(() => 
     { 
      System.Timers.Timer timer = new System.Timers.Timer(); 
      timer.Interval = 100; 
      timer.AutoReset = true; 
      timer.Elapsed += Timer_Elapsed; 
      timer.Start(); 
     }).Start(); 
    } 

    private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     try 
     {  
      //Change AppIcon on UI-Thread   
      Dispatcher.Invoke(() => 
      { 
       /* CHANGE YOUR ICONS HERE !!! */ 
       BitmapSource ms_icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(Properties.Resources.Microsoft_logo.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
       BitmapSource so_icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(Properties.Resources.images.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
       if (_imgId == 0) 
       { 
        this.Icon = so_icon; 
        _imgId = 1; 
       } 
       else 
       { 
        this.Icon = ms_icon; 
        _imgId = 0; 
       } 
      }); 
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Trace.WriteLine(ex.Message);    
     }    
    } 
} 

会产生这样的输出(开关每100ms):

enter image description here

相关问题