📄 clientwnd1.cpp
字号:
// ClientWnd1.cpp : implementation file
//
#include "stdafx.h"
#include "songclient.h"
#include "ClientWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientWnd1
CString CClientWnd::m_strUsedTime = "";
CString CClientWnd::m_strPayforMoney = "";
CClientWnd::CClientWnd()
{
//得到当前屏幕的
m_nScreenX = GetSystemMetrics(SM_CXSCREEN);
m_nScreenY = GetSystemMetrics(SM_CYSCREEN);
CRect rcClient(-2, -2, /*200, 200);//*/m_nScreenX+2, m_nScreenY+2);
//创建一个全屏窗口
CreateEx(WS_EX_APPWINDOW, \
AfxRegisterWndClass(CS_DBLCLKS, ::LoadCursor(NULL,IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), AfxGetApp()->LoadIcon(IDR_MAINFRAME)), \
"点歌系统客户端", \
WS_SYSMENU | WS_POPUP | WS_VISIBLE, \
rcClient, \
NULL, \
NULL);
m_nPageNum = 0;
m_pSelectID = NULL;
m_nMenuIndex = 0;
m_nMenuFlag = 1;
m_pConnectionPtr = NULL;
m_pmediaplay = NULL;
m_bFstRun = true;
m_strAlphabet = "";
m_strPreSelect = "";
}
CClientWnd::~CClientWnd()
{
//通知服务器已退出
char szmsg[] = "quit";
CClientSocket::SendMsg(szmsg, sizeof(szmsg));
//从数据库删除本次已点的所有歌曲
char szhostname[256];
gethostname(szhostname, sizeof(szhostname));
CString strsql;
strsql.Format("delete from request where username='%s'", szhostname);
m_addrecord.GetRecordsetPtr(strsql);
if( m_pConnectionPtr != NULL)
{
if( m_pConnectionPtr->State )
m_pConnectionPtr->Close();
}
if( m_pSelectID != NULL )
{
delete[] m_pSelectID;
m_pSelectID = NULL;
}
}
BEGIN_MESSAGE_MAP(CClientWnd, CFrameWnd)
//{{AFX_MSG_MAP(CClientWnd1)
ON_WM_PAINT()
ON_WM_KEYDOWN()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientWnd1 message handlers
void CClientWnd::InitMenuItem()
{
int nMenuIndex = 0;
m_selectmenu[nMenuIndex].SetMenuItemNum(5);
m_selectmenu[nMenuIndex].m_strMenuItem = new CString[5];
m_selectmenu[nMenuIndex].m_strMenuItem[0] = "请选择选歌方式:";
m_selectmenu[nMenuIndex].m_strMenuItem[1] = "1、 按语种";
m_selectmenu[nMenuIndex].m_strMenuItem[2] = "2、 按歌手";
m_selectmenu[nMenuIndex].m_strMenuItem[3] = "3、 按首字母";
m_selectmenu[nMenuIndex].m_strMenuItem[4] = "4、 按歌名长度";
nMenuIndex++;
m_selectmenu[nMenuIndex].SetMenuItemNum(m_nSpeechNum+1);
m_selectmenu[nMenuIndex].m_strMenuItem = new CString[m_nSpeechNum+1];
m_selectmenu[nMenuIndex].m_strMenuItem[0] = "语种类型:";
m_selectmenu[nMenuIndex].m_strMenuItem[1] = "1、 国语";
m_selectmenu[nMenuIndex].m_strMenuItem[2] = "2、 奥语";
m_selectmenu[nMenuIndex].m_strMenuItem[3] = "3、 英语";
nMenuIndex++;
m_selectmenu[nMenuIndex].SetMenuItemNum(m_nSingerNum+1);
m_selectmenu[nMenuIndex].m_strMenuItem = new CString[m_nSingerNum+1];
m_selectmenu[nMenuIndex].m_strMenuItem[0] = "请选择歌手:";
m_selectmenu[nMenuIndex].m_strMenuItem[1] = "1、 刘若英";
m_selectmenu[nMenuIndex].m_strMenuItem[2] = "2、 郑秀文";
m_selectmenu[nMenuIndex].m_strMenuItem[3] = "3、 王菲";
m_selectmenu[nMenuIndex].m_strMenuItem[4] = "4、 王杰";
m_selectmenu[nMenuIndex].m_strMenuItem[5] = "5、 陆毅";
m_selectmenu[nMenuIndex].m_strMenuItem[6] = "5、 胡敏";
nMenuIndex++;
m_selectmenu[nMenuIndex].SetMenuItemNum(27);
m_selectmenu[nMenuIndex].m_strMenuItem = new CString[27];
m_selectmenu[nMenuIndex].m_strMenuItem[0] = "请选择首字母:";
for(int i = 1; i < 27; i++)
{
m_selectmenu[nMenuIndex].m_strMenuItem[i].Format("%c", 64+i);
}
nMenuIndex++;
m_selectmenu[nMenuIndex].SetMenuItemNum(10);
m_selectmenu[nMenuIndex].m_strMenuItem = new CString[10];
m_selectmenu[nMenuIndex].m_strMenuItem[0] = "请选择歌名长度:";
for(i =1; i < 10; i++)
{
m_selectmenu[nMenuIndex].m_strMenuItem[i].Format("%d", i);
}
//初始化各歌手图片路径
for(i = 0; i< m_nSingerNum; i++)
{
m_strSingerPath[i].Format(m_conserver.GetAppPath() + "picture\\singer%d.bmp", i+1);
}
}
int CClientWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CClientDC dc(this);
m_bgmdc.CreateCompatibleDC(&dc);//创建与CClientDC兼容的DC
m_hbmp = LoadBitmap(m_conserver.GetAppPath() + "picture\\login.bmp", m_nScreenX, m_nScreenY);
SelectObject(m_bgmdc.m_hDC, m_hbmp); //将位图选入m_bgmdc
AfxOleInit();//初始化COM
CString strsql;
strsql.Format("driver={SQL Server};Server=%s;DATABASE=%s;UID=%s;PWD=%s;", \
m_conserver.GetServerIp(), m_conserver.GetDatabaseName(), \
m_conserver.GetUsername(), m_conserver.GetPassword() );
try
{
m_pConnectionPtr.CreateInstance(__uuidof(Connection));
m_pConnectionPtr->Open((_bstr_t)strsql, "", "", adModeUnknown);
m_addrecord.SetConnectionPtr(m_pConnectionPtr);
}
catch(_com_error e)
{
AfxMessageBox((CString)e.ErrorMessage() + "连接数据库失败.");
return -1;
}
InitMenuItem();
m_playsong.Create(IDD_Playsong, this);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
//函数功能: 返回一行字出现在屏幕中间的X坐标
/*参数说明: lpszText: 目的文本
nFontsize: 字体大小
szFontface: 字体类型 */
int CClientWnd::GetXPos(LPCTSTR lpszText, int nFontsize, char* szFontface)
{
CClientDC dc(this);
CFont font, *pfont;
font.CreatePointFont(nFontsize, szFontface);
pfont = dc.SelectObject(&font);
CSize sz = dc.GetTextExtent(lpszText);
int xpos = (m_nScreenX - sz.cx) / 2;
dc.SelectObject(pfont);
return xpos;
}
//////////////////////////////////////////////////////////////////////////////////
//函数功能: 贴上背景位图
void CClientWnd::DrawBGBmp()
{
CClientDC dc(this);
dc.BitBlt(0, 0, m_nScreenX, m_nScreenY, &m_bgmdc, 0, 0, SRCCOPY);
}
//////////////////////////////////////////////////////////////////////////////////
//函数功能: 加载位图
/*参数说明: lpszPath: 位图路径
xpos: 位图宽
ypos: 位图高*/
HBITMAP CClientWnd::LoadBitmap(LPCTSTR lpszPath, int xpos, int ypos)
{
return (HBITMAP)LoadImage(NULL, lpszPath, IMAGE_BITMAP, \
xpos, ypos, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
}
//////////////////////////////////////////////////////////////////////////////////
//函数功能: 将文件输出到屏幕上
/*参数说明: phdc: 内存设备上下文
xpos: 输出X坐标 ypos: 输出Y坐标
lpszText: 输出文本
color: 字体颜色,默认为蓝色
nFontsize: 字体大小,默认为200
lpFontface: 字体类型,默认为楷体 */
void CClientWnd::TextOut(HDC *phdc, int xpos, int ypos, LPCTSTR lpszText, COLORREF color, int nFontsize, LPCTSTR lpFontface)
{
CFont font;
font.CreatePointFont(nFontsize, lpFontface);
SelectObject(*phdc, font.m_hObject);
SetBkMode(*phdc, TRANSPARENT);
SetTextColor(*phdc, color);
::TextOut(*phdc, xpos, ypos, lpszText, strlen(lpszText));
}
void CClientWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.BitBlt(0, 0, m_nScreenX, m_nScreenY, &m_bgmdc, 0, 0, SRCCOPY);
if(m_bFstRun)
{
int xpos;
char szText[256];
sprintf(szText, "欢迎光临");
xpos = GetXPos(szText, 500, "楷体_GB2312");
TextOut(&dc.m_hDC, xpos, 120, szText, RGB(255,0,255), 500);
sprintf(szText, "按确认进入点歌界面");
xpos = GetXPos(szText, 500, "楷体_GB2312");
TextOut(&dc.m_hDC, xpos, 300, szText, RGB(255,0,255), 500);
}
}
//////////////////////////////////////////////////////////////////////////////////
//函数功能: 翻页
/*参数说明: nFlag = 1: 上翻
= 2: 下翻
bSonglistTable: 是否为显示songlist表里的歌曲*/
void CClientWnd::Flip(int nFlag)//, BOOL bSonglistTable)
{
if( nFlag < 0 && nFlag > 3 )//nFlag只能是1、2
return;
if( m_nMenuFlag < 3 || m_nMenuFlag > 5 )//只有m_nMenuFlag为3、4 或 5时才执行翻页
return;
if( m_pSelectID == NULL )//数组为空,退出
return;
char hostname[256];
CString strsql;
gethostname(hostname, 256);
int nID;
if( nFlag == 1)
{
if( m_nPageIndex == 1)//如果现在是第一页,则退出
return;
nID = m_pSelectID[ (m_nPageIndex-1)*10+1 ];
m_nPageIndex--;
if( m_nPageIndex < 1 )
m_nPageIndex = m_nPageNum;
}
else
{
if( m_nPageIndex == m_nPageNum )//如果是最后一页,则退出
return;
nID = m_pSelectID[ m_nPageIndex*10 ];
m_nPageIndex++;
if( m_nPageIndex > m_nPageNum )
m_nPageIndex = 1;
}
if( m_nMenuFlag == 3 )//当m_nMenuFlag == 3时,是在songlist表里查询歌曲
{
if( m_strPreSelect.IsEmpty() )
return;
ShowSelectSong(m_strPreSelect, "请按键选择:", true, true);
}
else
{
int nPlayseq;
strsql.Format("select playseq from request where username='%s' and ID=%d",
hostname, nID);
_RecordsetPtr pRequestPtr;
pRequestPtr = m_addrecord.GetRecordsetPtr(strsql);
_variant_t vtplayseq = pRequestPtr->GetCollect("playseq");
nPlayseq = atoi((_bstr_t)vtplayseq);
if( nFlag == 1 )
{
strsql.Format("select songname, singer \
from request \
where username = '%s' \
group by songname, singer, playseq \
having playseq < %d \
order by playseq", \
hostname, nPlayseq);
}
else
{
strsql.Format("select songname, singer \
from request \
where username = '%s' \
group by songname, singer, playseq \
having playseq > %d \
order by playseq", \
hostname, nPlayseq);
}
ShowSelectSong(strsql, "已点歌清单,按对应的数字键播放:", true, false);
}
}
//////////////////////////////////////////////////////////////////////////////////
//函数功能: 移动坐标
/*参数说明: 1: 上移
2: 下移
3: 左移
4: 右移 */
void CClientWnd::MovePosition(int nMoveFlag)
{
if( m_nMenuFlag == 3 )
return;
int nIndex;
switch( nMoveFlag )
{
case 1://向上移 begin
nIndex = m_selectmenu[m_nMenuIndex].GetUpDwnIndex();
--nIndex;
switch( m_nMenuIndex )
{
case 0://主菜单移动
if(nIndex < 1)
nIndex = 4;
break;
case 1://处理语种查询时的坐标移动
if(nIndex < 1)
nIndex = m_nSpeechNum;
break;
case 2://处理歌手查询时的坐标移动
if(nIndex < 1)
nIndex = m_nSingerNum;
break;
case 3://处理字母查询时的坐标移动
if(nIndex < 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -