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

📄 busstationupdatedlg.cpp

📁 一个简单的公交查询管理系统
💻 CPP
字号:
// BusStationUpdateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BusQuery.h"
#include "BusStationUpdateDlg.h"
#include "DB_Func.h"

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


extern SQLHENV henv;
extern SQLHDBC hdbc;
extern SQLHSTMT hstmt;
LVITEM Item;
/////////////////////////////////////////////////////////////////////////////
// CBusStationUpdateDlg dialog


CBusStationUpdateDlg::CBusStationUpdateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBusStationUpdateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBusStationUpdateDlg)
	m_Cstring_Name = _T("");
	m_Cstring_Type_Name = _T("");
	//}}AFX_DATA_INIT
}


void CBusStationUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBusStationUpdateDlg)
	DDX_Control(pDX, IDC_STATION_UPDATE_LIST, m_ListCtrl_Station);
	DDX_Control(pDX, IDC_STATION_UPDATE_COMBO, m_ComboBox_Type);
	DDX_Text(pDX, IDC_STATION_UPDATE_NAME, m_Cstring_Name);
	DDX_CBString(pDX, IDC_STATION_UPDATE_COMBO, m_Cstring_Type_Name);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBusStationUpdateDlg, CDialog)
	//{{AFX_MSG_MAP(CBusStationUpdateDlg)
	ON_BN_CLICKED(IDC_STATION_UPDATE_CLEAR, OnStationUpdateClear)
	ON_BN_CLICKED(IDC_STATION_UPDATE_ADD, OnStationUpdateAdd)
	ON_BN_CLICKED(IDC_STATION_UPDATE_DELETE, OnStationUpdateDelete)
	ON_BN_CLICKED(IDC_STATION_UPDATE_DELETE_ALL, OnStationUpdateDeleteAll)
	ON_BN_CLICKED(IDC_STATION_UPDATE_MODIFY, OnStationUpdateModify)
	ON_NOTIFY(NM_CLICK, IDC_STATION_UPDATE_LIST, OnClickStationUpdateList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBusStationUpdateDlg message handlers

BOOL CBusStationUpdateDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CRect rc(0, 0, 0, 0);
	
	GetParent()->GetClientRect(&rc);
	((CTabCtrl*)GetParent())->AdjustRect(false, &rc); 
	MoveWindow(&rc);

	//////////////////////////////////////////////////////////////////////////
	// 初始化选中行
	m_hanghao = -1;
	
	// 添加单元格
	HWND h=GetDlgItem(IDC_STATION_UPDATE_LIST)->m_hWnd;
	DWORD dwExStyle=ListView_GetExtendedListViewStyle(h);
    dwExStyle |= LVS_EX_FULLROWSELECT|LVS_LIST|LVS_NOLABELWRAP;
	ListView_SetExtendedListViewStyle(h,dwExStyle);
	
	m_ListCtrl_Station.SetBkColor(RGB(240,247,233));
	m_ListCtrl_Station.SetTextBkColor(RGB(240,247,233));
	
	// 添加列:
	LV_COLUMN lc;
	lc.mask = LVCF_FMT | LVCF_WIDTH |
		      LVCF_TEXT | LVCF_SUBITEM;
	lc.fmt = LVCFMT_LEFT;
	lc.cx = 37;
	lc.iSubItem = 0;
	lc.pszText = "序号";
	m_ListCtrl_Station.InsertColumn(0, &lc);
	
	lc.cx = 113;
	lc.iSubItem = 1;
	lc.pszText = "站点名称";
	m_ListCtrl_Station.InsertColumn(1, &lc);

	lc.cx = 108;
	lc.iSubItem = 2;
	lc.pszText = "站点类型";
	m_ListCtrl_Station.InsertColumn(2, &lc);
	//////////////////////////////////////////////////////////////////////////

	CBusStationUpdateDlg::StationInforQuery();
	CBusStationUpdateDlg::StationTypeQuery();

//	m_Cstring_Type_Name = "";
	///////////////////////////////////////////////////////////////
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CBusStationUpdateDlg::OnCancel() 
{
	// TODO: Add your message handler code here and/or call default
	return;
}

void CBusStationUpdateDlg::OnOK() 
{
	// TODO: Add your message handler code here and/or call default
	return;
}

void CBusStationUpdateDlg::OnStationUpdateClear() 
{
	// TODO: Add your control notification handler code here
	m_Cstring_Name = _T("");
	m_Cstring_Type_Name = _T("");
	UpdateData(FALSE);
}

void CBusStationUpdateDlg::OnStationUpdateAdd() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	//-----判断输入信息是否符合条件 
	if( m_Cstring_Name.GetLength() == 0 )
	{
		AfxMessageBox("站点名称不能为空!");
		return;
	}
	
	if( m_Cstring_Name.GetLength() >= 24 )
	{
		AfxMessageBox("站点名称长度超出限度,请重输!");
		return;
	}

	int num = m_ComboBox_Type.GetCurSel();
	if( num < 0 )
	{
		AfxMessageBox("请先选定站点类型!");
	}	

	//----添加信息 
	//赋值
	STATION_INFOR station_infor;
	memset(&station_infor, 0, sizeof(STATION_INFOR));
	strcpy(station_infor.Station_Name,      m_Cstring_Name);
	strcpy(station_infor.Station_Type_Name, m_Cstring_Type_Name);
	memset(station_infor.Bus_Number, '0', 128);
	//添加信息
	int rv;
	rv = DB_STATION_INFOR_Insert(hstmt, station_infor);
	if( rv != 0 )
	{
		AfxMessageBox("已存在此站点,请确认!");
		return;
	}
	else
	{
		AfxMessageBox("成功保存添加信息!");
	}//end if
	
	
	//重新初始化窗口信息
	m_hanghao = -1;
	m_Cstring_Name = _T("");
//	m_Cstring_Type_Name = _T("");
	CBusStationUpdateDlg::StationInforQuery();
	UpdateData(FALSE);
}

void CBusStationUpdateDlg::OnStationUpdateDelete() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if( m_hanghao < 0 )
	{
		AfxMessageBox("请先选定要删除信息!");
		return;
	}
    ///////
	if( MessageBox("删除此单位信息,将丢失关于此单位的信息,是否删除?","删除确认",MB_YESNO|MB_ICONQUESTION) != IDYES )
	{
		return;
	}

	// 获取删除条件
    char station_name[24];
	memset(station_name, 0, 24);
	strcpy(station_name, m_ListCtrl_Station.GetItemText(m_hanghao,1));

	// 删除信息
	int rv;
	rv = DB_STATION_INFOR_Delete(hstmt, station_name);
    if( rv != 0 )
	{
		AfxMessageBox("删除信息失败!");
		return;
	}
	else
	{
		AfxMessageBox("删除信息成功!");
	}//end if
	
	//重新初始化窗口信息
	m_hanghao = -1;
	m_Cstring_Name = _T("");
	m_Cstring_Type_Name = _T("");
	CBusStationUpdateDlg::StationInforQuery();
	UpdateData(FALSE);
}

void CBusStationUpdateDlg::OnStationUpdateDeleteAll() 
{
	// TODO: Add your control notification handler code here
	if( MessageBox("是否真的删除全部站点信息,这样将会丢失全部站点信息","删除确认",MB_YESNO|MB_ICONQUESTION) != IDYES )
	{
		return;
	}
	
	//---- 删除全部车次类型 
	// 删除信息
	int rv;
	rv = DB_STATION_INFOR_Delete_All(hstmt);
	if( rv != 0)
	{
		AfxMessageBox("无法删除信息,请“刷新”界面,确认数据是否存在数据!");
		return;
	}
	else
	{
		AfxMessageBox("删除信息成功!");
	}

	//重新初始化窗口信息
	m_hanghao = -1;
	m_Cstring_Name = _T("");
	m_Cstring_Type_Name = _T("");
    m_ListCtrl_Station.DeleteAllItems();
	UpdateData(FALSE);
}

void CBusStationUpdateDlg::OnStationUpdateModify() 
{
	// TODO: Add your control notification handler code here
	if( m_hanghao < 0 )
	{
		AfxMessageBox("请先选定要修改的信息,增加信息保存请点击“增加”!");
		return;
	}

	UpdateData(TRUE);
	//-----判断输入信息是否符合条件 
	if( m_Cstring_Name.GetLength() == 0 )
	{
		AfxMessageBox("站点名称不能为空!");
		return;
	}
	
	if( m_Cstring_Name.GetLength() >= 24 )
	{
		AfxMessageBox("站点名称长度超出限度,请重输!");
		return;
	}
	
	int num = m_ComboBox_Type.GetCurSel();
	if( num < 0 )
	{
		AfxMessageBox("请先选定站点类型!");
	}	
	
	//----添加信息 
	// 得到查询条件
	char station_name[24];
	memset(station_name, 0, 24);
	strcpy(station_name, m_ListCtrl_Station.GetItemText(m_hanghao,1));

	// 赋值
	STATION_INFOR station_infor;
	memset(&station_infor, 0, sizeof(STATION_INFOR));
	strcpy(station_infor.Station_Name,      m_Cstring_Name);
	strcpy(station_infor.Station_Type_Name, m_Cstring_Type_Name);
	memset(station_infor.Bus_Number, '0', 128);
	// 添加信息
	int rv;
	rv = DB_STATION_INFOR_Modify(hstmt, &station_infor, station_name);
	if( rv != 0 )
	{
		AfxMessageBox("已存在此站点,请确认!");
		return;
	}
	else
	{
		AfxMessageBox("成功保存修改信息!");
	}//end if
	
	
	//重新初始化窗口信息
	m_hanghao = -1;
	m_Cstring_Name = _T("");
	m_Cstring_Type_Name = _T("");
	CBusStationUpdateDlg::StationInforQuery();
	UpdateData(FALSE);
}

void CBusStationUpdateDlg::OnClickStationUpdateList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	POINT pt;
	::GetCursorPos(&pt);
	ScreenToClient(&pt);
	*pResult = 0;
	
	//得到选定行号
	m_hanghao = m_ListCtrl_Station . GetNextItem ( -1, LVNI_SELECTED );
	
	//判断是否选中
	if( m_hanghao < 0 )
	{
		return;
	}
	
    //显示查询信息	
	m_Cstring_Name      = m_ListCtrl_Station.GetItemText(m_hanghao,1);
	m_Cstring_Type_Name = m_ListCtrl_Station.GetItemText(m_hanghao,2);
	UpdateData(FALSE);
}


void CBusStationUpdateDlg::StationTypeQuery()
{
	//---------------- 查询站点类型信息 ----------------//
	// 初始化变量
	STATION_TYPE staiton_type;
	STATION_TYPE *staiton_type_head;
	staiton_type_head = new STATION_TYPE;
	memset(&staiton_type,     0, sizeof(STATION_TYPE));
	memset(staiton_type_head, 0, sizeof(STATION_TYPE));
	
	// 查询
	int rv;
	rv = DB_STATION_TYPE_Query(hstmt, &staiton_type, staiton_type_head);   
	if( rv != 0 )
	{
		delete staiton_type_head;
		return ;
	}
	// 列表显示
	m_ComboBox_Type.ResetContent();
	
    STATION_TYPE *pTemp_staiton;
	
	while( staiton_type_head != NULL )
    {
		pTemp_staiton = staiton_type_head;
		m_ComboBox_Type.AddString(pTemp_staiton->Station_Type_Name);
		staiton_type_head = staiton_type_head->next;
		delete pTemp_staiton;		
    }

}

void CBusStationUpdateDlg::StationInforQuery()
{
	//---------------- 查询站点信息 ----------------//
	//初始化变量	
    STATION_INFOR station_infor;
	STATION_INFOR *station_infor_head;
	station_infor_head = new STATION_INFOR;
    memset(&station_infor,    0, sizeof(STATION_INFOR));
	memset(station_infor_head,0, sizeof(STATION_INFOR));
	
	//查询信息
	int rv;
	rv =  DB_STATION_INFOR_Query(hstmt,&station_infor,station_infor_head);
	if( rv != 0 )
	{
		//AfxMessageBox("数据库中无信息,请添加信息!");
		delete station_infor_head;
		return ;
	}	
	
	//--------显示信息 
	m_ListCtrl_Station.DeleteAllItems();
	STATION_INFOR *p;
	CString str;
	char s[8];
	int j = 0;
	
    while ( station_infor_head != NULL )
	{
		p = station_infor_head;		
        //
		j = j+1;
		
		itoa(j,s,10);
		str = s;
		if( j < 10 )
		{
			str = "00"+str;		
		}
		if( (10 <= j) && (j < 100) )
		{
			str = "0"+str;		
		}
		
        Item.mask=LVIF_IMAGE|LVIF_TEXT;
		Item.pszText=str.GetBuffer(str.GetLength());
		m_ListCtrl_Station.InsertItem(&Item);
		//
		j = j-1;
		str = p->Station_Name;
		m_ListCtrl_Station.SetItemText(j,1,str);
		
		str = p->Station_Type_Name;
		m_ListCtrl_Station.SetItemText(j,2,str);
		
		
		station_infor_head = station_infor_head->next;
		delete p;
		j = j+1;
	}

}

⌨️ 快捷键说明

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