MyKTV项目总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyKTV项目总结相关的知识,希望对你有一定的参考价值。
项目名称: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 { con.Close(); } } } //点击按钮主界面触发的事件 private void btnmain_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } // 点击返回触发的事件 private
以上是关于MyKTV项目总结的主要内容,如果未能解决你的问题,请参考以下文章