⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 songmanage.cpp

📁 点歌系统
💻 CPP
字号:
// AddSong.cpp : implementation file
//

#include "stdafx.h"
#include "songserver.h"
#include "SongManage.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSongManage dialog


CSongManage::CSongManage(CWnd* pParent /*=NULL*/)
	: CDialog(CSongManage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSongManage)
	m_fstalphabet = _T("");
	m_speech = _T("");
	m_singer = _T("");
	m_songname = _T("");
	m_songpath = _T("");
	m_namelen = _T("");
	//}}AFX_DATA_INIT

    m_nIndexlist = 0;
	m_nIndexsong = 0;
	m_bChgrecord = false;
}


void CSongManage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSongManage)
	DDX_Control(pDX, IDC_Songlist, m_songlist);
	DDX_Text(pDX, IDC_FstAlphabet, m_fstalphabet);
	DDX_Text(pDX, IDC_Language, m_speech);
	DDX_Text(pDX, IDC_Singer, m_singer);
	DDX_Text(pDX, IDC_Songname, m_songname);
	DDX_Text(pDX, IDC_Songpath, m_songpath);
	DDX_Text(pDX, IDC_Namelen, m_namelen);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSongManage, CDialog)
	//{{AFX_MSG_MAP(CSongManage)
	ON_BN_CLICKED(IDC_FilePath, OnFilePath)
	ON_BN_CLICKED(IDC_DeleteRecord, OnDeleteRecord)
	ON_NOTIFY(NM_DBLCLK, IDC_Songlist, OnDblclkSonglist)
	ON_BN_CLICKED(IDC_SaveRecord, OnSaveRecord)
	ON_BN_CLICKED(IDC_ChgRecord, OnChgRecord)
	ON_BN_CLICKED(IDC_Query, OnQuery)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSongManage message handlers
BOOL CSongManage::OnInitDialog() 
{
	CDialog::OnInitDialog();
	 
	m_songlist.InsertColumn(0, "ID",       LVCFMT_LEFT, 25);
	m_songlist.InsertColumn(1, "    歌名", LVCFMT_LEFT, 80);
	m_songlist.InsertColumn(2, "长度",     LVCFMT_LEFT, 40);
	m_songlist.InsertColumn(3, "语种",     LVCFMT_LEFT, 40);
	m_songlist.InsertColumn(4, "首字母",   LVCFMT_LEFT, 50);
	m_songlist.InsertColumn(5, "歌手",     LVCFMT_LEFT, 40);
	m_songlist.InsertColumn(6, "歌曲路径", LVCFMT_LEFT, 110);
 	m_songlist.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);

	SetAllItem();
    	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//-----------------------------------------------------
//函数功能: 得到歌曲的路径
void CSongManage::OnFilePath() 
{
	CFileDialog filedlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "影视文件(*.*)|*.*||");
	if( filedlg.DoModal() == IDOK)
	{
		UpdateData();
		m_songpath = filedlg.GetPathName();
		UpdateData(false);
	}   	
}

//-----------------------------------------------------
//函数功能: 保存记录及更改记录
void CSongManage::OnSaveRecord() 
{
	UpdateData();

	//保存记录
	if( m_Addrecord.SaveRecord(m_songname, m_namelen, m_speech, m_fstalphabet, m_singer, m_songpath, !m_bChgrecord, m_nIndexsong) )
	{
		//保存记录成功
        //m_bChgrecord为false时为新建记录,为true是为更改当前记录
		if( m_bChgrecord )
		{
			m_songlist.DeleteItem(m_nIndexlist);
		}

		m_bChgrecord = false;
        
		//把刚新建的记录添加到列表框
		CString strsql;
		strsql.Format("select * \
			           from songlist \
					   where songname='%s' and singer='%s' and speech='%s' \
					   order by ID", \
			           m_songname, m_singer, m_speech);

		_RecordsetPtr pSonglistPtr;
		pSonglistPtr = m_Addrecord.GetRecordsetPtr(strsql);

		if(pSonglistPtr->adoEOF)
			return;
		pSonglistPtr->MoveFirst();

		SetItemText(pSonglistPtr);
		
		m_speech = "";
		m_singer = "";
		m_namelen = "";
		m_songname = "";
		m_songpath = "";
		m_fstalphabet = "";
		UpdateData(false);
	}
}

//-----------------------------------------------------
//函数功能: 更改记录
void CSongManage::OnChgRecord() 
{
    m_bChgrecord = true;
	OnSaveRecord();
}

//-----------------------------------------------------
//函数功能: 删除从列表框中选中的记录
void CSongManage::OnDeleteRecord() 
{
	int nIndexlsit = m_songlist.GetSelectionMark();

	int nIndexsong = atoi(m_songlist.GetItemText(nIndexlsit, 0).GetBuffer(0));

	if( m_Addrecord.DeleteRecord( nIndexsong ) )
		m_songlist.DeleteItem( nIndexlsit );
}

//--------------------------------------------------------------------
//函数功能: 根据编辑框中输入的查询条件从数据中查找记录并显示到列表框中
void CSongManage::OnQuery() 
{
	UpdateData();

	if(m_songname.IsEmpty() && m_namelen.IsEmpty() 
		&& m_speech.IsEmpty() && m_singer.IsEmpty() 
		&& m_fstalphabet.IsEmpty())
	{
		SetAllItem();
 		return;
	}

	CString strsubcon[5];//存放五个查询条件
	//判断每个查询条件是否为空,不为空则格式化成能在selects查询的where语句里用的格式。
	strsubcon[0].Format( ( m_songname.IsEmpty() ) ? "%s" : "songname='%s'", m_songname );
	strsubcon[1].Format( ( m_namelen.IsEmpty() ) ? "%s" : "namelen=%s", m_namelen );
	strsubcon[2].Format( ( m_speech.IsEmpty() ) ? "%s" : "speech='%s'", m_speech );
	strsubcon[3].Format( ( m_singer.IsEmpty() ) ? "%s" : "singer='%s'", m_singer );
	strsubcon[4].Format( ( m_fstalphabet.IsEmpty() ) ? "%s" : "fstalphabet='%s'", m_fstalphabet );
 
	BOOL bFstcon = true;
	CString strand("and ");
	CString strcondition("");

	//把不为空的查询条件连接在一起,中间用加上AND
	for(int i = 0; i < 5; i++)
	{
		if( !strsubcon[i].IsEmpty() )
		{
			strcondition += bFstcon ? strsubcon[i] : strand + strsubcon[i];
			if(bFstcon)
				bFstcon = false;
		}
	}
 
	CString strsql;
	strsql.Format("select * from songlist where %s order by ID", strcondition);
	
 	_RecordsetPtr pSonglistPtr;
	pSonglistPtr = m_Addrecord.GetRecordsetPtr(strsql);

	if(pSonglistPtr->adoEOF)
	{
		AfxMessageBox("记录为空,请重新查找.");
		return;
	}
	pSonglistPtr->MoveFirst();	

	m_songlist.DeleteAllItems();
 	while( !pSonglistPtr->adoEOF )
	{
		SetItemText(pSonglistPtr);
		pSonglistPtr->MoveNext();
	}
}

//--------------------------------------------------------------------------
//函数功能: 通过双击列表框的记录,将列表框中选中记录的内容显示到对应的编辑中
void CSongManage::OnDblclkSonglist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	m_nIndexlist = m_songlist.GetSelectionMark();

    m_nIndexsong = atoi(m_songlist.GetItemText(m_nIndexlist, 0).GetBuffer(0));

	m_songname     = m_songlist.GetItemText(m_nIndexlist, 1);
	m_namelen      = m_songlist.GetItemText(m_nIndexlist, 2);
	m_speech       = m_songlist.GetItemText(m_nIndexlist, 3);
	m_fstalphabet  = m_songlist.GetItemText(m_nIndexlist, 4);
	m_singer       = m_songlist.GetItemText(m_nIndexlist, 5);
	m_songpath     = m_songlist.GetItemText(m_nIndexlist, 6);

	UpdateData(false);

	*pResult = 0;
}

//-----------------------------------------------------
//函数功能: 将数据中的所有记录显示到列表框中
void CSongManage::SetAllItem()
{
	_RecordsetPtr pSonglistPtr;
	pSonglistPtr = m_Addrecord.GetRecordsetPtr("select * from songlist order by ID");

	if(pSonglistPtr == NULL)
		return;

	if(pSonglistPtr->adoEOF)
		return;

	pSonglistPtr->MoveFirst();
	m_songlist.DeleteAllItems();
 	while( !pSonglistPtr->adoEOF )
	{
		SetItemText(pSonglistPtr);
		pSonglistPtr->MoveNext();
	}
}

//-----------------------------------------------------
//函数功能: 将pRecordset指向的记录显示到列表框中
void CSongManage::SetItemText(_RecordsetPtr pRecordset)
{
	int nItemCount = 0;
	nItemCount = m_songlist.GetItemCount();
	m_songlist.InsertItem(nItemCount,     (_bstr_t)pRecordset->GetCollect("ID"));
	m_songlist.SetItemText(nItemCount, 1, (_bstr_t)pRecordset->GetCollect("songname"));
	m_songlist.SetItemText(nItemCount, 2, (_bstr_t)pRecordset->GetCollect("namelen"));
	m_songlist.SetItemText(nItemCount, 3, (_bstr_t)pRecordset->GetCollect("speech"));
	m_songlist.SetItemText(nItemCount, 4, (_bstr_t)pRecordset->GetCollect("fstalphabet"));
	m_songlist.SetItemText(nItemCount, 5, (_bstr_t)pRecordset->GetCollect("singer"));
	m_songlist.SetItemText(nItemCount, 6, (_bstr_t)pRecordset->GetCollect("songpath"));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -