maintaindlg.cpp

来自「VC做的矢量画图程序!」· C++ 代码 · 共 407 行

CPP
407
字号
// MaintainDlg.cpp : implementation file
//

#include "stdafx.h"
#include "draw.h"
#include "MaintainDlg.h"

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

 void  GetTimeFromStr(CString strTm, CTime &tm);
/////////////////////////////////////////////////////////////////////////////
// CMaintainDLG dialog


CMaintainDLG::CMaintainDLG(CWnd* pParent /*=NULL*/)
	: CDialog(CMaintainDLG::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMaintainDLG)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CMaintainDLG::CMaintainDLG(CString ID,CWnd* pParent /*=NULL*/)
	: CDialog(CMaintainDLG::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMaintainDLG)
		// NOTE: the ClassWizard will add member initialization here
	m_ID.Format("%s",ID);
	m_No=_T("");
	m_Fzr=_T("");
	m_Nr=_T("");
	m_SelNo=_T("");
	//}}AFX_DATA_INIT
}

void CMaintainDLG::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMaintainDLG)
	DDX_Control(pDX, IDC_MAINTAINLIST, m_MaintainInfo);
	DDX_Text(pDX, IDC_MAINTAINNO, m_No);
	DDX_Text(pDX, IDC_FZR, m_Fzr);
	DDX_Text(pDX, IDC_NR, m_Nr);
	DDX_DateTimeCtrl(pDX,IDC_SJ,m_Sj);
	
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMaintainDLG, CDialog)
	//{{AFX_MSG_MAP(CMaintainDLG)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_MAINTAINLIST, OnItemchangedList)
	ON_BN_CLICKED(IDC_MODIFY,OnModify)
	ON_BN_CLICKED(IDC_ADD,OnAdd)
	ON_BN_CLICKED(IDC_DEL,OnDel)
	ON_BN_CLICKED(IDC_REFRESH,OnRefresh)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMaintainDLG message handlers

BOOL CMaintainDLG::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//设置窗口标题
	
	SetWindowText("管线维修记录");
	
    //设置列表头
    m_MaintainInfo.InsertColumn(0, "维修编号", LVCFMT_LEFT, 80);
	m_MaintainInfo.InsertColumn(1, "维修组长", LVCFMT_LEFT, 80);
	m_MaintainInfo.InsertColumn(2, "维修日期", LVCFMT_LEFT, 80);
	m_MaintainInfo.InsertColumn(3, "维修内容", LVCFMT_LEFT, 310);
	
		
	// 设置列表控件扩展样式
	
	DWORD dwStyle;
	dwStyle=::GetWindowLong(m_MaintainInfo.m_hWnd,GWL_STYLE);
	dwStyle |=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
	::SetWindowLong(m_MaintainInfo.m_hWnd,GWL_STYLE,dwStyle);
    dwStyle=m_MaintainInfo.GetExtendedStyle();
	dwStyle |=LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT;
	m_MaintainInfo.SetExtendedStyle(dwStyle);
	m_MaintainInfo.SetBkColor(RGB(0XFF,0XFF,0XE0));
	m_MaintainInfo.SetTextBkColor(RGB(0XFF,0XFF,0XE0));

	ShowData();   //显示列表数据
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
 
}

void CMaintainDLG::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(pNMListView->uNewState&LVIS_SELECTED)
	{
		char pContent[800] ;

		int iCurrent = pNMListView->iItem;
		m_MaintainInfo.GetItemText(iCurrent,0,pContent,50);
		m_No.Format("%s", pContent);
		m_SelNo.Format("%s", pContent);
		m_MaintainInfo.GetItemText(iCurrent,1,pContent,50);
		m_Fzr = CString(pContent);
		m_MaintainInfo.GetItemText(iCurrent,2,pContent,50);
		GetTimeFromStr(pContent,m_Sj);
		m_MaintainInfo.GetItemText(iCurrent,3,pContent,800);
		m_Nr = CString(pContent);
		UpdateData(FALSE);
	}

	
	*pResult = 0;
}

void  GetTimeFromStr(CString strTm, CTime &tm)
{
	int nPos = strTm.Find("-");
	CString strYear = strTm.Left(nPos);
	CString strRight = strTm.Right(strTm.GetLength()-nPos-1);
	nPos = strRight.Find("-");
	CString strMonth = strRight.Left(nPos);
	CString strDay = strRight.Right(strRight.GetLength()-nPos-1);

	int nYear,nMonth,nDay;
	nYear = atoi(strYear.GetBuffer(0));
	if(nYear < 100)
		nYear += 2000;
	nMonth = atoi(strMonth.GetBuffer(0));
	nDay = atoi(strDay.GetBuffer(0));
	tm = CTime(nYear,nMonth,nDay,0,0,0);
}

void CMaintainDLG::OnModify()
{
	if(SelectValid()==false)
	    return;
	
	UpdateData(TRUE);
	if(ModifyValid()==false)
	    return;
	CString sqlStr;
	sqlStr.Format("UPDATE MAINTAIN SET WX_CODE='%s',FZR='%s',COMMENT='%s' ,RQ='%s' WHERE WX_CODE='%s'",m_No,m_Fzr,m_Nr,m_Sj.Format("%y-%m-%d"),m_SelNo);//,FZR='%S',COMMENT='%S'",m_No,m_Fzr,m_Nr);
	
	((CDrawApp*)AfxGetApp())->m_pdatabase.ExecuteSQL(sqlStr);
	ShowData();  //显示列表数据
	//ReSetSel();
	//置表单为添加状态
	m_No="";
	m_Fzr="";
	m_Nr="";
	m_SelNo="";
    m_Sj=CTime::GetCurrentTime();
	UpdateData(FALSE);
}

void CMaintainDLG::OnAdd()
{   

    UpdateData(TRUE);
	//字段验证
    if(AddValid()==false)
		return;      
	CString sqlStr;
	sqlStr.Format("INSERT INTO MAINTAIN (WX_CODE,LINE_ID,FZR,RQ,COMMENT) VALUES ('%s','%s','%s' ,'%s' ,'%s')",m_No,m_ID,m_Fzr,m_Sj.Format("%y-%m-%d"),m_Nr);
	((CDrawApp*)AfxGetApp())->m_pdatabase.ExecuteSQL(sqlStr);
	ShowData();//显示列表数据
//	ReSetSel();
	//置表单为添加状态
	m_No="";
	m_Fzr="";
	m_Nr="";
	m_SelNo="";
    m_Sj=CTime::GetCurrentTime();
	UpdateData(FALSE);
}
void CMaintainDLG::OnDel()
{   
	if(SelectValid()==false)
	    return;
	if(MessageBox("你真的要删除这条记录吗?",
			"民政校园管网管理系统",MB_YESNO)==IDNO)
	   return;
	CString sqlStr;
	sqlStr.Format("DELETE FROM MAINTAIN WHERE WX_CODE='%s'",m_SelNo);
	((CDrawApp*)AfxGetApp())->m_pdatabase.ExecuteSQL(sqlStr);
	ShowData();//显示列表数据
	m_No="";
	m_Fzr="";
	m_Nr="";
	m_SelNo="";
    m_Sj=CTime::GetCurrentTime();
	UpdateData(FALSE);

}
void CMaintainDLG::OnRefresh()
{   m_No="";
	m_Fzr="";
	m_Nr="";
	m_SelNo="";
	m_Sj=CTime::GetCurrentTime();
	UpdateData(FALSE);
	ShowData();
}

bool CMaintainDLG::AddValid()//字段验证
{
	//验证维修编号
    m_No.TrimLeft();
	m_No.TrimRight();
	if(m_No=="")
	{
		AfxMessageBox("维修编号不能为空!");
		GetDlgItem(IDC_MAINTAINNO)->SetFocus();
		return false;
	}
	bool bTag=true;
	for(int i=0;i<m_No.GetLength();i++)
	{
		char ch=m_No.GetAt(i);
		if(ch<'0' || ch>'9')
		{ 
			bTag=false;
			break;
		}
	}
	if(m_No.GetLength()!=8 || !bTag)
	{
		AfxMessageBox("请输入8位数字组成的编号 !");	
		GetDlgItem(IDC_MAINTAINNO)->SetFocus();
        return false;
	}
	CRecordset * pSet=new CRecordset(&((CDrawApp*) AfxGetApp())->m_pdatabase);
    pSet->Open(CRecordset::dynaset,_T("select WX_CODE from Maintain"));
      
	if(!pSet->IsBOF())
	{
		pSet->MoveFirst();
		CString f;
		CString & f1=f;
		while(!pSet->IsEOF())
		{
		
           pSet->GetFieldValue("WX_CODE",f1);
	       if(f.Compare(m_No)==0)
		   {
			  AfxMessageBox("维护编号不能重复!");
			  m_No="";
			  UpdateData(FALSE);
			  GetDlgItem(IDC_MAINTAINNO)->SetFocus();
			  return false;
		   }
		 
		pSet->MoveNext();
	
		}
	}
    //验证负责人
	m_Fzr.TrimLeft();
	m_Fzr.TrimRight();
	if(m_Fzr=="")
	{
		AfxMessageBox("维修组长不能为空!");
		GetDlgItem(IDC_FZR)->SetFocus();
		return false;
	}
	//验证维修内容
	m_Nr.TrimLeft();
	m_Nr.TrimRight();
	if(m_Nr=="")
	{
		AfxMessageBox("维修内容不能为空!");
		GetDlgItem(IDC_NR)->SetFocus();
		return false;
	}
	return true;
}
bool CMaintainDLG::ModifyValid()//字段验证
{
    
	//验证维修编号
    m_No.TrimLeft();
	m_No.TrimRight();
	if(m_No=="")
	{
		AfxMessageBox("维修编号不能为空!");
		GetDlgItem(IDC_MAINTAINNO)->SetFocus();
		return false;
	}
		bool bTag=true;
	for(int i=0;i<m_No.GetLength();i++)
	{
		char ch=m_No.GetAt(i);
		if(ch<'0' || ch>'9')
		{ 
			bTag=false;
			break;
		}
	}
	if(m_No.GetLength()!=8 || !bTag)
	{
		AfxMessageBox("请输入8位数字组成的编号 !");	
		GetDlgItem(IDC_MAINTAINNO)->SetFocus();
        return false;
	}
	//验证负责人
	m_Fzr.TrimLeft();
	m_Fzr.TrimRight();
	if(m_Fzr=="")
	{
		AfxMessageBox("维修组长不能为空!");
		GetDlgItem(IDC_FZR)->SetFocus();
		return false;
	}
	//验证维修内容
	m_Nr.TrimLeft();
	m_Nr.TrimRight();
	if(m_Nr=="")
	{
		AfxMessageBox("维修内容不能为空!");
		GetDlgItem(IDC_NR)->SetFocus();
		return false;
	}
	return true;
}





void CMaintainDLG::ShowData()
{
    m_MaintainInfo.DeleteAllItems();//删除以前列表数据
	
    CRecordset * pSet=new CRecordset(&((CDrawApp*) AfxGetApp())->m_pdatabase);
    CString sqlstr="select * from MAINTAIN  where lINE_ID='"+m_ID+"' ORDER BY WX_CODE";	
    pSet->Open(CRecordset::dynaset,_T(sqlstr));
    //充填列表  
	int i=0;
	if(!pSet->IsBOF())
	{
		pSet->MoveFirst();
		int nItem=0;
		CString f;
		CString & f1=f;
		while(!pSet->IsEOF())
		{
		
        pSet->GetFieldValue("WX_CODE",f1);
		nItem = m_MaintainInfo.InsertItem(i,f);
	
		for(int j=2 ;j<=4;j++){

	    	pSet->GetFieldValue(j,f1);
			if(j==3)
			f=f.Left(10);//截取年月日
		    m_MaintainInfo.SetItemText(nItem, j-1,f); 
		}
		 
		pSet->MoveNext();
		nItem++;
		i++;
		}
    }
	pSet->Close();
	delete  pSet;
	m_SelNo="";
	//重新设置选中项


}

bool CMaintainDLG::SelectValid()
{
 POSITION pos=m_MaintainInfo.GetFirstSelectedItemPosition();
 if(pos==NULL)
 {
	 AfxMessageBox("请先选中一条记录");
	 return false;
 }
 return true;
}

void CMaintainDLG::ReSetSel()//重新显示选项
{

 for(int i=0;i<m_MaintainInfo.GetItemCount();i++)
 
	 if (m_MaintainInfo.GetItemText(i,0).Compare(m_No)==0)
	 {   
		 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)m_MaintainInfo.m_hWnd;
	     pNMListView->iItem=i;
		 
		 break;
	 }
 
}

⌨️ 快捷键说明

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