?? clientwnd.cpp
字號:
// ClientWnd.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
/////////////////////////////////////////////////////////////////////////////
// CClientWnd
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, CWnd)
//{{AFX_MSG_MAP(CClientWnd)
ON_WM_PAINT()
ON_WM_KEYDOWN()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientWnd 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://處理字母查詢時的坐標移動
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -