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

📄 wuzidlg.cpp

📁 一个局域网的五子棋程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// wuziDlg.cpp : implementation file
//
#include "GameSocket.h"
#include "HestoryDlg.h"
#include "stdafx.h"
#include "wuzi.h"
#include "wuziDlg.h"
#include "Node.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 void OnPaint();
	//}}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)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWuziDlg dialog

CWuziDlg::CWuziDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWuziDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWuziDlg)
	m_message = _T("");
	m_show = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	col = 10;
	row = 10;
	cx = 50;
	cy = 50;
	x = 50;
	y = 50;
	shu=1;
	m_IsDown = TRUE;
	m_IsStart = FALSE;
	m_IsWin = FALSE;
	m_parip="";
	m_host="";
	m_run=FALSE;

	//m_IsServer=true;
	
}

void CWuziDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWuziDlg)
	DDX_Control(pDX, IDC_IPADDRESS1, m_IP);
	DDX_Control(pDX, IDC_JTIME, m_curtime);
	DDX_Control(pDX, IDC_DTIME, m_betime);
	DDX_Text(pDX, IDC_MESSAGE, m_message);
	DDX_Text(pDX, IDC_SHOW, m_show);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWuziDlg, CDialog)
	//{{AFX_MSG_MAP(CWuziDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CUT, OnCut)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_WM_CTLCOLOR()
	ON_WM_CHAR()
	
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWuziDlg message handlers

BOOL CWuziDlg::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
     
		m_curtime.SetWindowText("");//消除静态文本中文字
		m_betime.SetWindowText("");
        SetTimer(1,1000,0);
        CreateFolder();//创建聊天记录文件
	for (int i = 0 ;i<row+1;i++)
		for (int j = 0; j<col+1;j++)
		{
			nodes[i][j].m_Point= CPoint(x+cx*j,y+cy*i); 	
		}

	for (int m = 0 ;m<row+1;m++)
		for (int n = 0; n<col+1;n++)
		{
			SetRecentNode(&nodes[m][n]); 	
		}
		InitAllInfo();
		m_sersock.SetParent(this);
		m_cliensock.SetParent(this);
		m_serconsock.SetParent(this);
		//创建服务套接字进行监听

        
			 m_sersock.Create(1000);
		 m_sersock.Listen();
		 //创建客户套接字
		  m_cliensock.Create();
		
		  GetDlgItem(IDC_CUT)->EnableWindow(false);
		GetDlgItem(IDC_START)->EnableWindow(false);
         GetDlgItem(IDC_SEND)->EnableWindow(false);
	GetHost();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWuziDlg::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 CWuziDlg::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();
	}
	CDC* pDC = GetDC();//IDB_BITMAP2
	CBitmap bmp,bmp2,bk;
	CDC memdc;
	memdc.CreateCompatibleDC(pDC);
	bmp.LoadBitmap(IDB_BITMAP3);
	bmp2.LoadBitmap(IDB_BITMAP2);
	bk.LoadBitmap(IDB_BITMAP1);
	memdc.SelectObject(&bk);
	pDC->BitBlt(0,0,600,600,&memdc,0,0,SRCCOPY);

	DrawGrid(row,col);
if (m_IsWin)
	{
		CPen pen(PS_SOLID,2,RGB(255,0,0));
		pDC->SelectObject(&pen);
		pDC->MoveTo(m_Startpt);
		pDC->LineTo(m_Endpt);
	}


	for (int m = 0 ;m<row+1;m++)
		for (int n = 0; n<col+1;n++)
		{
			if (nodes[m][n].m_Color == nWhite)
			{
				memdc.SelectObject(&bmp);
				pDC->BitBlt(nodes[m][n].m_Point.x-11 ,nodes[m][n].m_Point.y-11,22,22,&memdc,0,0,SRCCOPY);	
			}
			else if (nodes[m][n].m_Color == nBlack)
			{
				memdc.SelectObject(&bmp2);
				pDC->BitBlt(nodes[m][n].m_Point.x-11 ,nodes[m][n].m_Point.y-11,22,22,&memdc,0,0,SRCCOPY);				
			}

		}
	bmp.DeleteObject();
	ReleaseDC(&memdc);
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWuziDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWuziDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	

CString str;

SYSTEMTIME   CurTime;   
GetLocalTime(&CurTime);   

str.Format("%d:%02d:%02d",CurTime.wHour,CurTime.wMinute,CurTime.wSecond);  
m_betime.SetWindowText(str);
m_IsStart=TRUE;
GetDlgItem(IDC_START)->EnableWindow(false);
if(shu>1)
m_serconsock.Send("开始",4);
else
{
	m_cliensock.Send("开始",4);
shu=2;
}
if(!m_run)
m_run=TRUE;
}

void CWuziDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	//从ip控件中获得ip并存入m——addr中
	UpdateData(TRUE);
	CString str;
		BYTE f0,f1,f2,f3;
	m_IP.GetAddress(f0,f1,f2,f3);  
	
  m_addr.Format("%d%s%d%s%d%s%d",f0,".",f1,".",f2,".",f3);
  
	GetDlgItem(IDC_CONNECT)->EnableWindow(false);
	GetDlgItem(IDC_IPADDRESS1)->EnableWindow(false);
  m_cliensock.Connect(m_addr,1000);
  if(m_sersock!=NULL)
  m_sersock.Close();
	
  
	
  }
  

	
	
	


void CWuziDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString msg,buf;
	
if(m_message!="")
{
	msg.Format("%s说:%s",m_host,m_message);
buf="M"+m_message;
int n=buf.GetLength();
	if(m_IsServer==TRUE)
	{
		m_serconsock.Send(buf,n);
	}
	else
		m_cliensock.Send(buf,n);
ShowMsg(msg);
}

	m_message="";
	UpdateData(FALSE);

}

void CWuziDlg::DrawGrid(int row,int col)
{
CDC* pDC = GetDC();
	CPen pen(PS_SOLID,1,RGB(0,0,0));
	pDC->SelectObject(&pen);

	for (int i = 0; i<row+1; i++)
	{
		pDC->MoveTo(x+cx*(i),y);
		pDC->LineTo(x+cx*(i),y+row*cy);
	}
	for (int j = 0; j<col+1; j++)
	{
		pDC->MoveTo(x,y+(j)*cy);
		pDC->LineTo(x+col*cx,y+(j)*cy);
	}
}

void CWuziDlg::OnReceive()
{
CString str,strtem ;

char* pbuf=new char[2048];
memset(pbuf,0,2048);
if(m_IsServer)
{
	
	int factlen= m_serconsock.Receive(pbuf,2048);
	str=pbuf;

	if(str=="开始")
	{
		delete pbuf;
    OnStart();
	}
	else if(str.Mid(0,1)=="M")//如果是聊天信息
	{
		delete pbuf;
	strtem=str.Mid(1,str.GetLength()-1);
	CString str1;
	str1.Format("%s说:%s",m_parip,strtem);
    ShowMsg(str1);
	}
	else if(factlen==sizeof(CPoint))//如果是坐标信息
	{
   CPoint* pt = (CPoint*)pbuf;
		NODE* pnode = GetNodeFromPoint(*pt);
		pnode->m_Color = nBlack;
		OnPaint();	
		delete pbuf;
		m_IsDown = TRUE;
	}

	else
	{
     m_Startpt.x = ((CPoint*)pbuf)->x;
		m_Startpt.y = ((CPoint*)pbuf)->y;
		
		m_Endpt.x =  ((CPoint*)(pbuf+sizeof(CPoint)))->x;
		m_Endpt.y =  ((CPoint*)(pbuf+sizeof(CPoint)))->y;

		delete pbuf;
		m_IsDown = FALSE;
		m_IsStart = FALSE;

		m_IsWin = TRUE;
		Invalidate();
		MessageBox("你输了!!!");
	//	KillTimer(1);
		m_betime.SetWindowText("");
		m_IsWin = FALSE;
		InitAllInfo();
		Invalidate();
		GetDlgItem(IDC_START)->EnableWindow(true);
	}
    
}
else
{
	int factlen=m_cliensock.Receive(pbuf,2048);
	str=pbuf;

	if(str=="开始")
	{
		delete pbuf;
		m_parip=m_addr;
		m_IsDown=TRUE;
		GetDlgItem(IDC_SEND)->EnableWindow(true);
	
    GetDlgItem(IDC_START)->EnableWindow(true);
	}
   else if(str.Mid(0,1)=="M")
	{
		delete pbuf;
		CString str1;
	strtem=str.Mid(1,str.GetLength()-1);
	str1.Format("%s说:%s",m_parip,strtem);
    ShowMsg(str1);
	}
   else if(factlen==sizeof(CPoint))//如果是坐标信息
	{
   CPoint* pt = (CPoint*)pbuf;
		NODE* pnode = GetNodeFromPoint(*pt);
		pnode->m_Color = nWhite;
		OnPaint();	
		delete pbuf;
		m_IsDown = TRUE;
	}
    
   else if(str=="拒绝")//拒绝
   {
   m_cliensock.Close();
   m_cliensock.Create();
   m_IsServer=TRUE;
   GetDlgItem(IDC_CONNECT)->EnableWindow(true);
   	GetDlgItem(IDC_SEND)->EnableWindow(true);
	GetDlgItem(IDC_START)->EnableWindow(true);

⌨️ 快捷键说明

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