📄 busdlg.cpp
字号:
// BusDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NameGis.h"
#include "BusDlg.h"
#include "Mainfrm.h"
#include "AppApi.h"
#include "Crack.h"
#include "NrstPath.h"
#include "BusResultDlg.h"
// CBusDlg dialog
IMPLEMENT_DYNAMIC(CBusDlg, CDialog)
CBusDlg::CBusDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBusDlg::IDD, pParent)
{
}
CBusDlg::~CBusDlg()
{
}
void CBusDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUS_STARTLIST, m_StartList);
DDX_Control(pDX, IDC_BUS_ENDLIST, m_EndList);
}
BEGIN_MESSAGE_MAP(CBusDlg, CDialog)
ON_LBN_SELCHANGE(IDC_BUS_STARTLIST, OnLbnSelchangeBusStartlist)
ON_LBN_SELCHANGE(IDC_BUS_ENDLIST, OnLbnSelchangeBusEndlist)
ON_EN_CHANGE(IDC_BUS_STARTEDT, OnEnChangeBusStartedt)
ON_EN_CHANGE(IDC_BUS_ENDEDT, OnEnChangeBusEndedt)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
// CBusDlg message handlers
//-----------------------------------------------------------------------------------------
BOOL CBusDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 在对话框的2个列表框中加入站点名称
LoadListBox();
return TRUE;
}
//-----------------------------------------------------------------------------------------
void CBusDlg::LoadListBox()
{
CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
CDaoDatabase* tmpDB = new CDaoDatabase;
try
{
tmpDB->Open(pMainWnd->m_environment.m_szDBName);
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return ;
}
m_StartList.ResetContent();
m_EndList.ResetContent();
CDaoRecordset rs(tmpDB);
try
{
CString szSQL = "Select distinct 站名 From 公交车站路线 Order By 站名";
rs.Open(dbOpenDynaset, szSQL);
while(!rs.IsEOF())
{
COleVariant var;
var = rs.GetFieldValue("站名");
CString str = CCrack::strVARIANT(var);
m_StartList.AddString(str);
rs.MoveNext();
}
rs.Close();
szSQL = "Select * From 公交车站";
rs.Open(dbOpenDynaset, szSQL);
while(!rs.IsEOF())
{
COleVariant var;
var = rs.GetFieldValue("站名");
CString str = CCrack::strVARIANT(var);
m_EndList.AddString(str);
rs.MoveNext();
}
rs.Close();
}
catch (CDaoException* e)
{
DisplayDaoException(e);
tmpDB->Close();
delete tmpDB;
e->Delete();
return ;
}
if(tmpDB)
{
if(tmpDB->IsOpen())
{
tmpDB->Close();
}
delete tmpDB;
tmpDB = NULL;
}
}
//-----------------------------------------------------------------------------------------
void CBusDlg::OnLbnSelchangeBusStartlist()
{
CString strName;
m_StartList.GetText(m_StartList.GetCurSel(), strName);
GetDlgItem(IDC_BUS_STARTEDT)->SetWindowText(strName);
}
//-----------------------------------------------------------------------------------------
void CBusDlg::OnLbnSelchangeBusEndlist()
{
CString strName;
m_EndList.GetText(m_EndList.GetCurSel(), strName);
GetDlgItem(IDC_BUS_ENDEDT)->SetWindowText(strName);
}
//-----------------------------------------------------------------------------------------
void CBusDlg::OnEnChangeBusStartedt()
{
CString strName;
GetDlgItem(IDC_BUS_STARTEDT)->GetWindowText(strName);
int nIndex = m_StartList.FindString(0, strName);
if(nIndex != LB_ERR)
m_StartList.SetCurSel(nIndex);
}
//-----------------------------------------------------------------------------------------
void CBusDlg::OnEnChangeBusEndedt()
{
CString strName;
GetDlgItem(IDC_BUS_ENDEDT)->GetWindowText(strName);
int nIndex = m_EndList.FindString(0, strName);
if(nIndex != LB_ERR)
m_EndList.SetCurSel(nIndex);
}
//-----------------------------------------------------------------------------------------
void CBusDlg::OnBnClickedOk()
{
// 判断站名的正确性
CString strStartName;
GetDlgItem(IDC_BUS_STARTEDT)->GetWindowText(strStartName);
CString strEndName;
GetDlgItem(IDC_BUS_ENDEDT)->GetWindowText(strEndName);
if(strStartName == "" || strEndName == "")
return;
if (!IsValidStation(strStartName))
{
AfxMessageBox("错误的起始站名。");
return;
}
if (!IsValidStation(strEndName))
{
AfxMessageBox("错误的终点站名。");
return;
}
CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
CEnvironment* env = &(pMainWnd->m_environment);
CWaitCursor WaitCursor;
CList<PathNode, PathNode&> array;
env->m_path.Search(strStartName, strEndName, &array);
if (array.GetCount() == 0)
{
AfxMessageBox("没有合适的乘车路线。");
return;
}
else
{
ModifyStyle(WS_VISIBLE, 0);// 隐藏对话框
CBusResultDlg* pBusResultDlg = new CBusResultDlg(pMainWnd);
pBusResultDlg->m_array = &array;
pBusResultDlg->DoModal();
delete pBusResultDlg;
pBusResultDlg = NULL;
}
int nCount = array.GetCount();
for(int i=0; i<nCount; i++)
{
PathNode node = array.GetTail();
array.RemoveTail();
delete []node.szFromStationName;
delete []node.szRoutineName;
delete []node.szToStationName;
}
OnOK(); //关闭对话框
}
//-----------------------------------------------------------------------------------------
// 判断用于输入的站点名是否正确
BOOL CBusDlg::IsValidStation(CString szStation)
{
CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
CDaoDatabase* tmpDB = new CDaoDatabase;
try
{
tmpDB->Open(pMainWnd->m_environment.m_szDBName);
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return FALSE;
}
BOOL bResult = FALSE;
CDaoRecordset rs(tmpDB);
try
{
CString szSQL = "Select distinct 站名 From 公交车站路线 Order By 站名";
rs.Open(dbOpenDynaset, szSQL);
if(rs.GetRecordCount() > 0)
bResult = TRUE;
rs.Close();
}
catch (CDaoException* e)
{
DisplayDaoException(e);
tmpDB->Close();
delete tmpDB;
e->Delete();
return FALSE;
}
if(tmpDB)
{
if(tmpDB->IsOpen())
{
tmpDB->Close();
}
delete tmpDB;
tmpDB = NULL;
}
return bResult;
}
//-----------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -