当前位置: 移动技术网 > IT编程>开发语言>c# > c#实现KTV点歌系统

c#实现KTV点歌系统

2019年07月18日  | 移动技术网IT编程  | 我要评论

下面通过图文并茂的方式给大家分享c#实现ktv点歌系统。

public enum songplaystate
 {
  //未播放,播放,重播,切歌
  unplayed, played, again, cut
 }
 public class song
 {
  public string songname { get; set; }//歌曲名称
  public string songurl { get; set; }//歌曲路径
  public songplaystate playstate = songplaystate.unplayed;//默认未播放
  internal songplaystate playstate { get; set; }
  //状态为已播
  public void setsongplayed()
  {
  this.playstate = songplaystate.played;
  }
  //重唱
  public void setplayagain()
  {
  this.playstate = songplaystate.again;
  }
  //切歌
  public void setsongcut()
  {
  this.playstate = songplaystate.cut;
  }

playlist类中实现切歌 重唱 下一首 等.....

 public class playlist
 {
  //定义一个长度为、 的歌曲数组,默认存储 首歌曲
  public static song[] songlist = new song[ ];
  public static int songindex = ;//当前播放的歌曲在数组中的索引
  //点播一首歌曲,其实是将歌曲对象添加到歌曲数组中
  public static bool addsong(song song)
  {
  bool success = false;//记录添加歌曲是否成功
  for (int i = ; i < songlist.length; i++)
  {
   //找到数组中第一个为null的位置
   if (songlist[i] == null)
   {
   songlist[i] = song;
   success = true;
   break;
   }
  }
  return success;
  }
  //获取当前播放的歌曲::既然是获取当前播放的歌曲,返回值肯定是song类型
  public static song getplaysong()
  {
  if (songlist[songindex] != null)
  {
   return songlist[songindex];
  }
  else
  {
   return null;
  }
  }
  /// <summary>
  /// 播放下一首
  /// </summary>
  public static void moveon()
  {
  if (songlist[songindex] != null && songlist[songindex].playstate == songplaystate.again)
  {
   songlist[songindex].setsongplayed();
  }
  else
  {
   songindex++;
  }
  }
  /// <summary>
  /// 当前播放的歌曲名称
  /// </summary>
  /// <returns>歌曲名称</returns>
  public static string playingsongname()
  {
  string songname = ""; // 歌曲名称
  if (songlist[songindex] != null)
  {
   songname = songlist[songindex].songname;
  }
   return songname;
  }
  /// <summary>
  /// 下一首要播放的歌曲名称
  /// </summary>
  /// <returns>歌曲名称</returns>
  public static string nextsongname()
  {
  string songname = ""; // 歌曲名称
  if (songlist[songindex + ] != null)
  {
   songname = songlist[songindex + ].songname;
  }
   return songname;
  }
  //重放当前歌曲
  public static void playagain()
  {
  if (songlist[songindex] != null)
  {
   songlist[songindex].setplayagain();
  }
  }
  //切歌
  public static void cutsong(int index)
  {
  int i;//循环变量,代表切歌的位置
  if (index == - )//循环变量,代表切割的位置
  {
   i = songindex;
  }
  else
  {
   i = index;//从切歌的位置开始,将歌曲逐个向前移一个位置
  }
  songlist[i].setsongcut();
  while (songlist[i] != null)
  {
   songlist[i] = songlist[i + ];
   i++; 
   //如果达到数组最后一个元素,就将最后一个元素指向空
   if (i == songlist.length)
   {
   songlist[i] = null;
   }
  }
  }
 }

实现歌手点歌

 public frmmain frmmain;
  string connectionstr = "server=.;database=myktv;uid=sa";
  dbhelp db = new dbhelp();
  private sqlconnection con;
  //首先要查出数据库中的图片路径和歌曲路径
  private void frmcountry_load(object sender, eventargs e)
  {
   con = new sqlconnection(connectionstr);
  con.open();
  string sql = "select resource_path from resource_path where resource_id= ";
   string sqlsongpath = "select resource_path from resource_path where resource_id= ";
  sqlcommand cmd = new sqlcommand(sql,con);
   sqlcommand cmd = new sqlcommand(sqlsongpath, con);
  ktvunit.imagepath = cmd.executescalar().tostring();
  ktvunit.songpath = cmd .executescalar().tostring();
  con.close();
  }
  //点击歌手男女或组合时
  private void lvone_click(object sender, eventargs e)
  {
   loadsingerarea();
  }
  public string singer_type { get; set; }
  private void loadsingerarea()
  {
  if (this.lvone.selecteditems[ ] != null)
  {
   lvone.visible = false;
   lvtwo.location = lvone.location;
   lvtwo.dock = dockstyle.fill;
   lvtwo.visible = true;
   this.singer_type=convert.tostring(lvone.selecteditems[ ].text);
  }
   con = new sqlconnection(connectionstr);
  string sql = "select singertype_id,singertype_name from singer_type";
  sqlcommand cmd = new sqlcommand(sql, con);
  sqldatareader dr;
  try
  {
   con.open();
   lvtwo.items.clear();
   dr = cmd.executereader();  
   if (dr.hasrows)
   {
   int index = ;
   while (dr.read())
   {
    listviewitem lvitem = new listviewitem();
    int typeid = convert.toint (dr["singertype_id"]);
    string typename = convert.tostring(dr["singertype_name"]);
    lvitem.text = typename;
    lvitem.tag = typeid;
    lvitem.imageindex = index;
    lvtwo.items.add(lvitem);
    index++;
   }
   }
   dr.close();
  }
  catch (exception ex)
  {
   messagebox.show("系统出现异常" + ex.message);
  }
  finally 
  {
   con.close();
  }
  }
  public string singertype_id { get; set; }
  /// <summary>
  /// 点击地区类型时
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void lvtwo_click(object sender, eventargs e)
  {
  if (this.lvtwo.selecteditems[ ] != null)
  {
   lvtwo.visible = false;
   lvthree.location = lvtwo.location;
   lvthree.dock = dockstyle.fill;
   lvthree.visible = true;
   this.singertype_id = convert.tostring(lvtwo.selecteditems[ ].tag);
  }
  string result = singer_type;
  if (result != "组合")
  {
   result = singer_type == "女歌手" ? "女" : "男";
  }
  con = new sqlconnection(connectionstr);
  string sql =string.format( "select singer_id,singer_name,singer_photo_url from singer_info where singertype_id={ } and singer_sex='{ }'",singertype_id,result);
  sqlcommand cmd = new sqlcommand(sql, con);
  sqldatareader dr;
  try
  {
   con.open();
   int index = ;
   lvthree.items.clear();
   imagelist .images.clear();
   dr = cmd.executereader();
   if (dr.hasrows)
   { 
   while (dr.read())
   {   
    string photourl =ktvunit.imagepath + convert.tostring(dr["singer_photo_url"]);
    //先给imagelist填充图片
    imagelist .images.add(image.fromfile(photourl));
    listviewitem lvitem = new listviewitem();
    lvitem.text = convert.tostring(dr["singer_name"]);
    lvitem.tag = convert.tostring(dr["singer_id"]);
    lvitem.imageindex = index;
    lvthree.items.add(lvitem);
    index++;
   }
   }
   dr.close();
  }
  catch (exception ex)
  {
   messagebox.show("系统出现异常" + ex.message);
  }
  finally
  {
   con.close();
  }
  }
  public void songlist()
  {
  //读取数据库,读出该歌手的所有歌曲
   stringbuilder sb = new stringbuilder();
  //拼接sql语句
  sb.appendformat("select song_id,song_name,song_url,singer_name from song_info,singer_info where singer_name='{ }' and song_info.singer_id={ }", lvthree.selecteditems[ ].text, convert.toint (lvthree.selecteditems[ ].tag));
  frmsonglist songlist = new frmsonglist();
   songlist.sql = sb.tostring();
  songlist.previous = ktvclient.previoisform.singer;//指定返回的窗体是按歌手点歌
  songlist.showdialog();
  this.close();
  }
  private void lvthree_click(object sender, eventargs e)
  {
  songlist();
  }
  private void tssingermain_click(object sender, eventargs e)
  {
  frmmain main = new frmmain();
  main.show();
  this.hide();
  }
  private void tssingerback_click(object sender, eventargs e)
  {
  if (this.lvone.visible==true)
  {
   frmmain man = new frmmain();
   man.show();
   this.hide();
  }
  else if (this.lvtwo.visible==true)
  {
   this.lvtwo.visible = false;
   this.lvone.visible = true;
  }
  else if (this.lvthree.visible==true)
  {
   this.lvthree.visible = false;
   this.lvtwo.visible = true;
  }
  }
  private void tssingercut_click(object sender, eventargs e)
  {
  playlist.cutsong(- );
  }
  private void tssingeragain_click(object sender, eventargs e)
  {
  playlist.playagain();
  }
  private void tssingeryidian_click(object sender, eventargs e)
  {
  frmplaylist frm = new frmplaylist();
  frm.show();
  }

拼音点歌

 public frmmain frmmain;
  [dllimportattribute("user .dll")]
  private static extern bool animatewindow(intptr hwnd, int dwtime, int dwflags);
  dbhelp db = new dbhelp();
  string connectionstr = "server=.;database=myktv;uid=sa";
  private void frmpinyin_load(object sender, eventargs e)
  {
  animatewindow(this.handle, , frmmain.aw_slide + frmmain.aw_ver_positive);
  sqlconnection con = new sqlconnection(connectionstr);
  con.open();
  db.connection(); 
  string sqlsongpath = "select resource_path from resource_path where resource_id= ";
  sqlcommand cmd = new sqlcommand(sqlsongpath, con);
  ktvunit.songpath = cmd.executescalar().tostring();
  }
  private void btnsearch_click(object sender, eventargs e)
  {
  string pinyin = this.txtpinyin.text;
  //判断是否是中文 还是拼音
  if (!regex.ismatch(this.txtpinyin.text, @"^[\u e -\u fa ]+$"))
  {
   stringbuilder py = new stringbuilder(pinyin);
   for (int i = ; i <= py.length; i++)
   {
   py.insert(i, "%");
   i++;
   }
   string sql = string.format("select song_name,singer_name from dbo.singer_info, dbo.song_info where dbo.singer_info.singer_id=dbo.song_info.singer_id and song_ab like '{ }'", py);
   this.dgvpinyininfo.datasource = db.datatable(sql,"py");
  }
  else
  {
   stringbuilder zw = new stringbuilder(pinyin);
   for (int i = ; i < zw.length; i++)
   {
   zw.insert(i,"%");
   i++;
   }
   string sql = string.format("select song_name,singer_name from dbo.singer_info, dbo.song_info where dbo.singer_info.singer_id=dbo.song_info.singer_id and song_name like '{ }'", zw);
   this.dgvpinyininfo.datasource = db.datatable(sql, "py");
  }
  }
  private void dgvpinyininfo_doubleclick(object sender, eventargs e)
  {
  string songname = this.dgvpinyininfo.selectedrows[ ].cells["song_name"].value.tostring();
  dbhelp db = new dbhelp();
  db.connection();
  string sql = string.format("select song_name,singer_name,song_url,song_photo_url from dbo.song_info,dbo.singer_info where dbo.singer_info.singer_id=dbo.song_info.singer_id and song_name='{ }'",songname);
   sqldatareader reader = db.executereaders(sql.tostring());
   song song;
  if (reader.read())
  {
   song = new song();
   song.songname = reader["song_name"].tostring();
   song.songurl = ktvunit.songpath+reader["song_url"].tostring();
   playlist.addsong(song);  
  }
  reader.close();
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "a";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "b";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "c";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "d";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "e";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "f";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "g";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "h";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "i";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "j";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "k";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "l";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "m";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "n";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "o";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "p";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "q";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "r";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "s";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "t";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "u";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "v";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "w";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "x";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "y";
  }
  private void picturebox _click(object sender, eventargs e)
  {
  textbox .text = textbox .text + "z";
  }
  private void frmpinyin_formclosing(object sender, formclosingeventargs e)
  {
  animatewindow(this.handle, , frmmain.aw_slide + frmmain.aw_ver_positive);
  }
  public void binder() 
  {
  string pinyin = this.textbox .text;
  stringbuilder py = new stringbuilder(pinyin);
   for (int i = ; i <= py.length; i++)
  {
   py.insert(i, "%");
   i++;
  }
  string sql = string.format("select song_name,singer_name from dbo.singer_info, dbo.song_info where dbo.singer_info.singer_id=dbo.song_info.singer_id and song_ab like '{ }'", py);
  dataset ds = db.dataset(sql, "py");
  if (ds.tables["py"]!=null)
  {
   ds.tables["py"].clear();
  }
  this.dgvpinyininfo.datasource = db.datatable(sql, "py");
  }
  private void picturebox _click(object sender, eventargs e)
  {
  string text = textbox .text;
  int index = text.length - ;
  if (index >= )
  {
   textbox .text = text.remove(index);
  }
  }
  private void textbox _textchanged(object sender, eventargs e)
  {
  if (textbox .text!=string.empty)
  {
   binder();
   this.dgvpinyininfo.autogeneratecolumns = false;
  }
  else
  {
   this.dgvpinyininfo.datasource=null;
  }
  }
  private void tspymain_click(object sender, eventargs e)
  {
  frmmain main = new frmmain();
  main.show();
  this.hide();
  }
  private void txpyagain_click(object sender, eventargs e)
  {
  frmmain main = new frmmain();
  main.playsong();
  }
  song song = new song();
  private void tspycut_click(object sender, eventargs e)
  {
  song.playstate = songplaystate.cut;
  }
  private void tspyyidian_click(object sender, eventargs e)
  {
  frmplaylist list = new frmplaylist();
  list.show();
  }
  private void tspyback_click(object sender, eventargs e)
  {
  application.exit();
  } 

类型点歌

 public frmmain frmmain;
  string connectionstr = "server=.;database=myktv;uid=sa";
  dbhelp db = new dbhelp();
  private sqlconnection con;
  private void frmsongtype_load(object sender, eventargs e)
  {
  con = new sqlconnection(connectionstr);
  con.open();
  string sql = "select resource_path from resource_path where resource_id= ";
  string sqlsongpath = "select resource_path from resource_path where resource_id= ";
  sqlcommand cmd = new sqlcommand(sqlsongpath,con);
  ktvunit.songpath = cmd .executescalar().tostring();
  sqlcommand cmd = new sqlcommand(sql, con);
  ktvunit.imagepath = cmd.executescalar().tostring();
  con.close();
   con = new sqlconnection(connectionstr);
  string sql = string.format("select songtype_id,songtype_name,songtype_url from song_type");
  sqlcommand cmd = new sqlcommand(sql , con);
  sqldatareader dr;
  try
  {
   con.open();
   int index = ;
   lvsongtype.items.clear();
   imagelist .images.clear();
   dr = cmd .executereader();
   if (dr.hasrows)
   {
   while (dr.read())
   {
    string photourl = ktvunit.imagepath + convert.tostring(dr["songtype_url"]);
    //先给imagelist填充图片
    imagelist .images.add(image.fromfile(photourl));
    listviewitem lvitem = new listviewitem();
    lvitem.text = convert.tostring(dr["songtype_name"]);
    lvitem.tag = convert.tostring(dr["songtype_id"]);
    lvitem.imageindex = index;
    lvsongtype.items.add(lvitem);
    index++;
   }
   }
   dr.close();
  }
  catch (exception ex)
  {
   messagebox.show("系统出现异常" + ex.message);
  }
  finally
  {
   con.close();
  }
  }
  private void loadsongtype()
  {
  //读取数据库,读出该歌曲类型的所有歌曲
   stringbuilder sb = new stringbuilder();
  //拼接sql语句
  sb.appendformat("select song_info.song_name,singer_info.singer_name,song_info.song_url from singer_info,song_info where song_info.singer_id=singer_info.singer_id and song_info.songtype_id={ }", convert.toint (lvsongtype.selecteditems[ ].tag));
  frmsonglist songlist = new frmsonglist();
   songlist.sql = sb.tostring();
  songlist.previous = ktvclient.previoisform.songtype;//指定返回的窗体是按歌曲类型点歌
  songlist.showdialog();
  this.close();
  }
  private void lvsongtype_click(object sender, eventargs e)
  {
  loadsongtype();
  }
  private void tstysingermain_click(object sender, eventargs e)
  {
  frmmain main = new frmmain();
  main.show();
  this.hide();
  }
  private void tstysingeragain_click(object sender, eventargs e)
  {
  frmmain main = new frmmain();
  main.playsong();
  }
  song song = new song();
  private void tstysingercut_click(object sender, eventargs e)
  {
  song.playstate = songplaystate.cut;
  }
  private void tstysingeryidian_click(object sender, eventargs e)
  {
  frmplaylist list = new frmplaylist();
  list.show();
  }
  private void tstysingerback_click(object sender, eventargs e)
  {
  frmmain main = new frmmain();
  main.show();
  this.hide();
  }

金榜排行

public frmmain frmmain;
  dbhelp db = new dbhelp();
  string connectionstr = "server=.;database=myktv;uid=sa";
  private void frmjb_load(object sender, eventargs e)
  {
  sqlconnection con = new sqlconnection(connectionstr);
  con.open();
  db.connection();
  string sql = "select song_name,song_play_count from dbo.song_info order by song_play_count desc";
  string sqlsongpath = "select resource_path from resource_path where resource_id= ";
  sqlcommand cmd = new sqlcommand(sqlsongpath, con);
  ktvunit.songpath = cmd.executescalar().tostring();
  dataset ds = db.dataset(sql,"count");
  this.dgvsonglist.datasource = ds.tables["count"].defaultview;
  }
  private void dgvsonglist_click(object sender, eventargs e)
  {
  dbhelp db = new dbhelp();
  if (dgvsonglist.selectedrows[ ]!=null)
  {
   string songname = this.dgvsonglist.selectedrows[ ].cells["songname"].value.tostring();
   db.connection();
   string sql = string.format("select song_name,singer_name,song_url,song_photo_url from dbo.song_info,dbo.singer_info where dbo.singer_info.singer_id=dbo.song_info.singer_id and song_name='{ }'", songname);
   sqldatareader reader = db.executereaders(sql.tostring());
   song song;
   if (reader.read())
   {
   song = new song();
   song.songname = reader["song_name"].tostring();
   song.songurl = ktvunit.songpath + reader["song_url"].tostring();
   playlist.addsong(song);
   }
   reader.close();
  }
  else
  {
   messagebox.show("空");
  }
  }
 

数字点歌

 public frmmain frmmain;
  string connectionstr = "server=.;database=myktv;uid=sa";
  dbhelp db = new dbhelp();
  private sqlconnection con;
  private void frmnumber_load(object sender, eventargs e)
  {
  con = new sqlconnection(connectionstr);
  con.open();
  string sqlsongpath = "select resource_path from resource_path where resource_id= ";
  sqlcommand cmd = new sqlcommand(sqlsongpath, con);
  ktvunit.songpath = cmd.executescalar().tostring();
  con.close();
  for (int i = ; i <= ; i++)
  {
   for (int j = ; j <= ; j++)
   {
   label label = new label();
   label.forecolor = color.red;
   label.backcolor = color.pink;
   label.font=new system.drawing.font("华文彩云", );
   label.textalign = contentalignment.middlecenter;
   label.click += label_click;
   this.mousemove += frmnumber_mousemove;
   label.mousehover += label_mousehover;
   label.size = new system.drawing.size( , );
   label.text = j.tostring();
   if (i > )
   {
    label.text = (j + i + ).tostring();
   }
   if (i > )
   {
    label.text = (j + i + ).tostring();
   }
   if (i > )
   {
    label.text = (j + i + ).tostring();
   }     
   label.location = new point( + * j, + * i);
   this.controls.add(label);
   }
  }
  }

已点列表

 private void frmplaylist_load(object sender, eventargs e)
  {
  songlist();
  }
  public void songlist() 
  {
  lvsong.items.clear();
  for (int i = ; i < playlist.songlist.length; i++)
  {
   if (playlist.songlist[i]!=null)
   {
   listviewitem item = new listviewitem();
   item.text = playlist.songlist[i].songname;
   item.tag = i;
   string playstate = playlist.songlist[i].playstate == songplaystate.unplayed ? "未播放" : "已播";
   item.subitems.add(playstate);
   lvsong.items.add(item);
   }
  }
  }
  private void btnclose_click(object sender, eventargs e)
  {
  this.close();
  }

以上就是c#实现ktv点歌系统的全部代码,希望大家喜欢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网