📄 dballgamedlg.cpp
字号:
// DBallGameDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DBallGame.h"
#include "DBallGameDlg.h"
#include "SingleMode.h"
#include "MartchMode.h"
#include "NetMode.h"
#include "MyDef.h"
#include "NetModeDlg.h"
#include "ServerThread.h"
#include "ClientThread.h"
#include "CLocalMachine.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDBallGameDlg dialog
CDBallGameDlg::CDBallGameDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDBallGameDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDBallGameDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDBallGameDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDBallGameDlg)
DDX_Control(pDX, IDC_NEWS, m_news);
DDX_Control(pDX, IDC_GAMEWIN, m_GameWin);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDBallGameDlg, CDialog)
//{{AFX_MSG_MAP(CDBallGameDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SINGLE, OnSingle)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_MARTCH, OnMartch)
ON_BN_CLICKED(IDC_NET, OnNet)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDBallGameDlg message handlers
BOOL CDBallGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CWnd * pWnd = GetDlgItem(IDC_GAMEWIN);
p_WinDC = pWnd->GetDC ();
m_GameWin.GetClientRect(&WinRect);
btn_single.Attach (GetDlgItem (IDC_SINGLE)-> GetSafeHwnd( ));
btn_martch.Attach (GetDlgItem (IDC_MARTCH)-> GetSafeHwnd( ));
btn_net.Attach (GetDlgItem (IDC_NET)-> GetSafeHwnd( ));
p_Ball = 0;
m_GameMode = 0;
m_GameBegin = 0;
SorC=1;
//while(1)
PlaySound("start",AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC|SND_NODEFAULT );
return TRUE; // return TRUE unless you set the focus to a control
}
void CDBallGameDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CDBallGameDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDBallGameDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDBallGameDlg::OnSingle()
{
KillTimer(Init_Game);
if(m_GameBegin==0&&m_GameMode !=Mode_Single)
{
if(m_GameMode == Mode_Net)
{
if(m_ThreadStart==1)
{
p_Thread->ExitInstance ();//关闭线程
m_ThreadStart = 0;
}
}
if(p_Ball !=0)
{
p_Ball->DrawBall (0);
//Bar_Above.DrawBar (0);
//Bar_Below.DrawBar (0);
delete p_Ball;
p_Ball=0;
}
p_Ball = new CSingleMode;
p_Ball->InitBall (p_WinDC , &WinRect , &m_news);
Bar_Above.InitBar (p_WinDC,&WinRect,BAR_ABOVE);
Bar_Below.InitBar (p_WinDC,&WinRect,BAR_BELOW);
btn_single.SetCheck (1);
btn_net.SetCheck (0);
btn_martch.SetCheck (0);
}
else if(m_GameBegin==1)
{
m_news.SetWindowText ("进行中...无法切换模式");
btn_single.SetCheck (1);
return ;
}
Bar_Below.SetPos(Bar_Above.GetPos ());
m_GameMode = Mode_Single;
m_news.SetWindowText ("↓:游戏开始,←→:方向选择");
SetTimer(Init_Game,1,NULL);
}
void CDBallGameDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
btn_single.Detach ();
btn_net.Detach ();
btn_martch.Detach ();
CDialog::OnClose();
}
void CDBallGameDlg::OnMartch()
{
KillTimer(Init_Game);
if(m_GameBegin==0&&m_GameMode !=Mode_Martch)
{
if(m_GameMode == Mode_Net)
{
if(m_ThreadStart==1)
{
p_Thread->ExitInstance ();//关闭线程
m_ThreadStart = 0;
}
}
if(p_Ball !=0)
{
p_Ball->DrawBall (0);
//Bar_Above.DrawBar (0);
//Bar_Below.DrawBar (0);
delete p_Ball;
p_Ball=0;
}
p_Ball = new CMartchMode;
p_Ball->InitBall (p_WinDC , &WinRect , &m_news);
Bar_Above.InitBar (p_WinDC,&WinRect,BAR_ABOVE);
Bar_Below.InitBar (p_WinDC,&WinRect,BAR_BELOW);
btn_single.SetCheck (0);
btn_net.SetCheck (0);
btn_martch.SetCheck (1);
}
else if(m_GameBegin==1)
{
m_news.SetWindowText ("进行中...无法切换模式");
btn_martch.SetCheck (0);
return ;
}
m_GameMode = Mode_Martch;
m_news.SetWindowText ("A:↓:游戏开始,←→:方向选择\nB:X :游戏开始,Z C :方向选择");
SetTimer(Init_Game,1,NULL);
}
void CDBallGameDlg::OnNet()
{
KillTimer(Init_Game);
if(m_GameBegin==0&&m_GameMode !=Mode_Net)
{
if(p_Ball !=0)
{
p_Ball->DrawBall (0);
//Bar_Above.DrawBar (0);
//Bar_Below.DrawBar (0);
delete p_Ball;
p_Ball=0;
}
p_Ball = new CNetMode;
Bar_Above.InitBar (p_WinDC,&WinRect,BAR_ABOVE);
Bar_Below.InitBar (p_WinDC,&WinRect,BAR_BELOW);
btn_single.SetCheck (0);
btn_net.SetCheck (1);
btn_martch.SetCheck (0);
}
else if(m_GameBegin==1)
{
m_news.SetWindowText ("进行中...无法切换模式");
btn_net.SetCheck (0);
return ;
}
//m_news.SetWindowText ("该模块仍在开发中...");
char host[255], hostIP[255];
CLocalMachine myhost;
myhost.GetIPAddress(hostIP, host);
CString str;
str.Format ("本机名: %s\n本机IP: %s",host,hostIP);
m_news.SetWindowText(str);
m_GameMode = Mode_Net;
CNetModeDlg dlg;
if(dlg.DoModal ()==IDOK)
{
if(dlg.GetSorC ()==1)
{
SorC = 1;
p_Thread = AfxBeginThread(RUNTIME_CLASS( CServerThread),THREAD_PRIORITY_ABOVE_NORMAL,0,0,NULL );
((CServerThread*)p_Thread)->Exchange (this);
((CServerThread*)p_Thread)->StartListen();
}
else
{
SorC = 0;
p_Thread = AfxBeginThread(RUNTIME_CLASS( CClientThread),THREAD_PRIORITY_ABOVE_NORMAL,0,0,NULL );
((CClientThread*)p_Thread)->Exchange (this);
((CClientThread*)p_Thread)->StartConnect(dlg.GetIpAddr ());
}
m_ThreadStart = 1;
}
else
{
delete p_Ball;
p_Ball=0;
m_ThreadStart = 0;
return ;
}
((CNetMode*)p_Ball)->SetSorC(SorC);
p_Ball->InitBall (p_WinDC , &WinRect , &m_news);
// Sleep(1000);
SetTimer(Init_Game,1,NULL);
}
void CDBallGameDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==Init_Game)
{
if(m_GameBegin == 1)
KillTimer(Init_Game);
Bar_Above.DrawBar (0);
Bar_Above.DrawBar (1);
Bar_Below.DrawBar (0);
Bar_Below.DrawBar (1);
p_Ball->DrawBall (1);
if(m_GameMode==Mode_Net)
{
if(SorC==1)//服务器or非网络模式
p_Ball->FollowBar(Bar_Below.GetPos ());
else//客户机
p_Ball->FollowBar(Bar_Above.GetPos ());
}
}
if(nIDEvent==1)
{
if((p_Ball->Move ())==1)
{
KillTimer(1);
p_Ball->Failure();
Reset();
}
}
CDialog::OnTimer(nIDEvent);
}
BOOL CDBallGameDlg::PreTranslateMessage(MSG* pMsg)
{
if(m_GameMode == Mode_Single)
{
if (pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam==VK_RIGHT)
{
Bar_Below.GoRight ();
Bar_Above.GoRight ();
p_Ball->SetBarPos (Bar_Below.GetPos (),BAR_BELOW);
p_Ball->SetBarPos (Bar_Above.GetPos (),BAR_ABOVE);
}
if(pMsg->wParam==VK_LEFT)
{
Bar_Below.GoLeft ();
Bar_Above.GoLeft ();
p_Ball->SetBarPos (Bar_Below.GetPos (),BAR_BELOW);
p_Ball->SetBarPos (Bar_Above.GetPos (),BAR_ABOVE);
}
if(pMsg->wParam ==VK_DOWN)
{
BeginGame();
}
if(m_GameBegin!=1)
p_Ball->FollowBar(Bar_Below.GetPos ());
}
}
if(m_GameMode == Mode_Martch)
{
if (pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam==VK_RIGHT)
{
Bar_Below.GoRight ();
p_Ball->SetBarPos (Bar_Below.GetPos (),BAR_BELOW);
}
if(pMsg->wParam==VK_LEFT)
{
Bar_Below.GoLeft ();
p_Ball->SetBarPos (Bar_Below.GetPos (),BAR_BELOW);
}
if(pMsg->wParam ==VK_DOWN)
{
if(m_GameBegin !=1)
{
BeginGame();
}
}
if(pMsg->wParam=='C')
{
Bar_Above.GoRight ();
p_Ball->SetBarPos (Bar_Above.GetPos (),BAR_ABOVE);
}
if(pMsg->wParam=='Z')
{
Bar_Above.GoLeft ();
p_Ball->SetBarPos (Bar_Above.GetPos (),BAR_ABOVE);
}
if(pMsg->wParam =='X')
{
if(m_GameBegin !=1)
{
BeginGame();
}
}
if(m_GameBegin!=1)
p_Ball->FollowBar(Bar_Below.GetPos ());
}
}
if(m_GameMode == Mode_Net)
{
if (pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam==VK_RIGHT)
{
Bar_Below.GoRight ();
p_Ball->SetBarPos (Bar_Below.GetPos (),BAR_BELOW);
if(SorC==1)
((CServerThread*)p_Thread)->GoLeft ();
else
((CClientThread*)p_Thread)->GoLeft ();
}
if(pMsg->wParam==VK_LEFT)
{
Bar_Below.GoLeft ();
p_Ball->SetBarPos (Bar_Below.GetPos (),BAR_BELOW);
if(SorC==1)
((CServerThread*)p_Thread)->GoRight ();
else
((CClientThread*)p_Thread)->GoRight ();
}
if(pMsg->wParam ==VK_DOWN)
{
if(SorC==1)
BeginGame();
if(SorC==1)
((CServerThread*)p_Thread)->Start ();
else
((CClientThread*)p_Thread)->Start ();
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CDBallGameDlg::Reset()
{
KillTimer(1);
m_GameBegin = 0;
p_Ball->Reset();
SetTimer(Init_Game,1,NULL);
}
void CDBallGameDlg::BeginGame()
{
m_GameBegin = 1;
SetTimer(1,1,NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -