📄 driversetview.cpp
字号:
// DriversetView.cpp : implementation file
//
#include "stdafx.h"
#include "StationManage.h"
#include "DriversetView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDriversetView
IMPLEMENT_DYNCREATE(CDriversetView, CRecordView)
CDriversetView::CDriversetView()
: CRecordView(CDriversetView::IDD)
{
//{{AFX_DATA_INIT(CDriversetView)
m_pSet = NULL;
m_driverid = 0;
m_drivername = _T("");
//}}AFX_DATA_INIT
}
CDriversetView::~CDriversetView()
{
if (!((CStationManageApp*)AfxGetApp())->m_driversettingcontrol)
{
((CStationManageApp*)AfxGetApp())->m_driversetting=0;
}
if (m_pSet)
delete m_pSet;
}
void CDriversetView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDriversetView)
DDX_Control(pDX, IDC_LIST, m_list);
DDX_Text(pDX, IDC_DRIVERID, m_driverid);
DDX_Text(pDX, IDC_DRIVERNAME, m_drivername);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDriversetView, CRecordView)
//{{AFX_MSG_MAP(CDriversetView)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_CHANGE, OnChange)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_RETURN, OnReturn)
ON_BN_CLICKED(IDC_REFURBISH, OnRefurbish)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDriversetView diagnostics
#ifdef _DEBUG
void CDriversetView::AssertValid() const
{
CRecordView::AssertValid();
}
void CDriversetView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDriversetView message handlers
CRecordset* CDriversetView::OnGetRecordset()
{
if (m_pSet != NULL)
return m_pSet;
m_pSet = new CDriverSet(NULL);
m_pSet->Open();
return m_pSet;
}
CDriverSet* CDriversetView::GetRecordset()
{
CDriverSet* pData = (CDriverSet*) OnGetRecordset();
ASSERT(pData == NULL || pData->IsKindOf(RUNTIME_CLASS(CDriverSet)));
return pData;
}
void CDriversetView::OnInitialUpdate()
{
BeginWaitCursor();
GetRecordset();
CRecordView::OnInitialUpdate();
GetDocument()->SetTitle("司机管理");
GetParent()->SetWindowText("司机管理");
EndWaitCursor();
RECT rectlist;
m_list.GetWindowRect(&rectlist);
int listwidth=rectlist.right-rectlist.left;
DWORD dwExtendedStyle=LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE;
m_list.SetExtendedStyle(dwExtendedStyle|LVS_EX_FULLROWSELECT);
m_list.InsertColumn(0,"司机ID",LVCFMT_LEFT,listwidth/2-10);
m_list.InsertColumn(1,"司机姓名",LVCFMT_LEFT,listwidth/2-10);
Showlist();
curiItem=0;
Getvalue();
m_list.SetItemState(curiItem,LVIS_SELECTED,LVIS_SELECTED);
EnableView(true);
UpdateData(FALSE);
}
void CDriversetView::Getvalue()
{
m_driverid = atoi(m_list.GetItemText(curiItem,0));
m_drivername = m_list.GetItemText(curiItem,1);
UpdateData(FALSE);
}
void CDriversetView::Showlist()
{
if (!m_pSet->IsOpen())
m_pSet->Open();
m_list.DeleteAllItems();
m_pSet->MoveFirst();
if (!m_pSet->IsEOF())
{
while(!m_pSet->IsEOF())
m_pSet->MoveNext();
int cout=m_pSet->GetRecordCount();
m_pSet->MoveFirst();
while(!m_pSet->IsEOF())
{
CString strValue;
int nCount=m_list.GetItemCount();
m_list.InsertItem(nCount,strValue);
strValue.Format("%d",m_pSet->m_driverid);
m_list.SetItemText(nCount,0,strValue);
m_pSet->m_drivername.TrimRight();
m_list.SetItemText(nCount,1,m_pSet->m_drivername);
m_pSet->MoveNext();
}
}
}
void CDriversetView::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
curiItem = pNMListView->iItem;
if(curiItem != -1)
{
Getvalue();
EnableView(true);
}
UpdateData(FALSE);
*pResult = 0;
}
void CDriversetView::EnableView(bool b)
{
GetDlgItem(IDC_ADD)->EnableWindow(b);
GetDlgItem(IDC_CHANGE)->EnableWindow(b);
GetDlgItem(IDC_DELETE)->EnableWindow(b);
GetDlgItem(IDC_SAVE)->EnableWindow(!b);
GetDlgItem(IDC_RETURN)->EnableWindow(!b);
GetDlgItem(IDC_DRIVERID)->EnableWindow(!b);
GetDlgItem(IDC_DRIVERNAME)->EnableWindow(!b);
UpdateData(FALSE);
}
void CDriversetView::OnAdd()
{
// TODO: Add your control notification handler code here
EnableView(false);
m_driverid=0;
m_drivername.Empty();
UpdateData(FALSE);
operation = 1;
curiItem = m_list.GetItemCount();
}
void CDriversetView::OnChange()
{
// TODO: Add your control notification handler code here
if (curiItem==m_list.GetItemCount())
{
MessageBox("请选择您要修改的行!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
EnableView(false);
Getvalue();
UpdateData(FALSE);
operation = 2;
}
void CDriversetView::OnDelete()
{
// TODO: Add your control notification handler code here
if (curiItem==m_list.GetItemCount())
{
MessageBox("请选择您要删除的行!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
CString info;
info="确认要删除司机“"+m_drivername+"”?";
if (IDOK==MessageBox(info,"提示",MB_OKCANCEL|MB_ICONQUESTION))
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->MoveFirst();
m_pSet->Move(curiItem);
m_pSet->Delete();
m_pSet->Requery();
m_list.DeleteItem(curiItem);
info.Empty();
info="司机“"+m_drivername+"”已经删除!";
curiItem=0;
Getvalue();
MessageBox(info,"删除成功",MB_OK|MB_ICONINFORMATION);
UpdateData(FALSE);
}
}
void CDriversetView::OnSave()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_driverid==0||m_drivername=="")
{
AfxMessageBox("司机ID不能为0或司机姓名不能为空");
return;
}
if (operation == 1)
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->AddNew(); //在表的末尾添加新记录
m_pSet->m_driverid = m_driverid;
m_pSet->m_drivername = m_drivername;
m_pSet->Update();
m_pSet->Requery();
Showlist();
MessageBox("增加成功!","增加成功",MB_OK|MB_ICONINFORMATION);
EnableView(true);
}
else
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->MoveFirst();
m_pSet->Move(curiItem);
m_pSet->Edit();
m_pSet->m_driverid = m_driverid;
m_pSet->m_drivername = m_drivername;
m_pSet->Update();
m_pSet->Requery();
Showlist();
MessageBox("修改成功!","修改成功",MB_OK|MB_ICONINFORMATION);
EnableView(true);
}
}
void CDriversetView::OnReturn()
{
// TODO: Add your control notification handler code here
EnableView(true);
}
void CDriversetView::OnRefurbish()
{
// TODO: Add your control notification handler code here
Showlist();
EnableView(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -