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

📄 waipcdlg.cpp

📁 VC++高级编程技巧与示例
💻 CPP
字号:
// WAIPCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WAIPC.h"
#include "WAIPCDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CWAPICDlg dialog

CWAPICDlg::CWAPICDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWAPICDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWAPICDlg)
		// 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 CWAPICDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWAPICDlg)
	DDX_Control(pDX, IDC_ANGULAR, m_Angular);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWAPICDlg, CDialog)
	//{{AFX_MSG_MAP(CWAPICDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_CAL, OnCal)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWAPICDlg message handlers

BOOL CWAPICDlg::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
	CButton * btn;
	btn=(CButton *)GetDlgItem(IDC_ANGULAR);
	btn->SetCheck(1);
	CEdit * edit;
	edit=(CEdit *)GetDlgItem(IDC_EDIT1);
	edit->SetFocus();
	edit->SetSel(0,-1);
//1. 准备数据:
	memset(&m_data,0,sizeof(m_data));
	while(m_data.m_strName[0]=='\0'||strAddr=="")
	{
		if(m_PortDlg.DoModal ()!=IDOK)
		{
			::PostQuitMessage(0);
			return FALSE;
		}
		//服务器地址:
		strAddr=m_PortDlg.m_Srvr;
		//通信端口:
		nPort  =m_PortDlg.m_Port;
		//客户名称:
		m_Name=m_PortDlg.m_Name;
		strcpy(m_data.m_strName,m_Name);
		if(strAddr==""||m_data.m_strName[0]=='\0')
		{
			AfxMessageBox("服务器地址或客户名称不能为空字符串!");
		}
	}
	m_data.m_bOnline=TRUE;
	strcpy(m_data.m_strAction,"Join the session!");
//2. 进行连接:
	try
	{
		m_Sock.Create(nPort,strAddr);
		while(!m_Sock.Connect())
		{
			if(AfxMessageBox("同服务器的连接被拒绝,是否重试?",
				              MB_RETRYCANCEL|MB_ICONQUESTION)
			    ==IDCANCEL)
			{
				::PostQuitMessage(0);
				return FALSE;
			}
		}
		//通知服务器,客户进入:
		m_Sock.Send((char *)&m_data,sizeof(_DATA));
	}
	catch(CMTSocketException * e)
	{
		AfxMessageBox(e->GetErrorMsg());
		delete e;
		::PostQuitMessage(0);
		return FALSE;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWAPICDlg::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 CWAPICDlg::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 CWAPICDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWAPICDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
	if(m_Sock.GetSocket()==NULL)
	{
		return;
	}
	try
	{
		memset(&m_data,0,sizeof(m_data));
		m_data.m_bOnline=FALSE;
		strcpy(m_data.m_strName,m_Name);
		strcpy(m_data.m_strAction,"Leave the sesion");
		m_Sock.Send((char *)&m_data,sizeof(m_data));
		m_Sock.Close();
	}
	catch(CMTSocketException * e)
	{
		AfxMessageBox(e->GetErrorMsg());
		delete e;
	}
}

void CWAPICDlg::OnCal() 
{
	// TODO: Add your control notification handler code here
	CString str;
	try
	{
		SetDlgItemText(IDC_RESULT,"");
		GetDlgItemText(IDC_EDIT1,str);
		memset(&m_data,0,sizeof(m_data));
		m_data.m_bOnline=TRUE;
		m_data.m_bAngular=m_Angular.GetCheck();
		m_data.m_dbData=atof(str);
		str="Calculating sin("+str+")";
		strcpy(m_data.m_strAction,str);
		strcpy(m_data.m_strName,m_Name);
		//将角度发送给服务器:
		m_Sock.Send((char *)&m_data,sizeof(m_data));
		//从服务器读取计算结果:
		m_Sock.Receive((char *)&m_data,sizeof(m_data));
		//设置对话框控件属性:
		str.Format("%f",m_data.m_dbData);
		SetDlgItemText(IDC_RESULT,str);
		CEdit * edit;
		edit=(CEdit * )GetDlgItem(IDC_EDIT1);
		edit->SetFocus();
		edit->SetSel(0,-1);
	}
	catch(CMTSocketException * e)
	{
		AfxMessageBox(e->GetErrorMsg());
		(CButton * )GetDlgItem(IDC_CAL)->EnableWindow(FALSE);
		delete e;
	}
}

⌨️ 快捷键说明

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