From 9fef93d8d8408be963e68a7fc193a5f3a054aa53 Mon Sep 17 00:00:00 2001 From: Sam Haddad Date: Sat, 29 Dec 2012 02:30:48 -0500 Subject: [PATCH 1/2] Added album art to thumbnail preview. --- FrmMain.cs | 124 ++++++++++++++++++++++++++++++++------------ Utils/ImageUtils.cs | 45 ++++++++++++++++ 2 files changed, 137 insertions(+), 32 deletions(-) create mode 100644 Utils/ImageUtils.cs diff --git a/FrmMain.cs b/FrmMain.cs index 546f40c..cb4d7f7 100644 --- a/FrmMain.cs +++ b/FrmMain.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Collections; using System.ComponentModel; +using System.Net; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; @@ -9,6 +10,7 @@ using Microsoft.WindowsAPICodePack; using Microsoft.WindowsAPICodePack.Controls; using Microsoft.WindowsAPICodePack.Taskbar; +using WinGrooves.Utils; namespace WinGrooves { @@ -74,6 +76,9 @@ public class FrmMain : System.Windows.Forms.Form private bool isbuttonPaused, isMusicPlaying; private ThumbnailToolbarButton buttonPause; private ThumbnailToolbarButton buttonNext; + private TabbedThumbnail _customThumbnail; //Taskbar image icon + + private string _cachedSongTitle; //Used to tell if song has changed public FrmMain() { @@ -477,16 +482,24 @@ private void FrmMain_Load(object sender, System.EventArgs e) //Thumbnail buttons for win7 users if (TaskbarManager.IsPlatformSupported) { + + //Add a thumbnail image during peak + _customThumbnail = new TabbedThumbnail(this.Handle, this.Handle); + TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(_customThumbnail); + + buttonPrev.Click += new EventHandler(Previous_Click); buttonPause.Click += new EventHandler(Play_Click); buttonNext.Click += new EventHandler(Next_Click); - + //Add the buttons (kinda of ugly tough) ThumbnailToolbarButton[] buttonList = new ThumbnailToolbarButton[3]; buttonList[0] = buttonPrev; buttonList[1] = buttonPause; buttonList[2] = buttonNext; TaskbarManager.Instance.ThumbnailToolbars.AddButtons(this.Handle, buttonList); + } } + /* * Simulates a browser click on an html element /// the HTML id of the element to click on/param> @@ -723,48 +736,61 @@ private void currentSongTimer_Tick(object sender, EventArgs e) } object songTitle = webBrowser1.Document.InvokeScript("getSongTitle"); - object songArtist = webBrowser1.Document.InvokeScript("getSongArtist"); - //set the Windows title - if (songTitle.ToString().Length > 0) + + + //Since the song title changed we must update all the information + if (String.IsNullOrEmpty(_cachedSongTitle) || _cachedSongTitle != songTitle.ToString()) { - this.Text = songTitle + " - " + songArtist + " - WinGrooves"; - //set the tray icon text if it is less than 63 characters (the max allowed) - if ((songTitle.ToString().Length + songArtist.ToString().Length + 3) < 63) - { - notifyIcon1.Text = songTitle + " - " + songArtist; - } - else + //cache new song title so we can tell if it changes again + _cachedSongTitle = songTitle.ToString(); + + + //Update thumbnail with album art cover + UpdateThumbnail(); + + //Set the Windows title + object songArtist = webBrowser1.Document.InvokeScript("getSongArtist"); + + if (songTitle.ToString().Length > 0) { - try // Get what you can up to max length. + this.Text = songTitle + " - " + songArtist + " - WinGrooves"; + //set the tray icon text if it is less than 63 characters (the max allowed) + if ((songTitle.ToString().Length + songArtist.ToString().Length + 3) < 63) { - notifyIcon1.Text = (songTitle + " - " + songArtist).Substring(0, 62); + notifyIcon1.Text = songTitle + " - " + songArtist; } - catch // Possible you land right on and under, throwing exception. handle with old fallback. + else { - notifyIcon1.Text = ("WinGrooves"); + try // Get what you can up to max length. + { + notifyIcon1.Text = (songTitle + " - " + songArtist).Substring(0, 62); + } + catch + // Possible you land right on and under, throwing exception. handle with old fallback. + { + notifyIcon1.Text = ("WinGrooves"); + } } } - } - //control thumbail icons - if (TaskbarManager.IsPlatformSupported) - { - //the element class of the play button on grooveshark changes according to the music state (contains play/paused/nothing) - //I can't figure a better way to control the thumbnail states. - if (Convert.ToBoolean(webBrowser1.Document.InvokeScript("getMusicState"))) - { - buttonPause.Icon = Properties.Resources.PlayerPause; - isbuttonPaused = false; - } - else + //control thumbail icons + if (TaskbarManager.IsPlatformSupported) { - buttonPause.Icon = Properties.Resources.PlayerPlay; - isbuttonPaused = true; + //the element class of the play button on grooveshark changes according to the music state (contains play/paused/nothing) + //I can't figure a better way to control the thumbnail states. + if (Convert.ToBoolean(webBrowser1.Document.InvokeScript("getMusicState"))) + { + buttonPause.Icon = Properties.Resources.PlayerPause; + isbuttonPaused = false; + } + else + { + buttonPause.Icon = Properties.Resources.PlayerPlay; + isbuttonPaused = true; + } + } } - - - // HandleThumbnailButtons(); } catch (NullReferenceException) { @@ -829,5 +855,39 @@ private void Dislike_Click(object sender, EventArgs e) { DislikeCurrentSong(); } + + #region Thumbnail Methods + + /// + /// Updates the thumbnail on the Taskbar in Windows 7 + /// + private void UpdateThumbnail() + { + + var url = webBrowser1.Document.GetElementById("now-playing-image").GetAttribute("src"); + + if (String.IsNullOrEmpty(url) || url.EndsWith("40_album.png")) + { + //there is no album art for this song so lets just redraw the application. + _customThumbnail.SetImage(ImageUtils.ApplicationThumbnail(this)); + } + else + { + //Calculate Album Art URL + string urlBase = url.Substring(0, url.LastIndexOf("/") + 1); + string fileName = url.Substring(url.LastIndexOf("/") + 1); + + if (fileName.StartsWith("40_")) + { + //Build the url for the 120 pixel album art + fileName = String.Format("120{0}", fileName.Substring(fileName.IndexOf("_"))); + url = urlBase + fileName; + } + Bitmap albumPreview = ImageUtils.BitmapFromUrl(url); + _customThumbnail.SetImage(albumPreview ?? ImageUtils.ApplicationThumbnail(this)); + } + } + #endregion + } } \ No newline at end of file diff --git a/Utils/ImageUtils.cs b/Utils/ImageUtils.cs new file mode 100644 index 0000000..6f576e2 --- /dev/null +++ b/Utils/ImageUtils.cs @@ -0,0 +1,45 @@ +using System; +using System.Drawing; +using System.Net; + +namespace WinGrooves.Utils +{ + public static class ImageUtils + { + /// + /// Retrieves the image at the url. + /// + /// url of image. + /// Returns image as bitmap, or null if not available. + public static Bitmap BitmapFromUrl(string url) + { + if (String.IsNullOrEmpty(url)) + { + return null; + } + var request = WebRequest.Create(url); + + using (var response = request.GetResponse()) + using (var stream = response.GetResponseStream()) + { + if (stream == null) return null; + return (Bitmap)Image.FromStream(stream); + } + } + + /// + /// Returns a bitmap of the current form. + /// + /// Returns a bitmap of the current form + public static Bitmap ApplicationThumbnail(System.Windows.Forms.Form form) + { + Graphics myGraphics = form.CreateGraphics(); + Size s = form.Size; + Bitmap memoryImage = new Bitmap(s.Width, s.Height, myGraphics); + Graphics memoryGraphics = Graphics.FromImage(memoryImage); + memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, s); + return memoryImage; + } + + } +} From 2fb6bff5459f03fab52e1e1d285378fb9262301c Mon Sep 17 00:00:00 2001 From: Sam Haddad Date: Sat, 29 Dec 2012 02:31:00 -0500 Subject: [PATCH 2/2] Updated refreances to project would build --- WinGrooves.csproj | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/WinGrooves.csproj b/WinGrooves.csproj index ba84add..2420c45 100644 --- a/WinGrooves.csproj +++ b/WinGrooves.csproj @@ -119,6 +119,13 @@ True + + WindowsAPICodePack\Microsoft.WindowsAPICodePack.dll + + + WindowsAPICodePack\Microsoft.WindowsAPICodePack.Shell.dll + + System @@ -134,6 +141,7 @@ System.XML + @@ -165,6 +173,7 @@ Settings.settings + AboutBox1.cs