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

📄 arrestdlg.cpp

📁 多机器人目标围捕 用MFC自行设计了简单的机器人围捕场景。机器人策略采用注册机制易于机器人的策略的扩展。捕捉机器人策略应用了人工势场法。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ArrestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Arrest.h"
#include "ArrestDlg.h"
#include "ARSAction.h"
#include "ARSUtility.h"
#include <winuser.h>

extern void RigestActions();

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

/////////////////////////////////////////////////////////////////////////////
// CArrestDlg dialog

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

void CArrestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CArrestDlg)
	DDX_Control(pDX, IDC_BUTTONSTART, m_butStart);
	DDX_Control(pDX, IDC_COMBOPOLICE6, m_comPolice6);
	DDX_Control(pDX, IDC_COMBOPOLICE5, m_comPolice5);
	DDX_Control(pDX, IDC_COMBOPOLICE4, m_comPolice4);
	DDX_Control(pDX, IDC_COMBOPOLICE1, m_comPolice1);
	DDX_Control(pDX, IDC_COMBOPOLICE3, m_comPolice3);
	DDX_Control(pDX, IDC_COMBOPOLICE2, m_comPolice2);
	DDX_Control(pDX, IDC_COMBOTHIEF, m_comThief);
	DDX_Control(pDX, IDC_COMBOFIELDSIZE, m_comboFieldSize);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CArrestDlg, CDialog)
//{{AFX_MSG_MAP(CArrestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_CBN_SELENDOK(IDC_COMBOFIELDSIZE, OnSelendokCombofieldsize)
ON_BN_CLICKED(IDC_BUTTONRESET, OnButtonreset)
ON_BN_CLICKED(IDC_BUTTONSTART, OnButtonstart)
	ON_CBN_SELENDOK(IDC_COMBOPOLICE1, OnSelendokCombopolice1)
	ON_CBN_SELENDOK(IDC_COMBOPOLICE2, OnSelendokCombopolice2)
	ON_CBN_SELENDOK(IDC_COMBOPOLICE3, OnSelendokCombopolice3)
	ON_CBN_SELENDOK(IDC_COMBOPOLICE4, OnSelendokCombopolice4)
	ON_CBN_SELENDOK(IDC_COMBOPOLICE5, OnSelendokCombopolice5)
	ON_CBN_SELENDOK(IDC_COMBOPOLICE6, OnSelendokCombopolice6)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CArrestDlg message handlers

BOOL CArrestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	
	//////////////////////////////////////////////////////////////////////////
	m_iFieldSize = FIELD_SIZE_SMALL;
	
	m_bMouseDown = false;
	
	m_iPersonNum = -1;
	
	m_bStartPhase = true;
	
	m_iStepsStat = 0;

	m_ThiefMenuDir = STILL;

	m_bKeyControl = false;
	
	InitPersonPosition();
	
	m_comboFieldSize.SetCurSel(0);

	RigestActions();

	ReadStrategy();
	
	//////////////////////////////////////////////////////////////////////////
	
	
	
	// 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
}

// 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 CArrestDlg::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();
		DrawField(m_iFieldSize);
		DrawPerson(false);
		ShowStepsStat();
		
		
	}
}

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


void CArrestDlg::DrawField(int iSize)
{
	
	CPaintDC dc(this);// 设备上下文		
	CWnd* pWnd = GetDlgItem(IDC_STATICFIELD);// 获取绘制坐标的文本框
	CDC* pDC = pWnd->GetDC();// 指针
	
	CBrush whiteBrush(RGB(255,255,255));
	
	CRect backRect(iEDGE1,iEDGE1,iEDGE2,iEDGE2);
	pDC->FillRect(&backRect,&whiteBrush);
	
	CPen* pPenOutside = new CPen;// 创建画笔对象		
	pPenOutside->CreatePen(PS_SOLID,3,RGB(0,0,0));// 画笔
	
	CPen* pPenInside = new CPen;// 创建画笔对象		
	pPenInside->CreatePen(PS_SOLID,1,RGB(0,0,0));// 画笔
	
	CGdiObject* pOldPen = pDC->SelectObject(pPenOutside);// 选中当前画笔,并保存以前的画笔
	
	pDC->MoveTo(iEDGE1,iEDGE1);// 绘制外围方框	
	pDC->LineTo(iEDGE2,iEDGE1);
	pDC->LineTo(iEDGE2,iEDGE2);
	pDC->LineTo(iEDGE1,iEDGE2);
	pDC->LineTo(iEDGE1,iEDGE1);
	
	pDC->SelectObject(pPenInside);
	
	int dist = 0;
	
	for (int i=1;i<iSize;i++)
	{
		dist = (int)((double)i/iSize*(iEDGE2 - iEDGE1));
		//画竖线
		pDC->MoveTo(iEDGE1,iEDGE1+dist);
		pDC->LineTo(iEDGE2,iEDGE1+dist);
		//画横线
		pDC->MoveTo(iEDGE1+dist,iEDGE1);
		pDC->LineTo(iEDGE1+dist,iEDGE2);
		
	}
	
	pDC->SelectObject(pOldPen);	// 恢复旧画笔		
	delete pPenOutside;
	delete pPenInside;
	
	CFont * pOldFont;
	CFont newFont;
	newFont.CreateFont(
		18,                        // nHeight
		0,                         // nWidth
		0,                         // nEscapement
		0,                         // nOrientation
		FW_SEMIBOLD,                 // nWeight
		FALSE,                     // bItalic
		FALSE,                     // bUnderline
		0,                         // cStrikeOut
		ANSI_CHARSET,              // nCharSet
		OUT_DEFAULT_PRECIS,        // nOutPrecision
		CLIP_DEFAULT_PRECIS,       // nClipPrecision
		DEFAULT_QUALITY,           // nQuality
		DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
		"宋体");                 // lpszFacename
	
	CString str;
	str = "步数统计:";	
	
	
	pOldFont = pDC->SelectObject(&newFont);
	
	pDC->TextOut(iEDGE1*2/3+iEDGE2/3,iEDGE2+15,str);
	pDC->SelectObject(pOldFont);
	
	
}

CRect CArrestDlg::XYToRect(int x,int y)
{
	
	CRect tmpRect(0,0,0,0);
	if (x<1||x>m_iFieldSize||y<1||y>m_iFieldSize)
	{
		return tmpRect;
	}
	//坐标转换为屏幕显示坐标
	y = m_iFieldSize - y + 1;
	
	//x,y坐标对应的方格区域
	tmpRect.left = iEDGE1+(int)((double)(x-1)/m_iFieldSize*(iEDGE2 - iEDGE1))+2;
	tmpRect.top = iEDGE1+(int)((double)(y-1)/m_iFieldSize*(iEDGE2 - iEDGE1))+2;
	tmpRect.right =iEDGE1+ (int)((double)x/m_iFieldSize*(iEDGE2 - iEDGE1))-1;
	tmpRect.bottom = iEDGE1+(int)((double)y/m_iFieldSize*(iEDGE2 - iEDGE1))-1;
	
	return tmpRect;	
	
}

void CArrestDlg::DrawRound(int x,int y,bool isErased)
{
	
	CPaintDC dc(this);// 设备上下文		
	CWnd* pWnd = GetDlgItem(IDC_STATICFIELD);// 获取绘制坐标的文本框
	CDC* pDC = pWnd->GetDC();// 指针
	
	CBrush whiteBrush(RGB(255,255,255));
	CBrush redBrush(RGB(255,0,0));
	CBrush * oldBrush; 
	
	CPen* pPenWhite = new CPen;// 创建画笔对象		
	pPenWhite->CreatePen(PS_SOLID,1,RGB(255,255,255));// 画笔
	CPen* pPenRed = new CPen;// 创建画笔对象		
	pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));// 画笔
	
	CGdiObject* pOldPen; 
	
	CRect tmpRect;
	
	if (isErased)
	{//擦除旧点
		oldBrush = pDC->SelectObject(&whiteBrush);
		pOldPen = pDC->SelectObject(pPenWhite);
		tmpRect = XYToRect(x,y);
		pDC->Ellipse(&tmpRect);
		pDC->SelectObject(&oldBrush);
		pDC->SelectObject(&pOldPen);
	}
	else
	{
		oldBrush = pDC->SelectObject(&redBrush);
		pOldPen = pDC->SelectObject(pPenRed);
		tmpRect = XYToRect(x,y);
		pDC->Ellipse(&tmpRect);
		pDC->SelectObject(&oldBrush);
		pDC->SelectObject(&pOldPen);
	}
	delete pPenWhite;
	delete pPenRed;
}

void CArrestDlg::DrawRect(int x,int y,bool isErased)
{
	CPaintDC dc(this);// 设备上下文		
	CWnd* pWnd = GetDlgItem(IDC_STATICFIELD);// 获取绘制坐标的文本框
	CDC* pDC = pWnd->GetDC();// 指针
	
	CBrush whiteBrush(RGB(255,255,255));
	CBrush blueBrush(RGB(0,0,255));
	
	CFont * pOldFont;
	CFont newFont;
	newFont.CreateFont(
		13,                        // nHeight
		0,                         // nWidth
		0,                         // nEscapement
		0,                         // nOrientation
		FW_SEMIBOLD,                 // nWeight
		FALSE,                     // bItalic
		FALSE,                     // bUnderline
		0,                         // cStrikeOut
		ANSI_CHARSET,              // nCharSet
		OUT_DEFAULT_PRECIS,        // nOutPrecision
		CLIP_DEFAULT_PRECIS,       // nClipPrecision
		DEFAULT_QUALITY,           // nQuality
		DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
		"Arial");                 // lpszFacename
	
	
	
	CRect tmpRect;
	
	if (isErased)
	{//擦除旧点
		tmpRect = XYToRect(x,y);
		pDC->FillRect(&tmpRect,&whiteBrush);
		
	}
	else
	{		
		tmpRect = XYToRect(x,y);
		pDC->FillRect(&tmpRect,&blueBrush);	
		
		CString num;
		num.Format("%d",m_iPersonNum);
		
		pOldFont = pDC->SelectObject(&newFont);
		
		pDC->TextOut(tmpRect.left,tmpRect.top,num);
		pDC->SelectObject(pOldFont);
	}
}


bool CArrestDlg::PointToXY(const CPoint &ptMouse,long &x,long &y)
{
	if (ptMouse.x<=iEDGE1||ptMouse.y<=iEDGE1||ptMouse.x>=iEDGE2||ptMouse.y>=iEDGE2)
	{
		return false;
	}
	x = (int)((double)(ptMouse.x  -5- iEDGE1)/(iEDGE2 - iEDGE1)*m_iFieldSize)+1;
	y = (int)((double)(ptMouse.y  -5- iEDGE1)/(iEDGE2 - iEDGE1)*m_iFieldSize)+1;
	y= m_iFieldSize -y +1;
	return true;
}

void CArrestDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	if (m_bStartPhase)//出于程序开始阶段
	{
		CPoint choicePt; //选中坐标
		choicePt.x = choicePt.y = -1;
		int choicePerson = -1; //选中人的编号
		if(PointToXY(point,choicePt.x,choicePt.y)) //转换为场地坐标
		{
			if (ChoicePerson(choicePt.x,choicePt.y,choicePerson)) //选中人
			{
				SetCapture();		//获取鼠标操作(可记录客户区外鼠标操作)
				m_bMouseDown = true;
				m_tmpPoint =  choicePt;//记录场地坐标				 
				m_iPersonNum = choicePerson; //记录选中的人	
			}
		}
	}
	
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CArrestDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_bMouseDown)
	{
		ReleaseCapture();
		m_bMouseDown = false;
		m_ptPerson[m_iPersonNum].x = m_tmpPoint.x;
		m_ptPerson[m_iPersonNum].y = m_tmpPoint.y;		
	}
	CDialog::OnLButtonUp(nFlags, point);
}

void CArrestDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	if (m_bMouseDown)
	{
		CPoint choicePt;
		choicePt.x = choicePt.y = -1;
		int choicePerson = -1;
		if(PointToXY(point,choicePt.x,choicePt.y))
		{
			if (choicePt!=m_tmpPoint&&(!ChoicePerson(choicePt.x,choicePt.y,choicePerson)))
			{
				
				if (m_iPersonNum == 0)
				{
					DrawRound(m_tmpPoint.x,m_tmpPoint.y,true);
				}
				if (m_iPersonNum>0&&m_iPersonNum<MAX_NUM_PERSON)
				{
					DrawRect(m_tmpPoint.x,m_tmpPoint.y,true);
				}
				m_tmpPoint = choicePt;
				if (m_iPersonNum == 0)
				{
					DrawRound(m_tmpPoint.x,m_tmpPoint.y,false);
				}
				if (m_iPersonNum>0&&m_iPersonNum<MAX_NUM_PERSON)
				{
					DrawRect(m_tmpPoint.x,m_tmpPoint.y,false);
				}	
				
			}
		}
		
	}
	
	
	CDialog::OnMouseMove(nFlags, point);
}

void CArrestDlg::DrawPerson(bool bErase)
{
	for (int i = 1;i<MAX_NUM_PERSON;i++)
	{
		if (m_ptPerson[i].x>0&&m_ptPerson[i].y>0)
		{
			m_iPersonNum = i;
			DrawRect(m_ptPerson[i].x,m_ptPerson[i].y,bErase);
		}		
	}
	DrawRound(m_ptPerson[0].x,m_ptPerson[0].y,bErase);
}

bool CArrestDlg::ChoicePerson(const int&x,const int &y,int &choicePerson)
{
	if (m_ptPerson[0].x == x&&m_ptPerson[0].y == y)
	{
		choicePerson = 0;
		return true;
	}
	for (int i = 1;i<MAX_NUM_PERSON;i++)
	{
		if (m_ptPerson[i].x>0&&m_ptPerson[i].y>0)
		{
			if (m_ptPerson[i].x == x&&m_ptPerson[i].y == y)
			{
				choicePerson = i;
				return true;
			}
		}		
	}
	return false;
	
}

void CArrestDlg::OnSelendokCombofieldsize() 
{
	// TODO: Add your control notification handler code here
	if (!m_bStartPhase)
	{
		return;
	}
	switch(	m_comboFieldSize.GetCurSel())
	{
	case 0:
		if (m_iFieldSize == 15)
			return;		
		m_iFieldSize = 15;
		break;
	case 1:
		if (m_iFieldSize == 20)
			return;	
		m_iFieldSize = 20;
		break;
	case 2:
		if (m_iFieldSize == 25)
			return;	
		m_iFieldSize = 25;
		break;
	}
	InitPersonPosition();
	DrawField(m_iFieldSize);
	DrawPerson(false);

	m_comThief.SetCurSel(1);
	m_comPolice1.SetCurSel(1);
	m_comPolice2.SetCurSel(1);
	m_comPolice3.SetCurSel(1);
	m_comPolice4.SetCurSel(1);
	m_comPolice5.SetCurSel(0);
	m_comPolice6.SetCurSel(0);

	
}


void CArrestDlg::InitPersonPosition()
{
	m_ptPerson[0].x = m_iFieldSize/2+1;
	m_ptPerson[0].y = m_iFieldSize/2+1;
	
	m_ptPerson[1].x = 1;
	m_ptPerson[1].y = 1;
	m_ptPerson[2].x = m_iFieldSize;
	m_ptPerson[2].y = 1;
	m_ptPerson[3].x = 1;
	m_ptPerson[3].y = m_iFieldSize;
	m_ptPerson[4].x = m_iFieldSize;
	m_ptPerson[4].y = m_iFieldSize;
	
	for (int i = 5;i<MAX_NUM_PERSON;i++)
	{
		m_ptPerson[i].x = 0;
		m_ptPerson[i].y = 0;

⌨️ 快捷键说明

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