Greetings,
I have a windows forms app coded in c# for playing some music using the axWidowsMediaPplayer component. I am trying to add thumbnail toolbar buttons for playing and and pausing the player.
The buttons are being created but their events are not being fired or nothing's happening on clicking them .
Please Help I have killing myself over this for two days now with no luck. Please Help.
Here's My Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TagLib;
using System.Windows.Shell;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;
using System.Xml;
using System.Linq;
using Lastfm;
using Lastfm.Services;
using System.Web;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.WindowsAPICodePack.Taskbar;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.ApplicationServices;
using System.Windows.Interop;
namespace KryptSoft
{
public partial class Form1 : Form
{
static Icon B1Icon = new StockIcon(StockIconIdentifier.Shield).Icon;
static Icon B2Icon = new StockIcon(StockIconIdentifier.Users).Icon;
static Icon B3Icon = new StockIcon(StockIconIdentifier.Help).Icon;
private ThumbnailToolbarButton ads;
private ThumbnailToolbarButton ads2;
public Form1()
{
InitializeComponent();
textBox5.Visible = false;
button8.Visible = false;
selector.Visible = false;
textBox2.Visible = false;
p3.Visible = false;
textBox7.Visible = false;
label15.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void CreateTabbedThumbnail()
{
}
private void Form1_Shown(object sender, EventArgs e)
{
this.ads = new ThumbnailToolbarButton(this.Icon, "Next");
this.ads2 = new ThumbnailToolbarButton(this.Icon, "prev");
this.ads.Enabled = true;
this.ads.IsInteractive = true;
this.ads2.Enabled = true;
this.ads2.IsInteractive = true;
this.ads.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonNext_Click);
this.ads2.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonPrev_Click);
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(this.Handle, this.ads2, this.ads);
}
void thumbButtonPrev_Click(object sender, Microsoft.WindowsAPICodePack.Taskbar.ThumbnailButtonClickedEventArgs e)
{
player.Ctlcontrols.pause();
}
void thumbButtonNext_Click(object sender, Microsoft.WindowsAPICodePack.Taskbar.ThumbnailButtonClickedEventArgs e)
{
player.Ctlcontrols.play();
}
string[] files, files2, files3, path, path2, path3;
int counter = 1, c2;
private bool button1WasClicked = false;
private bool button7WasClicked = false;
private void button10_Click(object sender, EventArgs e)
{
try
{
playlist.Items.Clear();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files = openFileDialog1.SafeFileNames;
path = openFileDialog1.FileNames;
for (int i = 0; i < files.Length; i++)
{
playlist.Items.Add(files[i]);
}
playlist.SelectedIndex = 0;
player.URL = path[playlist.SelectedIndex];
player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);
if (player.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
playlist.SelectedIndex++;
player.URL = path[playlist.SelectedIndex];
}
}
}
catch (Exception)
{ }
}
private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
timer1.Interval = 100;
timer1.Enabled = true;
}
}
private void playlist_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
player.URL = path[playlist.SelectedIndex];
TagLib.File f1 = TagLib.File.Create(path[playlist.SelectedIndex]);
textBox3.Text = f1.Tag.Title;
textBox4.Text = GetAllStringsFromArrary(f1.Tag.AlbumArtists, ",");
textBox55.Text = f1.Tag.Album;
textBox6.Text = GetAllStringsFromArrary(f1.Tag.Performers, ",");
lyric.Clear();
}
catch (Exception)
{
MessageBox.Show("OOPS!! some error occurred. Please Restart The Application or Click Properly On The Song Name");
}
}
private void button5_Click(object sender, EventArgs e)
{
try
{
button1WasClicked = false;
button7WasClicked = false;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Mp3 Files | *.mp3";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
p2.Visible = false;
}
TagLib.File f2 = TagLib.File.Create(textBox1.Text);
title.Text = f2.Tag.Title;
artist.Text = GetAllStringsFromArrary(f2.Tag.AlbumArtists, ",");
albums.Text = f2.Tag.Album;
year.Text = f2.Tag.Year.ToString();
duration.Text = f2.Properties.Duration.ToString();
genre.Text = GetAllStringsFromArrary(f2.Tag.Genres, ",");
cartist.Text = GetAllStringsFromArrary(f2.Tag.Performers, ",");
if (f2.Tag.Pictures.Length > 0)
{
TagLib.IPicture pic = f2.Tag.Pictures[0];
System.Drawing.Image currentImage = null;
MemoryStream ms = new MemoryStream(pic.Data.Data);
if (ms != null && ms.Length > 4096)
{
currentImage = System.Drawing.Image.FromStream(ms);
p2.Image = currentImage;
p2.Visible = true;
}
ms.Close();
}
}
catch (Exception)
{ }
}
private void button6_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(this.textBox1.Text))
{
MessageBox.Show("Please Select A File");
}
else
{
string filePath = textBox1.Text;
bool isFileInUse;
isFileInUse = FileInUse(filePath);
if (isFileInUse)
{
MessageBox.Show("File is in use");
}
else
{
if ((button1WasClicked == false) && (button7WasClicked == false))
{
TagLib.File f = TagLib.File.Create(textBox1.Text);
f.Tag.Title = title.Text;
f.Tag.AlbumArtists = this.artist.Text.Split('\0');
f.Tag.Album = albums.Text;
f.Tag.Year = Convert.ToUInt32(year.Text);
f.Tag.Genres[0] = genre.Text;
f.Tag.Performers = this.cartist.Text.Split(',');
f.Save();
MessageBox.Show("applied");
p3.Image = null;
p2.Image = null;
ClearTextBoxes(this);
}
else if (button1WasClicked)
{
TagLib.File f = TagLib.File.Create(textBox1.Text);
IPicture[] pictures = new IPicture[1];
pictures[0] = new Picture(textBox7.Text + "\\" + c2.ToString() + "####" + c2.ToString() + ".png");
f.Tag.Pictures = pictures;
f.Tag.Title = title.Text;
f.Tag.AlbumArtists = this.artist.Text.Split(',');
f.Tag.Album = albums.Text;
f.Tag.Year = Convert.ToUInt32(year.Text);
f.Tag.Genres = this.genre.Text.Split(',');
f.Tag.Performers = this.cartist.Text.Split(',');
f.Save();
deleter();
MessageBox.Show("applied");
p3.Image = null;
p2.Image = null;
ClearTextBoxes(this);
}
else if (button7WasClicked)
{
TagLib.File f = TagLib.File.Create(textBox1.Text);
IPicture[] pictures = new IPicture[1];
pictures[0] = new Picture(textBox2.Text);
f.Tag.Pictures = pictures;
f.Tag.Title = title.Text;
f.Tag.AlbumArtists = this.artist.Text.Split(',');
f.Tag.Album = albums.Text;
f.Tag.Year = Convert.ToUInt32(year.Text);
f.Tag.Genres = this.genre.Text.Split(',');
f.Tag.Performers = this.cartist.Text.Split(',');
f.Save();
MessageBox.Show("Applied");
p3.Image = null;
p2.Image = null;
ClearTextBoxes(this);
}
else
{
p3.Image = null;
p2.Image = null;
ClearTextBoxes(this);
MessageBox.Show("Nothing Done");
}
}
}
button8.Visible = false;
selector.Visible = false;
textBox2.Visible = false;
p3.Visible = false;
label15.Visible = false;
}
catch (Exception)
{ }
}
bool FileInUse(string path)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
if (fs.CanWrite)
{ }
}
return false;
}
catch (IOException)
{
return true;
}
}
public void ClearTextBoxes(Control control)
{
foreach (Control c in control.Controls)
{
if ((c is System.Windows.Forms.TextBox) && (c.Name != "textBox7"))
{
((System.Windows.Forms.TextBox)c).Clear();
}
if (c.HasChildren)
{
ClearTextBoxes(c);
}
}
}
public string GetAllStringsFromArrary(string[] strArray, string strDelimeter)
{
string strFinal = string.Empty;
for (int i = 0; i < strArray.Length; i++)
{
strFinal += strArray[i];
if (i != strArray.Length - 1)
{
strFinal += strDelimeter;
}
}
return strFinal;
}
private void button8_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
if (ofd1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = ofd1.FileName;
}
Image image = Image.FromFile(textBox2.Text);
p3.Image = image;
}
catch (Exception)
{ }
}
private void button7_Click(object sender, EventArgs e)
{
try
{
button8.Visible = true;
selector.Visible = true;
textBox2.Visible = true;
p3.Visible = true;
button7WasClicked = true;
button1WasClicked = false;
}
catch (Exception)
{ }
}
private void button9_Click_1(object sender, EventArgs e)
{
try
{
p3.Image = null;
p2.Image = null;
ClearTextBoxes(this);
button8.Visible = false;
selector.Visible = false;
textBox2.Visible = false;
p3.Visible = false;
label15.Visible = false;
deleter();
}
catch (Exception)
{ }
}
private static string slice(string source, int start, int end)
{
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
return source.Substring(start, len);
}
private static string sanitize(string s)
{
char[] array = s.Trim().ToCharArray();
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array).Trim().Replace(' ', '_');
}
private string pullLyrics(string strArtist, string strSongTitle)
{
try
{
if ((string.IsNullOrWhiteSpace(this.textBox3.Text)) || ((string.IsNullOrWhiteSpace(this.textBox3.Text)) && (string.IsNullOrWhiteSpace(this.textBox3.Text))))
{
MessageBox.Show("No Title or Artists Listed");
return null;
}
else
{
WebClient wc = new WebClient();
string sLyrics = null;
string sUrl = null;
int iStart = 0;
int iEnd = 0;
sUrl = @"http://lyrics.wikia.com/index.php?title=" + sanitize(strArtist) + ":" + sanitize(strSongTitle) + "&action=edit";
wc.Encoding = Encoding.UTF8;
sLyrics = wc.DownloadString(sUrl);
iStart = sLyrics.IndexOf("<lyrics>") + 12;
iEnd = sLyrics.IndexOf("</lyrics>") - 1;
sLyrics = slice(sLyrics, iStart, iEnd).Replace("\n", Environment.NewLine).TrimEnd();
if (sLyrics.Contains("#REDIRECT"))
{
iStart = sLyrics.IndexOf("#REDIRECT [[") + 12;
iEnd = sLyrics.IndexOf("]]", iStart);
strArtist = slice(sLyrics, iStart, iEnd).Split(':')[0];
strSongTitle = slice(sLyrics, iStart, iEnd).Split(':')[1];
pullLyrics(strArtist, strSongTitle);
}
else if (sLyrics.Contains("!-- PUT LYRICS HERE (and delete this entire line) -->"))
sLyrics = "Lyrics not found.";
return sLyrics;
}
}
catch (Exception)
{ return null; }
}
private void lyrics_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(this.textBox4.Text)))
{
lyric.Text = pullLyrics(textBox6.Text.ToString(), textBox3.Text.ToString());
}
else
lyric.Text = pullLyrics(textBox4.Text.ToString(), textBox3.Text.ToString());
}
public static string AbsUrlOfArt(string album, string artist)
{
string ApiKey = "2c5ae3d75d62bab28cd42352e3b22ede";
string LastFMLogin = "kryptonian1111";
Lastfm.Services.Session session = new Lastfm.Services.Session(ApiKey, LastFMLogin);
Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(artist, session);
Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, album, session);
try
{
return lAlbum.GetImageURL(AlbumImageSize.ExtraLarge);
}
catch (Exception)
{
return lArtist.GetImageURL(ImageSize.Large);
}
}
public static System.Drawing.Image AlbumArt(string album, string artist)
{
Stream stream = null;
try
{
WebRequest req = WebRequest.Create(AbsUrlOfArt(album, artist));
WebResponse response = req.GetResponse();
stream = response.GetResponseStream();
Image img = Image.FromStream(stream);
return img;
}
catch (Exception)
{
return null;
}
finally
{
if (stream != null)
stream.Dispose();
}
}
public static class HtmlRemoval
{
public static string StripTagsRegex(string source)
{
return Regex.Replace(source, "<.*?>", string.Empty);
}
static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
public static string StripTagsRegexCompiled(string source)
{
return _htmlRegex.Replace(source, string.Empty);
}
public static string StripTagsCharArray(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
label15.Visible = true;
textBox7.Visible = false;
textBox7.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if ((string.IsNullOrWhiteSpace(this.textBox7.Text)))
{
MessageBox.Show("Please Select A Location To Save The File");
}
else
{
string ApiKey = "2c5ae3d75d62bab28cd42352e3b22ede";
string LastFMLogin = "kryptonian1111";
p3.Visible = true;
button7WasClicked = false;
button1WasClicked = true;
if ((string.IsNullOrWhiteSpace(this.albums.Text)) && (string.IsNullOrWhiteSpace(this.artist.Text)) && (string.IsNullOrWhiteSpace(this.cartist.Text)))
{
MessageBox.Show("No Artist Or Album Listed");
}
else
{
if (string.IsNullOrWhiteSpace(this.artist.Text))
{
string x = textBox7.Text + "\\" + counter.ToString() + "####" + counter.ToString() + ".png";
Lastfm.Services.Session session = new Lastfm.Services.Session(ApiKey, LastFMLogin);
Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(cartist.Text, session);
Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, albums.Text, session);
textBox5.Text = AbsUrlOfArt(albums.Text, cartist.Text);
System.Net.WebClient Client = new System.Net.WebClient();
Client.DownloadFile(AbsUrlOfArt(albums.Text, cartist.Text), x);
p3.ImageLocation = x;
}
else
{
string x = textBox7.Text + "\\" + counter.ToString() + "####" + counter.ToString() + ".png";
Lastfm.Services.Session session = new Lastfm.Services.Session(ApiKey, LastFMLogin);
Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(artist.Text, session);
Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, albums.Text, session);
textBox5.Text = AbsUrlOfArt(albums.Text, artist.Text);
System.Net.WebClient Client = new System.Net.WebClient();
Client.DownloadFile(AbsUrlOfArt(albums.Text, artist.Text), x);
p3.ImageLocation = x;
}
}
c2 = counter;
counter = counter + 1;
}
}
catch (Exception)
{
MessageBox.Show("Some Error Occured.\n Please Check Your Album Name, Artist Name and Contributing Artists. \nSee To It That The Entries Are Valid.");
}
}
public void deleter()
{
string x = textBox7.Text + "\\" + c2.ToString() + "####" + c2.ToString() + ".png";
System.IO.File.Delete(x);
}
private void saveloc_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
location.Text = folderBrowserDialog1.SelectedPath;
}
}
private void Select_Click(object sender, EventArgs e)
{
pl.Items.Clear();
if (openFileDialog3.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files3 = openFileDialog3.SafeFileNames;
path3 = openFileDialog3.FileNames;
for (int i = 0; i < files3.Length; i++)
{
pl.Items.Add(files3[i]);
}
}
pl.SelectedIndex = 0;
}
private void pl_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
TagLib.File f2 = TagLib.File.Create(path3[pl.SelectedIndex]);
textBox11.Text = (path3[pl.SelectedIndex]);
title2.Text = f2.Tag.Title;
artist2.Text = GetAllStringsFromArrary(f2.Tag.AlbumArtists, ",");
album2.Text = f2.Tag.Album;
cartist2.Text = GetAllStringsFromArrary(f2.Tag.Performers, ",");
}
catch (Exception)
{
MessageBox.Show("OOPS!! some error occurred. Please Restart The Application or Click Properly On The Song Name");
}
}
private void Search_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(this.artist2.Text)) && (string.IsNullOrWhiteSpace(this.title2.Text)) && (string.IsNullOrWhiteSpace(this.album2.Text)) && (string.IsNullOrWhiteSpace(this.cartist2.Text)))
{
MessageBox.Show("Please Select a File First");
}
else
{
if ((string.IsNullOrWhiteSpace(this.artist2.Text)))
{
lyric2.Text = pullLyrics2(cartist2.Text.ToString(), title2.Text.ToString());
}
else
lyric2.Text = pullLyrics2(artist2.Text.ToString(), title2.Text.ToString());
}
}
private void Generate_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(this.location.Text)))
{
MessageBox.Show("Please Select A Location To Save The File");
}
else if ((string.IsNullOrWhiteSpace(this.artist2.Text)) && (string.IsNullOrWhiteSpace(this.title2.Text)) && (string.IsNullOrWhiteSpace(this.album2.Text)) && (string.IsNullOrWhiteSpace(this.cartist2.Text)))
{
MessageBox.Show("Please Select a File First");
}
else
{
if ((string.IsNullOrWhiteSpace(this.artist2.Text)))
{
using (StreamWriter sw1 = new StreamWriter(location.Text + "\\" + cartist2.Text + "####" + title2.Text + ".zpl"))
{
sw1.WriteLine(lyric2.Text);
}
}
else
{
using (StreamWriter sw1 = new StreamWriter(location.Text + "\\" + artist2.Text + "####" + title2.Text + ".zpl"))
{
sw1.WriteLine(lyric2.Text);
}
}
}
}
private string pullLyrics2(string strArtist, string strSongTitle)
{
try
{
if ((string.IsNullOrWhiteSpace(this.title2.Text)) || ((string.IsNullOrWhiteSpace(this.artist2.Text)) && (string.IsNullOrWhiteSpace(this.cartist2.Text))))
{
MessageBox.Show("No Title or Artists Listed");
return null;
}
else
{
WebClient wc = new WebClient();
string sLyrics = null;
string sUrl = null;
int iStart = 0;
int iEnd = 0;
sUrl = @"http://lyrics.wikia.com/index.php?title=" + sanitize(strArtist) + ":" + sanitize(strSongTitle) + "&action=edit";
wc.Encoding = Encoding.UTF8;
sLyrics = wc.DownloadString(sUrl);
iStart = sLyrics.IndexOf("<lyrics>") + 12;
iEnd = sLyrics.IndexOf("</lyrics>") - 1;
sLyrics = slice(sLyrics, iStart, iEnd).Replace("\n", Environment.NewLine).TrimEnd();
if (sLyrics.Contains("#REDIRECT"))
{
iStart = sLyrics.IndexOf("#REDIRECT [[") + 12;
iEnd = sLyrics.IndexOf("]]", iStart);
strArtist = slice(sLyrics, iStart, iEnd).Split(':')[0];
strSongTitle = slice(sLyrics, iStart, iEnd).Split(':')[1];
pullLyrics(strArtist, strSongTitle);
}
else if (sLyrics.Contains("!-- PUT LYRICS HERE (and delete this entire line) -->"))
sLyrics = "Lyrics not found.";
return sLyrics;
}
}
catch (Exception )
{ return null; }
}
private void lyric_TextChanged(object sender, EventArgs e)
{
}
private void player_Enter(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (playlist.SelectedIndex < files.Count() - 1)
{
playlist.SelectedIndex++;
timer1.Enabled = false;
}
else
{
playlist.SelectedIndex = 0;
timer1.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
player.Ctlcontrols.pause();
}
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
}
}
}