busroutequerydlg.cpp

来自「一个简单的公交查询管理系统」· C++ 代码 · 共 380 行

CPP
380
字号
// BusRouteQueryDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BusQuery.h"
#include "BusRouteQueryDlg.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;
extern LVITEM Item;
/////////////////////////////////////////////////////////////////////////////
// CBusRouteQueryDlg dialog


CBusRouteQueryDlg::CBusRouteQueryDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBusRouteQueryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBusRouteQueryDlg)
	m_String_Number = _T("");
	//}}AFX_DATA_INIT
}


void CBusRouteQueryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBusRouteQueryDlg)
	DDX_Control(pDX, IDC_ROUTE_QUERY_INFOR, m_ListBox_RouteInfor);
	DDX_Control(pDX, IDC_ROUTE_QUERY_LIST, m_ListCtrl_Route);
	DDX_Text(pDX, IDC_ROUTE_QUERY_NUMBER, m_String_Number);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBusRouteQueryDlg, CDialog)
	//{{AFX_MSG_MAP(CBusRouteQueryDlg)
	ON_BN_CLICKED(IDC_ROUTE_QUERY_QUERY_ENTER, OnRouteQueryQueryEnter)
	ON_NOTIFY(NM_CLICK, IDC_ROUTE_QUERY_LIST, OnClickRouteQueryList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBusRouteQueryDlg message handlers

BOOL CBusRouteQueryDlg::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_list_hanghao = -1;

	// 添加单元格
	HWND h=GetDlgItem(IDC_ROUTE_QUERY_LIST)->m_hWnd;
	DWORD dwExStyle=ListView_GetExtendedListViewStyle(h);
    dwExStyle |= LVS_EX_FULLROWSELECT|LVS_LIST|LVS_NOLABELWRAP;
	ListView_SetExtendedListViewStyle(h,dwExStyle);
	
	m_ListCtrl_Route.SetBkColor(RGB(240,247,233));
	m_ListCtrl_Route.SetTextBkColor(RGB(240,247,233));
	
	// 添加列:
	LV_COLUMN lc;
	lc.mask = LVCF_FMT | LVCF_WIDTH |
		      LVCF_TEXT | LVCF_SUBITEM;
	lc.fmt = LVCFMT_LEFT;
	lc.cx = 32;
	lc.iSubItem = 0;
	lc.pszText = "序号";
	m_ListCtrl_Route.InsertColumn(0, &lc);
	
	lc.cx = 44;
	lc.iSubItem = 1;
	lc.pszText = "路线号";
	m_ListCtrl_Route.InsertColumn(1, &lc);
	
	lc.cx = 60;
	lc.iSubItem = 2;
	lc.pszText = "类型说明";
	m_ListCtrl_Route.InsertColumn(2, &lc);
	
	lc.cx = 32;
	lc.iSubItem = 3;
	lc.pszText = "月票";
	m_ListCtrl_Route.InsertColumn(3, &lc);
	
	lc.cx = 380;
	lc.iSubItem = 4;
	lc.pszText = "详细线路";
	m_ListCtrl_Route.InsertColumn(4, &lc);
	
	lc.cx = 54;
	lc.iSubItem = 5;
	lc.pszText = "备注";
	m_ListCtrl_Route.InsertColumn(5, &lc);

    // 初始化路线列表
    CBusRouteQueryDlg::RouteInforQuery();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


//////////////////////////////////////////////////////////////////////////

void CBusRouteQueryDlg::RouteInforQuery() 
{
	// TODO: Add your message handler code here and/or call default
	//---------------- 查询所有线路信息信息 ----------------//
	//初始化变量	
    BUS_INFOR bus_infor;
	BUS_INFOR *bus_infor_head;
	bus_infor_head = new BUS_INFOR;
    memset(&bus_infor,    0, sizeof(BUS_INFOR));
	memset(bus_infor_head,0, sizeof(BUS_INFOR));
	
	//查询信息
	int rv;
	rv =  DB_BUS_INFOR_Query(hstmt,&bus_infor,bus_infor_head);
	if( rv != 0 )
	{
		//AfxMessageBox("数据库中无信息,请添加信息!");
		delete bus_infor_head;
		return ;
	}	
	
	//--------显示信息 
	m_ListCtrl_Route.DeleteAllItems();
	BUS_INFOR *p;
	CString str;
	char temp[8];
	int j = 0;
	
    while ( bus_infor_head != NULL )
	{
		p = bus_infor_head;		
        //
		j = j+1;	
		itoa(j,temp,10);
		str = temp;
		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_Route.InsertItem(&Item);
		//
		j = j-1;
		//
		itoa(p->Bus_Number,temp,10);
		str = temp;
		m_ListCtrl_Route.SetItemText(j,1,str);
		
		str = p->Bus_Type_Name;
		m_ListCtrl_Route.SetItemText(j,2,str);
		
		if( p->Bus_Ticket == 0 )
		{
			str = "有";
		}
		else
		{
			str = "无";
		}
		m_ListCtrl_Route.SetItemText(j,3,str);
		
		str = p->Bus_Route;
		m_ListCtrl_Route.SetItemText(j,4,str);
		
		
		str = p->Bus_Remark;
		m_ListCtrl_Route.SetItemText(j,5,str);
		
		//
		bus_infor_head = bus_infor_head->next;
		delete p;
		j = j+1;
	}
	
}
//////////////////////////////////////////////////////////////////////////

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

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

void CBusRouteQueryDlg::OnRouteQueryQueryEnter() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	//-----判断信息是否符合条件 
	if( m_String_Number.GetLength() == 0 )
	{
		AfxMessageBox("路线名称不能为空!");
		return;
	}
	
	if( m_String_Number <= "0" )
	{
		AfxMessageBox("请输入大于零的整数!");
		return;
	}
	
    //---------查询信息---------// 
	//---获取查询条件
	int bus_number;
	char temp[8];
	memset(temp, 0, 8);
	strcpy(temp, m_String_Number);
	bus_number =  atoi(temp);
	
	//初始化变量	
    BUS_INFOR bus_infor;
	BUS_INFOR *bus_infor_head;
	bus_infor_head = new BUS_INFOR;
    memset(&bus_infor,    0, sizeof(BUS_INFOR));
	memset(bus_infor_head,0, sizeof(BUS_INFOR));
	//----查询信息
	int rv;
	rv = DB_BUS_INFOR_Query_BusNumber(hstmt, &bus_infor, bus_infor_head,bus_number);
	if( rv != 0 )
	{
		AfxMessageBox("无此线路公交车,请确认!");
		return;
	}

	//
	CBusRouteQueryDlg::ShowRouteInfor(bus_infor_head);
	
}
//////////////////////////////////////////////////////////////////////////

void CBusRouteQueryDlg::OnClickRouteQueryList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	POINT pt;
	::GetCursorPos(&pt);
	ScreenToClient(&pt);
	*pResult = 0;
	
	//得到选定行号
	m_list_hanghao = m_ListCtrl_Route . GetNextItem ( -1, LVNI_SELECTED );
	
	//判断是否选中
	if( m_list_hanghao < 0 )
	{
		return;
	}

	//初始化变量	
    BUS_INFOR *bus_infor;
	bus_infor = new BUS_INFOR;
    memset(bus_infor,    0, sizeof(BUS_INFOR));

	char temp[8];
	memset(temp, 0, 8);
    //显示查询信息
	strcpy(temp, m_ListCtrl_Route.GetItemText(m_list_hanghao,1));
	bus_infor->Bus_Number = atoi(temp);

    strcpy(bus_infor->Bus_Type_Name, m_ListCtrl_Route.GetItemText(m_list_hanghao,2));
    strcpy(bus_infor->Bus_Route,     m_ListCtrl_Route.GetItemText(m_list_hanghao,4));
	strcpy(bus_infor->Bus_Remark,    m_ListCtrl_Route.GetItemText(m_list_hanghao,5));
	
	CString str;
	str = m_ListCtrl_Route.GetItemText(m_list_hanghao, 3);
	if( str == "有" )
	{
		bus_infor->Bus_Ticket = 0;
	}
	else
	{
		bus_infor->Bus_Ticket = -1;
	}

	bus_infor->next = NULL;
	//
    CBusRouteQueryDlg::ShowRouteInfor(bus_infor);
	//
	UpdateData(FALSE);
}

//////////////////////////////////////////////////////////////////////////
void CBusRouteQueryDlg::ShowRouteInfor(BUS_INFOR *bus_infor_head)
{
	BUS_INFOR *p;
	CString str,strR;
	char temp[8];
	char tempRoute[84];
	int nSign,routeLen;
	
	m_ListBox_RouteInfor.ResetContent();
    while ( bus_infor_head != NULL )
	{
		p = bus_infor_head;		
		//
		itoa(p->Bus_Number, temp, 10);
        str = temp;
		str = "-------------------------------------------  "+str+"路"+"  -------------------------------------------------";
		m_ListBox_RouteInfor.AddString(str);
        
		str = temp;
		str = str+"路:";
		strR = p->Bus_Type_Name;
		str = str+strR;
        if( p->Bus_Ticket == 0 )
        {
			strR = ",有月票";
        }
		else
		{
			strR = ",无月票";
		}
        str = str+strR;
		strR = p->Bus_Remark;
		str = str+","+strR;
		m_ListBox_RouteInfor.AddString(str);

        nSign = 0;
        routeLen = strlen(p->Bus_Route);
		while( (routeLen-nSign) > 0 )
		{
			
			
			memset(tempRoute, 0 ,84);
			if( (routeLen-84) >= 0)
			{
				strncpy(tempRoute, &p->Bus_Route[nSign], 84);
				tempRoute[84] = '\0';
			}
			else
			{			
                strncpy(tempRoute, &p->Bus_Route[nSign], routeLen-nSign);
				tempRoute[84] = '\0';
			}
            //
			str = tempRoute;
			str = "      "+str;
            m_ListBox_RouteInfor.AddString(str);
			//
			nSign = nSign+84;
		}
		//
		bus_infor_head = bus_infor_head->next; 
		str = " ";
		m_ListBox_RouteInfor.AddString(str);
		delete p;
	}

}

⌨️ 快捷键说明

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