📄 aiantdlg.cpp
字号:
// AIAntDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AIAnt.h"
#include "AIAntDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CAIAntDlg dialog
CAIAntDlg::CAIAntDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAIAntDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAIAntDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAIAntDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAIAntDlg)
DDX_Control(pDX, IDC_SINGLE, m_single);
DDX_Control(pDX, IDC_RESET, m_reset);
DDX_Control(pDX, IDC_AUTO, m_auto);
DDX_Control(pDX, IDC_COMBO_AIANT, m_aiantchoice);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAIAntDlg, CDialog)
//{{AFX_MSG_MAP(CAIAntDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_AUTO, OnAuto)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_SINGLE, OnSingle)
ON_WM_TIMER()
ON_CBN_SELCHANGE(IDC_COMBO_AIANT, OnSelchangeComboAiant)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAIAntDlg message handlers
BOOL CAIAntDlg::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
InitGrid();
m_aiantchoice.SetCurSel(0);
antnum=1;
ant.SetPos(13);
ant.SetDir(1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAIAntDlg::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 CAIAntDlg::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
{
CPaintDC dc(this),*mydc;
mydc=&dc;
//画框
dc.MoveTo(19,29);
dc.LineTo(380,29);
dc.LineTo(380,390);
dc.LineTo(19,390);
dc.LineTo(19,29);
//画坐标系
/* dc.MoveTo(550,50);
dc.LineTo(600,50);
dc.LineTo(595,45);
dc.MoveTo(600,50);
dc.LineTo(595,55);
dc.MoveTo(575,75);
dc.LineTo(575,25);
dc.LineTo(570,30);
dc.MoveTo(575,25);
dc.LineTo(580,30);
*/
//画图例
DrawCutline(mydc,430,295,0);
DrawCutline(mydc,430,322,1);
DrawCutline(mydc,430,349,2);
DrawCutline(mydc,430,376,3);
//画棋盘和蚂蚁及方向
Draw();
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAIAntDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAIAntDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
//if (pant) delete pant;
CDialog::OnClose();
}
void CAIAntDlg::DrawGrid(CDC *dc,const int pos, const int type)
{
int i,j;
i=pos%12;
j=pos/12;
dc->Rectangle(20+i*30,30+j*30,20+(i+1)*30,30+(j+1)*30);
switch (type)
{
case 0: break;
case 1: dc->FillSolidRect(21+i*30,31+j*30,28,28,RGB(0,0,255));break;
case 2: dc->FillSolidRect(21+i*30,31+j*30,28,28,RGB(0,255,0));break;
case 3://东
{
CPoint point[8];
point[0].x=25+30*i;
point[0].y=40+30*j;
point[1].x=37+30*i;
point[1].y=40+30*j;
point[2].x=37+30*i;
point[2].y=37+30*j;
point[3].x=45+30*i;
point[3].y=45+30*j;
point[4].x=37+30*i;
point[4].y=53+30*j;
point[5].x=37+30*i;
point[5].y=50+30*j;
point[6].x=25+30*i;
point[6].y=50+30*j;
point[7].x=25+30*i;
point[7].y=40+30*j;
dc->FillSolidRect(21+i*30,31+j*30,28,28,RGB(255,255,0));
dc->Polyline(point,8);
break;
};
case 4://南
{
CPoint point[8];
point[0].x=30+30*i;
point[0].y=35+30*j;
point[1].x=40+30*i;
point[1].y=35+30*j;
point[2].x=40+30*i;
point[2].y=47+30*j;
point[3].x=43+30*i;
point[3].y=47+30*j;
point[4].x=35+30*i;
point[4].y=55+30*j;
point[5].x=27+30*i;
point[5].y=47+30*j;
point[6].x=30+30*i;
point[6].y=47+30*j;
point[7].x=30+30*i;
point[7].y=35+30*j;
dc->FillSolidRect(21+i*30,31+j*30,28,28,RGB(255,255,0));
dc->Polyline(point,8);
break;
};
case 5://西
{
CPoint point[8];
point[0].x=24+30*i;
point[0].y=45+30*j;
point[1].x=32+30*i;
point[1].y=37+30*j;
point[2].x=32+30*i;
point[2].y=40+30*j;
point[3].x=44+30*i;
point[3].y=40+30*j;
point[4].x=44+30*i;
point[4].y=50+30*j;
point[5].x=32+30*i;
point[5].y=50+30*j;
point[6].x=32+30*i;
point[6].y=53+30*j;
point[7].x=24+30*i;
point[7].y=45+30*j;
dc->FillSolidRect(21+i*30,31+j*30,28,28,RGB(255,255,0));
dc->Polyline(point,8);
break;
};
case 6://北
{
CPoint point[8];
point[0].x=35+30*i;
point[0].y=35+30*j;
point[1].x=43+30*i;
point[1].y=43+30*j;
point[2].x=40+30*i;
point[2].y=43+30*j;
point[3].x=40+30*i;
point[3].y=55+30*j;
point[4].x=30+30*i;
point[4].y=55+30*j;
point[5].x=30+30*i;
point[5].y=43+30*j;
point[6].x=27+30*i;
point[6].y=43+30*j;
point[7].x=35+30*i;
point[7].y=35+30*j;
dc->FillSolidRect(21+i*30,31+j*30,28,28,RGB(255,255,0));
dc->Polyline(point,8);
break;
};
}
}
void CAIAntDlg::DrawCutline(CDC *dc, const int x, const int y, const int type)
{
switch(type)
{
case 0: dc->Rectangle(x,y,20+x,20+y);break;//无信息素
case 1: dc->Rectangle(x,y,20+x,20+y);dc->FillSolidRect(x+1,y+1,18,18,RGB(0,0,255));break;//有信息素,状态为关
case 2: dc->Rectangle(x,y,20+x,20+y);dc->FillSolidRect(x+1,y+1,18,18,RGB(0,255,0));break;//有信息素,状态为开
case 3: //蚂蚁图例
{
CPoint point[8];
dc->Rectangle(x,y,20+x,20+y);
dc->FillSolidRect(x+1,y+1,18,18,RGB(255,255,0));
point[0].x=x+3;
point[0].y=y+7;
point[1].x=x+12;
point[1].y=y+7;
point[2].x=x+12;
point[2].y=y+5;
point[3].x=x+17;
point[3].y=y+10;
point[4].x=x+12;
point[4].y=y+15;
point[5].x=x+12;
point[5].y=y+13;
point[6].x=x+3;
point[6].y=y+13;
point[7].x=x+3;
point[7].y=y+7;
dc->Polyline(point,8);
break;
}
case 4:
{
dc->Rectangle(x,y,30+x,30+y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -