項目名稱:KTV點歌系統 --指導老師原玉明 經過一周多的時間,終於完成的我的這個KTV項目,說實話,做這個項目我收穫了不少,難點也非常多,不過經過重重磨難,總算是過來了。不簡單呀! 這個項目主要分為前後臺。 前臺主要實現的功能是:歌星點歌,拼音點歌,字數點歌, 類型選擇,金曲排行,一些切歌,重唱,
項目名稱:KTV點歌系統
--指導老師原玉明
經過一周多的時間,終於完成的我的這個KTV項目,說實話,做這個項目我收穫了不少,難點也非常多,不過經過重重磨難,總算是過來了。不簡單呀!
這個項目主要分為前後臺。
前臺主要實現的功能是:歌星點歌,拼音點歌,字數點歌,
類型選擇,金曲排行,一些切歌,重唱,已點歌曲列表,主要是這些功能。
後臺主要實現的功能是:增加歌手,增加歌曲,修改歌曲路徑,修改歌手圖片路徑。
首先先看前臺:
前臺核心類:
SqlHelper類(負責連接資料庫):
//連接字元串 public static string str = "Data Source=HYJ-PC;Initial Catalog=MyKTV;User ID=sa;pwd=123";
KtvHelper類(保存歌曲的目錄;保存歌手圖片的目錄)
//保存歌曲的目錄 public static string songURL = ""; //保存歌手圖片的目錄 public static string singer_photoURL = ""; FrmByValue類(窗體對象之間的傳值) public static frmsonglist frmsl; public static FrmMain frM; public static SelectedSong ss;
Song類(歌曲的信息及實現歌曲狀態的一個切換(已播放;未播放;重播;切歌)):
//定義一個枚舉:歌曲播放狀態 public enum SongPlayState { unplayed, played, newplayed, cut } public class Song { public string SongName { get { return songName; } set { songName = value; } } public string SongURL { get { return songURL; } set { songURL = value; } } internal SongPlayState Playseat { get { return playseat; } set { playseat = value; } } private string songName;//歌曲名稱欄位 private string songURL;//歌曲存放路徑欄位 //讓歌曲的播放狀態預設為未播放; private SongPlayState playseat = SongPlayState.unplayed; //把歌曲狀態改為已播放 public void SetSongPlayed() { this.playseat = SongPlayState.played; } //把歌曲狀態改為重唱 public void SetnewSong() { this.playseat = SongPlayState.newplayed; } //把歌曲狀態改為一切歌 public void Setcut() { this.playseat = SongPlayState.cut; }
PlayList類(播放歌曲的核心內容,功能(實現播放,切歌,重唱)):
public class PlayList { //定義一個數組,預設可以儲存50首歌 public static Song[] songList=new Song[50];//歌曲播放列表數組 public static int songIndex;//當前播放的歌曲在數組的索引 #region 添加播放歌曲 public static bool AddSong(Song song) { bool happy = false; for (int i = 0; i < songList.Length; i++) { if (songList[i] == null) { songList[i] = song; happy = true; break; } } return happy; } #endregion #region 當前播放的歌曲名稱 public static string PlayingSongName() { string songName = ""; // 歌曲名稱 if(songList.Length>songIndex) { if (songList[songIndex] != null) { songName = songList[songIndex].SongName; } } return songName; } #endregion #region 獲取當前播放的歌曲 public static Song GetPlayingSong() { if(songIndex<songList.Length) { if (songList[songIndex] != null) { return songList[songIndex]; } else { return null; } } return null; } #endregion #region 下一首要播放的歌曲名稱 public static string NextSongName() { string songName = ""; // 歌曲名稱 if(songIndex<songList.Length) { if (songList.Length > songIndex + 1) { if (songList[songIndex + 1] != null) { songName = songList[songIndex + 1].SongName; } } } return songName; } #endregion #region 切歌: public static void CutSong() { //代表切歌的位置 int i = songIndex; if (songList[i]!=null) { songList[i].Setcut(); } while (songList[i] != null) { songList[i] = songList[i + 1]; i++; // 如果到達數組最後一個元素,就將最後一個元素指向空 if (i == songList.Length) { songList[i] = null; } } if (songList[0] != null) { songList[0].Playseat = SongPlayState.played; } } #endregion #region 重放當前歌曲 public static void PlayAgain() { if (songList[songIndex] != null) { songList[songIndex].SetnewSong(); } } #endregion #region 播放下一首 public static void MoveOn() { if(songIndex<songList.Length) { if (songList[songIndex] != null && songList[songIndex].Playseat == SongPlayState.newplayed) { songList[songIndex].SetSongPlayed(); } else { songIndex++; } } } #endregion }
實現窗體拖動的代碼(找到對應事件雙擊):
#region 讓窗體實現拖動 public Point mouseOffset; //記錄滑鼠指針的坐標 public bool isMouseDown = false; //記錄滑鼠按鍵是否按下 private void FrmMain_MouseDown(object sender, MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X - SystemInformation.FrameBorderSize.Width; yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void FrmMain_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X + 5, mouseOffset.Y + 30); Location = mousePos; } } private void FrmMain_MouseUp(object sender, MouseEventArgs e) { // 修改滑鼠狀態isMouseDown的值 // 確保只有滑鼠左鍵按下並移動時,才移動窗體 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } #endregion
主界面:(FrmMain)
//當前播放的歌曲 public Song songname; #region 讓窗體實現拖動 public Point mouseOffset; //記錄滑鼠指針的坐標 public bool isMouseDown = false; //記錄滑鼠按鍵是否按下 private void FrmMain_MouseDown(object sender, MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X - SystemInformation.FrameBorderSize.Width; yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void FrmMain_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X + 5, mouseOffset.Y + 30); Location = mousePos; } } private void FrmMain_MouseUp(object sender, MouseEventArgs e) { // 修改滑鼠狀態isMouseDown的值 // 確保只有滑鼠左鍵按下並移動時,才移動窗體 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } #endregion // 點擊退出按鈕觸發的事件 private void btnexit_Click(object sender, EventArgs e) { Application.Exit(); } // Login事件 private void FrmMain_Load(object sender, EventArgs e) { //把整個窗體對象賦給FrmByValue的靜態frM對象 FrmByValue.frM = this; //運行窗體時啟動定時器 this.tilist_played.Start(); //調用查詢返迴路徑的方法,傳入SQL語句 // 歌曲路徑 string sql = "select resource_path from Resource_path where resource_id=1"; KtvHelper.songURL = song_path(sql); // 歌手圖片路徑 string sql1 = "select resource_path from Resource_path where resource_id=2"; KtvHelper.singer_photoURL = song_path(sql1); } /// <summary> /// 查詢返迴路徑的方法 /// </summary> /// <param name="sql">傳入sql語句</param> /// <returns>根據傳入的sql語句返回不同的路徑(1.resource_id=1歌曲路徑:resource_id=2歌手圖片路徑)</returns> private string song_path(string sql) { SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); string path = ""; try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { path = dr["resource_path"].ToString(); } } } } catch (Exception) { MessageBox.Show("網路異常!"); } finally { con.Close(); } return path; } //點擊顯示播放觸發的事件 private void btnshow_Click(object sender, EventArgs e) { if (this.Width == 567) { this.Width = this.Width + 208; btnshow.Text = "隱 藏 播 放"; } else if (this.Width > 567) { this.Width = 567; btnshow.Text = "顯 示 播 放"; } } //已點歌曲窗體 private void btnopen_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); this.Hide(); frm.Show(); } // 點擊拼音點歌觸發的事件 private void btnspell_Click(object sender, EventArgs e) { frmbySongname frm = new frmbySongname(); this.Hide(); frm.Show(); } //點擊字數點歌觸發的事件 private void btnnum_Click(object sender, EventArgs e) { frmbyziSong frm = new frmbyziSong(); this.Hide(); frm.Show(); } //點擊類型點歌觸發的事件 private void btntype_Click(object sender, EventArgs e) { frmbyTypesong frm = new frmbyTypesong(); this.Hide(); frm.Show(); } //定時掃描歌曲列表,顯示當前播放歌曲的名稱 public void ShowPlayingSongName() { txtopen.Text = PlayList.PlayingSongName(); txtnext.Text = PlayList.NextSongName(); } //播放歌曲的方法 public void PlaySong() { // 獲取當前要播放的歌曲 this.songname = PlayList.GetPlayingSong(); if (songname != null) { #region 播放時顯示歌曲圖片 string name = songname.SongName; string sql = "select singer_id from song_info where song_name ='" + name + "' "; SqlConnection con = new SqlConnection(SqlHelper.str); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); int singerid = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); string sql1 = "select singer_photo from singer_info where singer_id=" + singerid + ""; SqlConnection con1 = new SqlConnection(SqlHelper.str); con1.Open(); SqlCommand cmd1 = new SqlCommand(sql1, con1); string singer_photo = cmd1.ExecuteScalar().ToString(); con1.Close(); plist.Image = Image.FromFile(KtvHelper.singer_photoURL + singer_photo); #endregion // 將當前歌曲播放狀態設為已播放 this.songname.SetSongPlayed(); // 得到當前播放歌曲的路徑 Playerlist.URL = KtvHelper.songURL + songname.SongURL; } } //計時器控制項的Tick事件 private void tilist_played_Tick(object sender, EventArgs e) { ShowPlayingSongName(); if(this.songname==null) { this.PlaySong(); } if (this.Playerlist.playState == WMPLib.WMPPlayState.wmppsStopped) { this.songname = null; PlayList.MoveOn(); } if(this.songname!=null&&this.songname.Playseat==SongPlayState.cut) { this.Playerlist.URL = ""; this.songname = null; } if (this.songname != null && this.songname.Playseat == SongPlayState.newplayed) { this.PlaySong(); } } //點擊切歌觸發的事件 private void btnnextsong_Click(object sender, EventArgs e) { if (this.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("親!你還沒有添加歌曲"); } } //點擊重唱觸發的事件 private void btnnew_Click(object sender, EventArgs e) { if (this.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("親!你還沒有添加歌曲"); } } //點擊金曲排行觸發的時間 private void btnorderby_Click(object sender, EventArgs e) { frmsonglist frm = new frmsonglist(); frm.ph = 1; this.Hide(); frm.Show(); } //歌星點歌 private void btnswan_Click_1(object sender, EventArgs e) { Frmbyswangetsong frm = new Frmbyswangetsong(); frm.Show(); this.Hide(); } //當滑鼠進入控制項可見狀態是觸發的事件 private void btnswan_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnswan,"歌星點歌"); } private void btnspell_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnspell, "拼音點歌"); } private void btnnum_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnum, "字數點歌"); } private void btntype_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btntype, "類型選擇"); } private void btnorderby_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnorderby, "金曲排行"); } private void btnnew_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnew, "重唱"); } private void btnnextsong_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnextsong, "切歌"); } private void btnopen_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnopen, "已點"); } private void btnfw_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnfw, "服務"); } private void btnexit_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnexit, "退出"); } private void btnfw_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服務..."); }
歌星點歌(Frmbyswangetsong):
//圖片索引 public int index=0; public int index_singer = 0; //Load事件 private void Frmbyswangetsong_Load(object sender, EventArgs e) { //在顯示當前窗體時,讓第二個lvlisttwo和第三個lvlistthere控制項不顯示 lvlisttwo.Visible = false; lvlistthere.Visible = false; #region 給第一個listview綁定數據 //給listview1綁定數據和圖片 ListViewItem lv = new ListViewItem("組合", 0); lv.Tag = "組合"; ListViewItem lv1 = new ListViewItem("女歌手", 1); lv1.Tag = "女"; ListViewItem lv2 = new ListViewItem("男歌手", 2); lv2.Tag = "男"; lvlistone.Items.Add(lv); lvlistone.Items.Add(lv1); lvlistone.Items.Add(lv2); #endregion } //點擊lvlistone中項時觸發的事件 private void lvlistone_Click(object sender, EventArgs e) { //如果沒有選中lvlistone控制項中的任何一項是不會顯示第二個窗體 //讓第二個出現的lvlisttwo和lvlistone顯示在同一個位置 lvlisttwo.Location = lvlistone.Location; //如果選中一項,就把第二個llvlisttwo顯示出來 //讓第二個lvlisttwo顯示 lvlisttwo.Visible = true; if (lvlistone.SelectedItems[0] != null) { //給第二個listview控制項動態綁定數據的方法 lvlisttwoadd(); } } //給第二個listview控制項動態綁定數據的方法lvlisttwoadd(); private void lvlisttwoadd() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from singer_type"; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { string singertype_group = dr["singertype_name"].ToString(); int id = Convert.ToInt32(dr["singertype_id"]); ListViewItem lv = new ListViewItem(); lv.ImageIndex = index++; lv.Text = singertype_group; lv.Tag= id; // MessageBox.Show(lvlisttwo.Tag.ToString()); lvlisttwo.Items.Add(lv); } } } } catch (Exception) { MessageBox.Show("網路異常!"); } finally { con.Close(); } } // 03點擊lvlisttwo中項時觸發的事件 private void lvlisttwo_Click(object sender, EventArgs e) { lvlistthere.Items.Clear(); //如果沒有選中lvlisttwo控制項中的任何一項是不會顯示第三個窗體 //如果選中一項,就把第二個llvlistthere顯示出來 if (lvlisttwo.SelectedItems[0] != null) { lvlisttwo.Visible = false; //讓第三個出現的lvlistthere和lvlistone顯示在同一個位置 lvlistthere.Location = lvlistone.Location; //讓第三個lvlistthere顯示 lvlistthere.Visible = true; //獲取用戶點擊第一個listview是男歌手還是女歌手或者是組合 string singer_group = lvlistone.SelectedItems[0].Tag.ToString(); //獲取用戶點擊點擊第二個listview是哪個地區的id int cityid = Convert.ToInt32(lvlisttwo.SelectedItems[0].Tag); SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select singer_name,singer_photo from singer_info where singer_sex ='" + singer_group + "' and singertype_id="+cityid+" "; SqlCommand cmd = new SqlCommand(sql,con); try { con.Open(); SqlDataReader dr= cmd.ExecuteReader(); if(dr!=null) { if(dr.HasRows) { while (dr.Read()) { //獲取到歌手姓名和歌手圖片路徑 string singer_name = dr["singer_name"].ToString(); string singer_photo_url = dr["singer_photo"].ToString(); //並接路徑 string URL = KtvHelper.singer_photoURL + singer_photo_url; imgsingerinfo .Images.Add(Image.FromFile(URL)); ListViewItem lv = new ListViewItem(); lv.Text = singer_name; lv.ImageIndex =index_singer++; lvlistthere.Items.Add(lv); } } } } catch (Exception) { MessageBox.Show("網路異常!"); } finally