2017-04-03 108 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

相關問題