cloudmsgdlg.cpp

来自「《Visual C++视频技术方案宝典》配套光盘」· C++ 代码 · 共 375 行

CPP
375
字号
// CloudMsgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CloudMsg.h"
#include "CloudMsgDlg.h"
#include "ControlForm.h"
#include "SrvInfo.h"

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


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define CM_READINFO WM_USER+100

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()

/////////////////////////////////////////////////////////////////////////////
// CCloudMsgDlg dialog

CCloudMsgDlg::CCloudMsgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCloudMsgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCloudMsgDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CCloudMsgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCloudMsgDlg)
	DDX_Control(pDX, IDC_ReBrush, m_ReBrush);
	DDX_Control(pDX, IDC_InBrush, m_InBrush);
	DDX_Control(pDX, IDC_InAperture, m_InAperture);
	DDX_Control(pDX, IDC_ReAperture, m_ReAperture);
	DDX_Control(pDX, IDC_ReLen, m_ReLen);
	DDX_Control(pDX, IDC_InLen, m_InLen);
	DDX_Control(pDX, IDC_ReFoci, m_ReFoci);
	DDX_Control(pDX, IDC_InFoci, m_InFoci);
	DDX_Control(pDX, IDC_BUTTONRIGHT, m_ButtonRight);
	DDX_Control(pDX, IDC_BUTTONLEFT, m_ButtonLeft);
	DDX_Control(pDX, IDC_BUTTONUP, m_ButtonUp);
	DDX_Control(pDX, IDC_BUTTONDOWN, m_ButtonDown);
	//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CCloudMsgDlg, CDialog)
	//{{AFX_MSG_MAP(CCloudMsgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONTROL, OnControl)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_LINKSVR, OnLinksvr)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCloudMsgDlg message handlers

BOOL CCloudMsgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	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);
		}
	}

	SetIcon(m_hIcon, TRUE);	
	SetIcon(m_hIcon, FALSE);

	//创建UDP套接字
	m_Client = socket(AF_INET,SOCK_DGRAM,0);
	//获取本机名称
	char name[MAX_PATH];
	memset(name,0,MAX_PATH);
	gethostname(name,MAX_PATH);

	hostent* pTent =  gethostbyname(name);

	sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_addr = (* (in_addr*)pTent->h_addr_list[0]) ;
	addr.sin_port = htonl(60025);

	if (bind(m_Client,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR)
	{
		MessageBox("地址绑定错误");
	}

	m_ButtonUp.m_Type    = ctUp;
	m_ButtonDown.m_Type  = ctDown;
	m_ButtonLeft.m_Type  = ctLeft;
	m_ButtonRight.m_Type = ctRight;
	m_InFoci.m_Type      = ctInFoci;
	m_ReFoci.m_Type      = ctReFoci;
	m_InLen.m_Type       = ctInLen;
	m_ReLen.m_Type       = ctReLen;
	m_InAperture.m_Type  = ctInAperture;
	m_ReAperture.m_Type  = ctReAperture;
	m_InBrush.m_Type     = ctInBrush;
	m_ReBrush.m_Type     = ctReBrush;

	m_Len = GetPrivateProfileInt("使用字节数","字节数",8,"./stage.ini");
	m_ActoinCount = GetPrivateProfileInt("控制项","控制数",13,"./stage.ini");
	m_pData = new  unsigned char[m_ActoinCount][100];

	m_Port = GetPrivateProfileInt("端口设置","端口号",1,"./stage.ini");
	GetPrivateProfileString("端口设置","环境设置","9600,n,8,1",m_Setting.GetBuffer(0),MAX_PATH,"./stage.ini");

	int data;
	char buff[20] = {0};
	char var[20] = {0};

	for (int i = 0 ; i<m_ActoinCount ; i++)
		for (int j = 0 ; j <m_Len; j++)
	{
		char section[20] = "字节";
		itoa(i+1,var,10);
		itoa(j+1,buff,10);
		strcat(section,buff);
		data = GetPrivateProfileInt(var,section,0,"./stage.ini");
		m_pData[i][j] = data;
	}
	return TRUE;  
}

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

void CCloudMsgDlg::OnOK() 
{

}

void CCloudMsgDlg::OnUp()
{
	//向上移动
    SendCmd(0);
}

void CCloudMsgDlg::OnReset()
{

}


void CCloudMsgDlg::OnDown()
{
	//向下移动
     SendCmd(1);
}

void CCloudMsgDlg::OnLeft()
{
	//向左移动
    SendCmd(2);
}

void CCloudMsgDlg::OnRight()
{
	//向右移动
    SendCmd(3);
}

void CCloudMsgDlg::OnInFoci()
{
	//增加聚焦
    SendCmd(5);
}

void CCloudMsgDlg::OnReFoci()
{
	//减小聚焦
    SendCmd(4);
}

void CCloudMsgDlg::OnInLen()
{
	//倍长增
    SendCmd(7);
}

void CCloudMsgDlg::OnReLen()
{
	//倍长减
    SendCmd(6);
}

void CCloudMsgDlg::OnReAperture()
{
	//光圈减
    SendCmd(8);
}

void CCloudMsgDlg::OnInAperture()
{
	//光圈增
    SendCmd(9);
}

void CCloudMsgDlg::OnReBrush()
{
	//雨刷减
    SendCmd(10);
}

void CCloudMsgDlg::OnInBrush()
{
	//雨刷增
    SendCmd(11);
}

void CCloudMsgDlg::OnControl() 
{
	CControlForm CtrlFrm;
	CtrlFrm.DoModal();
}

void CCloudMsgDlg::OnExit() 
{
	OnCancel();	
}

void CCloudMsgDlg::OnCancel()
{
	closesocket(m_Client);
	delete [] m_pData;
	CDialog::OnCancel();
}

void CCloudMsgDlg::SendCmd(int CtrCmd,int StopCmd /*=12*/)
{
	sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_addr.S_un.S_addr = inet_addr(m_Server);
	addr.sin_port = htons(m_SrvPort);
	//定义数据报的格式
	//--报的总长度(1个字节)--云台控制码字节数(1个字节)--控制码数据--停止码数据
	unsigned char * pPackage = new unsigned char [m_Len*2+2];
    memset(pPackage,0,m_Len+2+3);
	//填充数据报
	pPackage[0] = m_Len*2+2;
	pPackage[1] = m_Len  ;
	int index = 2;
	//填充控制码数据
	for (int i = 0 ; i<m_Len; i++,index++)
	{
		pPackage[index] = m_pData[CtrCmd][i];
	}
	//填充停止码数据
	for ( i = 0 ; i<m_Len; i++,index++)
	{
		pPackage[index] = m_pData[StopCmd][i];
	}
	//发送数据报
	sendto(m_Client,(char*)pPackage,(m_Len*2+2)*2,0,(sockaddr*)&addr,sizeof(addr));
	delete [] pPackage;  //equal to delete pPackage
}

void CCloudMsgDlg::OnLinksvr() 
{
    CSrvInfo srvDlg;
    if (srvDlg.DoModal()==IDOK)
    {
        m_Server = srvDlg.m_Server;
        m_SrvPort = srvDlg.m_Port;
    }
}

⌨️ 快捷键说明

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