📄 schoolguiderdlg.cpp
字号:
// SchoolGuiderDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SchoolGuider.h"
#include "SchoolGuiderDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define VERTICES 19 // 景点数目
#define SELDPATHCOLOR RGB(200, 0,0) // 选中路径的颜色
SCHOOL_POINT points[] =
{
{ 366, 271, "图书馆"},
{ 224, 271, " " },
{ 224, 216 , "学生一食堂"},
{ 224, 98, "田家炳体育馆"},
{ 312, 98, "师生超市"},
{ 430, 98, "男生宿舍3#"},
{ 532, 98, "男生宿舍4#"},
{ 562, 98, " " },
{ 562, 124, "学生二食堂"},
{ 562, 233, "第一教学楼"},
{ 562, 271, " " },
{ 562, 350, "第二教学楼"},
{ 562, 400, " " },
{ 366, 400, "主楼正门"},
{ 224, 400, "小南门" },
{ 224 ,379, " " },
{ 152, 379 , "女生宿舍1#"},
{ 152, 314 , "女生宿舍2#"},
{ 224, 314, " " }
};
int g_iViaPointNum = 0;
int *g_iPointIndex = NULL;
HCURSOR g_hDefalutCursor = NULL;
HCURSOR g_hSelectCursor = NULL;
WNDPROC wpOrigMapCtrlProc = NULL;
extern CGraph SchoolGuide;
LRESULT CALLBACK MapCtrlWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/////////////////////////////////////////////////////////////////////////////
// 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)
//virtual void OnOK();
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CSchoolGuiderDlg dialog
CSchoolGuiderDlg::CSchoolGuiderDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSchoolGuiderDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSchoolGuiderDlg)
m_pathType = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
g_hSelectCursor = AfxGetApp()->LoadCursor(IDC_HAND);
g_hDefalutCursor = AfxGetApp()->LoadCursor(IDC_POINTER);
m_bFirstPointSelected = FALSE;
}
void CSchoolGuiderDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSchoolGuiderDlg)
DDX_Control(pDX, IDC_MAP, m_mapCtrl);
DDX_Control(pDX, IDC_SHOWLENTH_STATIC, m_showLenStatic);
DDX_Control(pDX, IDC_SHOWPATH_STATIC, m_showPathStatic);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSchoolGuiderDlg, CDialog)
//{{AFX_MSG_MAP(CSchoolGuiderDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_HELP_ACCOUNT, OnHelpAccount)
ON_BN_CLICKED(IDC_MAP, OnMapClick)
ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClean)
ON_BN_CLICKED(IDC_OPTIMUM_BUTTON, OnOptimum)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSchoolGuiderDlg message handlers
BOOL CSchoolGuiderDlg::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
wpOrigMapCtrlProc = (WNDPROC)SetWindowLong(m_mapCtrl.GetSafeHwnd(), GWL_WNDPROC, (LPARAM)MapCtrlWndProc);
for (int i= 0; i<VERTICES;i++)
{
for (int j= 0; j<VERTICES;j++)
{
SchoolGuide.InsertEdge(i,j,MAXINT);
if (i == j)
SchoolGuide.InsertEdge(i,j,0);
}
}
SchoolGuide.InsertEdge(0,1,100);
SchoolGuide.InsertEdge(1,0,100);
SchoolGuide.InsertEdge(0,10,90);
SchoolGuide.InsertEdge(10,0,90);
SchoolGuide.InsertEdge(1,2,60);
SchoolGuide.InsertEdge(2,1,60);
SchoolGuide.InsertEdge(2,3,80);
SchoolGuide.InsertEdge(3,2,80);
SchoolGuide.InsertEdge(3,4,90);
SchoolGuide.InsertEdge(4,3,90);
SchoolGuide.InsertEdge(4,5,70);
SchoolGuide.InsertEdge(5,4,70);
SchoolGuide.InsertEdge(6,5,30);
SchoolGuide.InsertEdge(5,6,30);
SchoolGuide.InsertEdge(6,7,20);
SchoolGuide.InsertEdge(7,6,20);
SchoolGuide.InsertEdge(7,8,60);
SchoolGuide.InsertEdge(8,7,60);
SchoolGuide.InsertEdge(8,9,40);
SchoolGuide.InsertEdge(9,8,40);
SchoolGuide.InsertEdge(9,10,30);
SchoolGuide.InsertEdge(10,9,30);
SchoolGuide.InsertEdge(10,11,90);
SchoolGuide.InsertEdge(11,10,90);
SchoolGuide.InsertEdge(11,12,50);
SchoolGuide.InsertEdge(12,11,50);
SchoolGuide.InsertEdge(12,13,90);
SchoolGuide.InsertEdge(13,12,90);
SchoolGuide.InsertEdge(13,14,100);
SchoolGuide.InsertEdge(14,13,100);
SchoolGuide.InsertEdge(14,15,30);
SchoolGuide.InsertEdge(15,14,30);
SchoolGuide.InsertEdge(15,16,30);
SchoolGuide.InsertEdge(16,15,30);
SchoolGuide.InsertEdge(16,17,30);
SchoolGuide.InsertEdge(17,16,30);
SchoolGuide.InsertEdge(15,18,50);
SchoolGuide.InsertEdge(18,15,50);
SchoolGuide.InsertEdge(17,18,40);
SchoolGuide.InsertEdge(18,17,40);
SchoolGuide.InsertEdge(1,18,40);
SchoolGuide.InsertEdge(18,1,40);
SchoolGuide.InsertEdge(18,14,80);
SchoolGuide.InsertEdge(14,18,80);
SchoolGuide.InsertEdge(1,14,120);
SchoolGuide.InsertEdge(14,1,120);
SchoolGuide.InsertEdge(2,14,180);
SchoolGuide.InsertEdge(14,2,180);
SchoolGuide.InsertEdge(3,14,270);
SchoolGuide.InsertEdge(14,3,270);
SchoolGuide.InsertEdge(15,1,90);
SchoolGuide.InsertEdge(1,15,90);
SchoolGuide.InsertEdge(15,2,150);
SchoolGuide.InsertEdge(2,15,150);
SchoolGuide.InsertEdge(15,3,240);
SchoolGuide.InsertEdge(3,15,240);
SchoolGuide.InsertEdge(2,18,100);
SchoolGuide.InsertEdge(18,2,100);
SchoolGuide.InsertEdge(3,18,190);
SchoolGuide.InsertEdge(18,3,190);
SchoolGuide.InsertEdge(1,3,150);
SchoolGuide.InsertEdge(3,1,150);
SchoolGuide.InsertEdge(3,5,150);
SchoolGuide.InsertEdge(5,3,150);
SchoolGuide.InsertEdge(3,6,180);
SchoolGuide.InsertEdge(6,3,180);
SchoolGuide.InsertEdge(3,7,200);
SchoolGuide.InsertEdge(7,3,200);
SchoolGuide.InsertEdge(4,6,90);
SchoolGuide.InsertEdge(6,4,90);
SchoolGuide.InsertEdge(4,7,110);
SchoolGuide.InsertEdge(7,4,110);
SchoolGuide.InsertEdge(7,5,50);
SchoolGuide.InsertEdge(5,7,50);
SchoolGuide.InsertEdge(7,9,120);
SchoolGuide.InsertEdge(9,7,120);
SchoolGuide.InsertEdge(7,10,150);
SchoolGuide.InsertEdge(10,7,150);
SchoolGuide.InsertEdge(7,11,210);
SchoolGuide.InsertEdge(11,7,210);
SchoolGuide.InsertEdge(7,12,270);
SchoolGuide.InsertEdge(12,7,270);
SchoolGuide.InsertEdge(8,10,70);
SchoolGuide.InsertEdge(10,8,70);
SchoolGuide.InsertEdge(8,11,160);
SchoolGuide.InsertEdge(11,8,160);
SchoolGuide.InsertEdge(8,12,190);
SchoolGuide.InsertEdge(12,8,190);
SchoolGuide.InsertEdge(9,11,120);
SchoolGuide.InsertEdge(11,9,120);
SchoolGuide.InsertEdge(9,12,150);
SchoolGuide.InsertEdge(12,9,150);
SchoolGuide.InsertEdge(12,10,120);
SchoolGuide.InsertEdge(10,12,120);
SchoolGuide.InsertEdge(1,10,200);
SchoolGuide.InsertEdge(10,1,200);
SchoolGuide.InsertEdge(12,14,200);
SchoolGuide.InsertEdge(14,12,200);
//加入顶点
SchoolGuide.InsertVertex(0);
SchoolGuide.InsertVertex(1);
SchoolGuide.InsertVertex(2);
SchoolGuide.InsertVertex(3);
SchoolGuide.InsertVertex(4);
SchoolGuide.InsertVertex(5);
SchoolGuide.InsertVertex(6);
SchoolGuide.InsertVertex(7);
SchoolGuide.InsertVertex(8);
SchoolGuide.InsertVertex(9);
SchoolGuide.InsertVertex(10);
SchoolGuide.InsertVertex(11);
SchoolGuide.InsertVertex(12);
SchoolGuide.InsertVertex(13);
SchoolGuide.InsertVertex(14);
SchoolGuide.InsertVertex(15);
SchoolGuide.InsertVertex(16);
SchoolGuide.InsertVertex(17);
SchoolGuide.InsertVertex(18);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSchoolGuiderDlg::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 CSchoolGuiderDlg::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 CSchoolGuiderDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/***************************************************************************************/
/***************************************************************************************/
void CSchoolGuiderDlg::OnMapClick()
{
const int offset = 6;
POINT pos;
GetCursorPos(&pos);
m_mapCtrl.ScreenToClient(&pos);
CClientDC dc(&m_mapCtrl);
if ( dc.GetPixel(pos) != 0x000000)
return;
for (int i=0; i<VERTICES; i++)
{
if (pos.x+offset > points[i].pt.x && pos.x-offset < points[i].pt.x &&
pos.y+offset > points[i].pt.y && pos.y-offset < points[i].pt.y)
{
break;
}
}
if (i == VERTICES)
return;
CString strTemp;
if (!m_bFirstPointSelected)
{
OnClean();
m_bFirstPointSelected = TRUE;
m_iFirstPointIndex = i;
strTemp.Format(" 从 %s 到 ", points[m_iFirstPointIndex].name);
m_showPathStatic.SetWindowText(strTemp);
}
else
{
m_bFirstPointSelected = FALSE;
m_iSecondPointIndex = i;
strTemp.Format(" 从 %s 到 %s", points[m_iFirstPointIndex].name, points[i].name);
m_showPathStatic.SetWindowText(strTemp);
if(m_iSecondPointIndex<=m_iFirstPointIndex)
{
m_iSecondPointIndex=m_iFirstPointIndex;
m_iFirstPointIndex=i;
}
SchoolGuide.AllLengths();
int iDistance = 0;
// 调用计算最短路径子函数
g_iPointIndex = SchoolGuide.BestPath(m_iFirstPointIndex, m_iSecondPointIndex, g_iViaPointNum, iDistance);
strTemp.Format(" %d", iDistance);
m_showLenStatic.SetWindowText(strTemp);
m_mapCtrl.SendMessage(WM_PAINT, 5, NULL);
}
}
void DrawPath(HDC hdc, int *ptIndex, int size, BOOL bNeebDelay)
{
CPen pen(PS_SOLID,6, SELDPATHCOLOR);
HPEN oldPen = (HPEN)SelectObject(hdc, pen.GetSafeHandle());
for (int i=0; i<size-1; i++)
{
::MoveToEx(hdc, points[ptIndex[i]].pt.x, points[ptIndex[i]].pt.y, NULL);
::LineTo( hdc, points[ptIndex[i+1]].pt.x, points[ptIndex[i+1]].pt.y);
/* if (bNeebDelay)
Sleep(300);*/
}
SelectObject(hdc, oldPen);
}
LRESULT CALLBACK MapCtrlWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
HDC hdc = ::GetDC(hwnd);
CallWindowProc(wpOrigMapCtrlProc, hwnd, uMsg, wParam, lParam);
DrawPath(hdc, g_iPointIndex, g_iViaPointNum, wParam);
return 0;
}
case WM_SETCURSOR:
{
POINT pos;
GetCursorPos(&pos);
::ScreenToClient(hwnd, &pos);
HDC hdc = ::GetDC(hwnd);
if (::GetPixel(hdc, pos.x, pos.y) == 0x000000)
SetCursor(g_hSelectCursor);
else
SetCursor(g_hDefalutCursor);
return TRUE;
}
}
return CallWindowProc(wpOrigMapCtrlProc, hwnd, uMsg, wParam, lParam);
}
void CSchoolGuiderDlg::OnCancel()
{
Clean();
CDialog::OnCancel();
}
void CSchoolGuiderDlg::OnClean()
{
Clean();
m_mapCtrl.Invalidate(NULL);
m_mapCtrl.SendMessage(WM_PAINT, NULL, NULL);
m_showPathStatic.SetWindowText(_T(""));
m_showLenStatic.SetWindowText(_T(""));
}
void CSchoolGuiderDlg::Clean()
{
if (g_iPointIndex)
{
delete []g_iPointIndex;
g_iPointIndex = NULL;
}
g_iViaPointNum = 0;
m_bFirstPointSelected = FALSE;
}
void CSchoolGuiderDlg::OnOptimum()
{
OnClean();
g_iPointIndex = SchoolGuide.DFS(g_iViaPointNum);
//g_iPointIndex = SchoolGuide.Prim(g_iViaPointNum);
m_mapCtrl.SendMessage(WM_PAINT, 5, NULL);
}
void CSchoolGuiderDlg::OnHelpAccount()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
dlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -