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

📄 netelectroncltdlg.cpp

📁 网络家电
💻 CPP
字号:
// NetElectronCltDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetElectronClt.h"
#include "NetElectronCltDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CNetElectronCltDlg *g_pNetElectronCltDlg = NULL;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CXPButton	m_btnOK;
	//}}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)
	DDX_Control(pDX, IDOK, m_btnOK);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetElectronCltDlg dialog

CNetElectronCltDlg::CNetElectronCltDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNetElectronCltDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNetElectronCltDlg)
	m_nKongTiaoOpenOrClose = -1;
	m_nWaterHeaterOpenOrClose = -1;
	m_uiPort = 1234;
	m_csSrvIP = _T("127.0.0.1");
	m_nKongTiaoTemperature = 0;
	m_nCurtainTiming = 0;
	m_nMicroWaveOvenTiming = 0;
	m_nWaterHeaterTemperature = 0;
	m_nCurtainOpenOrClose = -1;
	m_nMicroWaveOvenOpenOrClose = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CNetElectronCltDlg::~CNetElectronCltDlg()
{
	m_Sock.Close();
}

void CNetElectronCltDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNetElectronCltDlg)
	DDX_Control(pDX, IDC_BUTTON_COMMIT, m_btnCommit);
	DDX_Control(pDX, IDC_BUTTON_CONNECT, m_btnConnect);
	DDX_Control(pDX, IDC_LIST_LOG_INFO, m_ListLogInfo);
	DDX_Radio(pDX, IDC_RADIO_KONGTIAO_OPEN, m_nKongTiaoOpenOrClose);
	DDX_Radio(pDX, IDC_RADIO_WATER_HEATER_OPEN, m_nWaterHeaterOpenOrClose);
	DDX_Text(pDX, IDC_EDIT_PORT, m_uiPort);
	DDX_Text(pDX, IDC_EDIT_SRV_IP, m_csSrvIP);
	DDX_Text(pDX, IDC_EDIT_KONGTIAO_TEMPERATURE, m_nKongTiaoTemperature);
	DDV_MinMaxInt(pDX, m_nKongTiaoTemperature, -127, 128);
	DDX_Text(pDX, IDC_EDIT_Curtain_TIMING, m_nCurtainTiming);
	DDV_MinMaxUInt(pDX, m_nCurtainTiming, 0, 255);
	DDX_Text(pDX, IDC_EDIT_MicroWaveOven_TIMING, m_nMicroWaveOvenTiming);
	DDV_MinMaxUInt(pDX, m_nMicroWaveOvenTiming, 0, 255);
	DDX_Text(pDX, IDC_EDIT_WATER_HEATER_TEMPERATURE, m_nWaterHeaterTemperature);
	DDV_MinMaxUInt(pDX, m_nWaterHeaterTemperature, 0, 128);
	DDX_Radio(pDX, IDC_RADIO_Curtain_OPEN, m_nCurtainOpenOrClose);
	DDX_Radio(pDX, IDC_RADIO_MicroWaveOven_OPEN, m_nMicroWaveOvenOpenOrClose);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNetElectronCltDlg, CDialog)
	//{{AFX_MSG_MAP(CNetElectronCltDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_COMMIT, OnButtonCommit)
	ON_LBN_DBLCLK(IDC_LIST_LOG_INFO, OnDblclkListLogInfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetElectronCltDlg message handlers

BOOL CNetElectronCltDlg::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
	g_pNetElectronCltDlg = this;
	

	Log ( "系统启动成功!" );
	Log ( "请设置好服务器IP地址和端口后点“连接”按钮" );
	EnableCtrlWindow ( FALSE );
	
	// 将窗口移动带最底端
	int nScrWidth = GetSystemMetrics(SM_CXSCREEN);
    int nScrHeight = GetSystemMetrics(SM_CYSCREEN);
	CRect rc,rcNew;
	GetWindowRect ( &rc );
	ClientToScreen ( &rc );
	rcNew.bottom = nScrHeight-20;
	rcNew.top = rcNew.bottom - rc.Height();
	rcNew.left = ( nScrWidth - rc.Width() ) / 2;
	rcNew.right = rcNew.left + rc.Width();
	MoveWindow ( &rcNew );

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BOOL CNetElectronCltDlg::ConnectSrv()
{
	BOOL bRet = FALSE;
	// 创建一个socket
	bRet = m_Sock.Create ();
	if ( bRet )
	{
		// 连接到服务器
		bRet = m_Sock.Connect ( m_csSrvIP, m_uiPort );
	}

	if ( bRet )
	{
		Log ( "连接服务器 [ %s : %d ] 成功 " ,
				m_csSrvIP, m_uiPort );
	}
	else
	{
		Log ( "连接服务器 [ %s : %d ] 失败 " ,
				m_csSrvIP, m_uiPort );
		OnClose();

		AfxMessageBox ( "连接服务器失败!" );
	}
	return bRet;
}

void CNetElectronCltDlg::OnButtonConnect() 
{
	UpdateData ( TRUE );
	// 改变控件的是否可用状态
	EnableCtrlWindow ( TRUE );
	// 连接到服务器
	ConnectSrv();
}

// 将新的状态显示到控件上去
void CNetElectronCltDlg::SetNewStatus(t_JiaDianState *pJiadianState)
{

	m_nKongTiaoOpenOrClose = !pJiadianState->nKongTiaoOpenOrClose;
	m_nKongTiaoTemperature = pJiadianState->nKongTiaoTemperature;
	m_nWaterHeaterOpenOrClose = !pJiadianState->nWaterHeaterOpenOrClose;
	m_nWaterHeaterTemperature = pJiadianState->nWaterHeaterTemperature;

	m_nMicroWaveOvenOpenOrClose = !pJiadianState->nMicroWaveOvenOpenOrClose;
	m_nMicroWaveOvenTiming = pJiadianState->nMicroWaveOvenTiming;
	m_nCurtainOpenOrClose = !pJiadianState->nCurtainOpenOrClose;
	m_nCurtainTiming = pJiadianState->nCurtainTiming;
	UpdateData ( FALSE );

}

// 从控件的设置中得到当前状态,存到 pJiaDianState 中
void CNetElectronCltDlg::GetCurStatus(t_JiaDianState *pJiaDianState)
{
	UpdateData ( TRUE );
	pJiaDianState->nKongTiaoOpenOrClose = !m_nKongTiaoOpenOrClose;
	pJiaDianState->nKongTiaoTemperature = m_nKongTiaoTemperature;
	pJiaDianState->nWaterHeaterOpenOrClose = !m_nWaterHeaterOpenOrClose;
	pJiaDianState->nWaterHeaterTemperature = m_nWaterHeaterTemperature;

	pJiaDianState->nMicroWaveOvenOpenOrClose = !m_nMicroWaveOvenOpenOrClose;
	pJiaDianState->nMicroWaveOvenTiming = m_nMicroWaveOvenTiming;
	pJiaDianState->nCurtainOpenOrClose = !m_nCurtainOpenOrClose;
	pJiaDianState->nCurtainTiming = m_nCurtainTiming;

}

void CNetElectronCltDlg::OnNewStatus()
{
	t_JiaDianState JiaDianState;
	GetCurStatus ( &JiaDianState );
	SendSockData ( (char*)&JiaDianState, sizeof(t_JiaDianState) );
}

void CNetElectronCltDlg::EnableCtrlWindow(BOOL bCommunicating)
{
	GetDlgItem(IDC_EDIT_SRV_IP)->EnableWindow ( !bCommunicating );
	GetDlgItem(IDC_EDIT_PORT)->EnableWindow ( !bCommunicating );
	GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow ( !bCommunicating );
	GetDlgItem(IDC_BUTTON_COMMIT)->EnableWindow ( bCommunicating );

	GetDlgItem(IDC_RADIO_KONGTIAO_OPEN)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_KONGTIAO_CLOSE)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_WATER_HEATER_OPEN)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_WATER_HEATER_CLOSE)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_MicroWaveOven_OPEN)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_MicroWaveOven_CLOSE)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_Curtain_OPEN)->EnableWindow ( bCommunicating );
	GetDlgItem(IDC_RADIO_Curtain_CLOSE)->EnableWindow ( bCommunicating );
}
// 关闭 socket
void CNetElectronCltDlg::OnClose()
{
	m_Sock.Close();
	EnableCtrlWindow ( FALSE );
}

void CNetElectronCltDlg::OnButtonCommit() 
{
	OnNewStatus();
}

void CNetElectronCltDlg::OnDblclkListLogInfo() 
{
	// 为了保证每行日志信息都能完全看得到,所以添加这个方法。
	int nCurSel = m_ListLogInfo.GetCurSel();
	if ( nCurSel < 0 ) return;
	CString csText;
	m_ListLogInfo.GetText ( nCurSel, csText );
	MessageBox ( csText, "日志信息", MB_ICONINFORMATION );
}

BOOL CNetElectronCltDlg::SendSockData(char *pData, int nSize)
{
	// 将数据通过socket发送给服务器
	Log ( "发送%d字节到服务器:%s", nSize, Data2String(pData,nSize) );
	int nReturned = m_Sock.Send ( (const void*)pData, nSize );
	if ( nReturned != nSize )
	{
		Log ( "发送 %d 字节数据到客户端失败!" );
		return FALSE;
	}
	return TRUE;
}

⌨️ 快捷键说明

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