📄 demo4dlg.cpp
字号:
// Demo4Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Demo4.h"
#include "Demo4Dlg.h"
#include "UdpData.h"
#include "HangJi.h"
#include "ModifyBp.h"
#include "proj.h"
#include <time.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()
/////////////////////////////////////////////////////////////////////////////
// CDemo4Dlg dialog
CDemo4Dlg::CDemo4Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemo4Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemo4Dlg)
// 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);
m_aList.RemoveAll();
}
void CDemo4Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemo4Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemo4Dlg, CDialog)
//{{AFX_MSG_MAP(CDemo4Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_MODIFY_BP, OnModifyBp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemo4Dlg message handlers
BOOL CDemo4Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_pUdp = new CUdpData(this);
if(!m_pUdp->Create(InterNetUdpPort[4], SOCK_DGRAM))
{
return false;
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CDemo4Dlg::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 CDemo4Dlg::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 CDemo4Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
TRACK0_10H_A* CDemo4Dlg::GetListData(CString& bp)
{
int i;
for(i = m_aList.GetSize()-1; i >= 0; i--)
{
TRACK0_10H_A* t = (TRACK0_10H_A*)m_aList.GetAt(i);
if(bp == t->t.bp)
{
return t;
}
}
return NULL;
}
int CDemo4Dlg::GetListPos(CString& bp)
{
int i;
for(i = m_aList.GetSize()-1; i >= 0; i--)
{
TRACK0_10H_A* t = (TRACK0_10H_A*)m_aList.GetAt(i);
if(bp == t->t.bp)
{
return i;
}
}
return -1;
}
void CDemo4Dlg::FreshTime()
{//更新航迹时间
int i;
time_t tim;
time( &tim );
for(i = m_aList.GetSize()-1; i >= 0; i--)
{
((TRACK0_10H_A*)m_aList.GetAt(i))->t.t = tim;
}
}
void CDemo4Dlg::FreshList()
{
int i, j = m_aList.GetSize();
CListBox* list = (CListBox*)GetDlgItem(IDC_LIST);
list->ResetContent();
for(i = 0; i < j; i++)
{
TRACK0_10H_A* t = (TRACK0_10H_A*)m_aList.GetAt(i);
list->AddString(t->t.bp);
}
}
void CDemo4Dlg::OnDestroy()
{
// TODO: Add your message handler code here
if(m_pUdp)
{
BYTE Buffer[50];
m_pUdp->ShutDown();
while (m_pUdp->Receive(Buffer,50) > 0);
delete m_pUdp;
m_pUdp = NULL;
}
KillTimer(0);
int i;
for(i=m_aList.GetSize()-1; i >=0; i--)
delete (TRACK0_10H_A*)m_aList.GetAt(i);
m_aList.RemoveAll();
CDialog::OnDestroy();
}
void CDemo4Dlg::OnTimer(UINT nIDEvent)
{//定时处理程序
// TODO: Add your message handler code here and/or call default
int i;
CMapProj proj;
time_t nowtime;
time( &nowtime);
for(i=m_aList.GetSize()-1; i >= 0; i--)
{//遍历报文列表
TRACK0_10H_A* t = (TRACK0_10H_A*)m_aList.GetAt(i);
if( t->select >= 0 && t->select <4 && (nowtime - t->t.t) >= t->dt )
{//发送时间间隔大于该报文设定间隔的报文
proj.inverse_rhumb1(t->t.w, t->t.j, t->t.hx, t->t.hs * 1852 / 3600 * (nowtime - t->t.t), &(t->t.w), &(t->t.j));
t->t.t = nowtime;
m_pUdp->SendTo((void*)&(t->t), sizeof(TRACK0_10H),
InterNetUdpPort[t->select], InternetAddress[t->select]);
}
}
CDialog::OnTimer(nIDEvent);
}
void CDemo4Dlg::OnAdd()
{//增加航迹
// TODO: Add your control notification handler code here
int i;
CHangJi dlg;
dlg.DoModal();
if(dlg.m_strBp.IsEmpty())
{
AfxMessageBox("必须输入标牌内容!");
return;
}
TRACK0_10H_A* t = new TRACK0_10H_A;
dlg.WriteData(t);
time( &(t->t.t) );
CListBox* list = (CListBox*)GetDlgItem(IDC_LIST);
CString item;
for(i=list->GetCount()-1; i >= 0; i--)
{
list->GetText(i, item);
if(item == t->t.bp)
{
AfxMessageBox("标牌重复!");
delete t;
return;
}
}
m_aList.Add(t);
list->AddString(t->t.bp);
}
void CDemo4Dlg::OnStart()
{//开始模拟,启动定时器
// TODO: Add your control notification handler code here
SetTimer(0, 1000, NULL);
//OnTimer(0);
}
void CDemo4Dlg::OnStop()
{//停止模拟,停止定时器
// TODO: Add your control notification handler code here
KillTimer(0);
}
void CDemo4Dlg::OnSelchangeList()
{//记录当前的选择项
// TODO: Add your control notification handler code here
CListBox* list = (CListBox*)GetDlgItem(IDC_LIST);
int n = list->GetCurSel();
list->GetText(n, m_strSelItem);
}
void CDemo4Dlg::OnModify()
{//修改航迹数据,但不能修改标牌
// TODO: Add your control notification handler code here
TRACK0_10H_A* t;
if( !(t = GetListData(m_strSelItem)) )
{
AfxMessageBox("请首先选择列表项!");
return;
}
CHangJi dlg;
dlg.ReadData(t);
dlg.DoModal();
//更新数据
t->t.corp = 1<<dlg.m_nSelect;
t->select = dlg.m_nSelect;
t->dt = dlg.m_nDt;
t->t.hs = dlg.m_fHs;
t->t.hx = dlg.m_fHx;
t->t.j = dlg.m_fJing;
t->t.w = dlg.m_fWei;
t->t.lx = dlg.m_nLx;
}
void CDemo4Dlg::OnDelete()
{
// TODO: Add your control notification handler code here
int i;
if( (i = GetListPos(m_strSelItem)) == -1 )
{
AfxMessageBox("请首先选择列表项!");
return;
}
TRACK0_10H_A* t = GetListData(m_strSelItem);
m_aList.RemoveAt(i);
TRACK1_11H t1;
t1.bh = 0x11;
strncpy(t1.bp, m_strSelItem, MAX_BP_LEN-1);
t1.Act = 0;
if(t->select >= 0 && t->select <4)
{//目标消失
m_pUdp->SendTo((void*)&(t1), sizeof(TRACK1_11H),
InterNetUdpPort[t->select], InternetAddress[t->select]);
}
CListBox* list = (CListBox*)GetDlgItem(IDC_LIST);
i = list->FindString(0, m_strSelItem);
list->DeleteString(i);
delete t;
}
void CDemo4Dlg::OnModifyBp()
{
// TODO: Add your control notification handler code here
TRACK0_10H_A* t;
if( !(t = GetListData(m_strSelItem)) )
{
AfxMessageBox("请首先选择列表项!");
return;
}
CModifyBp dlg;
dlg.m_strBp = m_strSelItem;
if(dlg.DoModal() == IDOK)
{//更新数据
strncpy(t->t.bp, dlg.m_strBp, MAX_BP_LEN);
FreshList();
TRACK1_11H t1;
t1.bh = 0x11;
t1.Act = 1;
strncpy(t1.bp, m_strSelItem, MAX_BP_LEN);//原标牌
strncpy(t1.bp1, dlg.m_strBp, MAX_BP_LEN);//新标牌
if(t->select >= 0 && t->select <4)
{//更改标牌
m_pUdp->SendTo((void*)&(t1), sizeof(TRACK1_11H),
InterNetUdpPort[t->select], InternetAddress[t->select]);
}
m_strSelItem.Empty();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -