www.pudn.com > PBMS.rar > ReturnDlg.cpp


// ReturnDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "PBMS.h" 
#include "ReturnDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CReturnDlg dialog 
 
 
CReturnDlg::CReturnDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CReturnDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CReturnDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void CReturnDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CReturnDlg) 
	DDX_Control(pDX, IDC_BUTTON_DEL, m_btnDel); 
	DDX_Control(pDX, IDC_LIST_RETLIST, m_listReturn); 
	//}}AFX_DATA_MAP 
} 
 
IMPLEMENT_DYNCREATE(CReturnDlg, CDialog) 
BEGIN_MESSAGE_MAP(CReturnDlg, CDialog) 
	//{{AFX_MSG_MAP(CReturnDlg) 
	ON_WM_PAINT() 
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CReturnDlg message handlers 
 
BOOL CReturnDlg::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	//重新排列控件位置 
	AlignControls(); 
	//*==============修改List控件风格===================*// 
	DWORD dwStyle = m_listReturn.GetStyle(); 
	dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ; 
	m_listReturn.SetExtendedStyle(dwStyle); 
	//*=================================================*// 
	//设置按钮风格 
	m_btnDel.SetFlat(false); 
	//设置list控件列名 
	SetColumnName(); 
	//初始化list控件数据 
	RefreshColumnData();	 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
/*=========================PreTranslateMessage======================================== 
说明: 
	重载PreTranslateMessage函数是为了确保用户回车时不关闭对话框, 
	而使其焦点指向下一个控件 
====================================================================================*/ 
BOOL CReturnDlg::PreTranslateMessage(MSG* pMsg)  
{ 
 	BOOL handle = FALSE; 
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) 
 	{ 
		CWnd *pCurrent = NULL; 
		pCurrent = GetFocus();//指向当前获得焦点的子窗口 
		if (pCurrent) 
		{ 
			CWnd *pNext = NULL; 
			pNext = GetNextDlgTabItem(pCurrent); 
			pNext->SetFocus();//焦点指向下一个子窗口 
			handle = TRUE; 
		} 
	} 
	return (handle ? handle : CDialog::PreTranslateMessage(pMsg)); 
} 
 
/*=========================SizeAllColumn============================================== 
说明: 
							调整列的宽度 
====================================================================================*/ 
void CReturnDlg::SizeAllColumn() 
{ 
	CHeaderCtrl *pHeader = m_listReturn.GetHeaderCtrl();//获取标题控件 
	ASSERT(pHeader); 
	if (pHeader) 
	{ 
		m_listReturn.SetRedraw(false);//关闭功能重画,直到全部列大小都设置好以后再整体重画,解决闪烁问题 
		for (int i = 0 ;i < pHeader->GetItemCount(); i++) 
		{ 
			m_listReturn.SetColumnWidth(i,LVSCW_AUTOSIZE);//首先按列中值最长的宽度设置 
			int temp1 = m_listReturn.GetColumnWidth(i); 
			m_listReturn.SetColumnWidth(i,LVSCW_AUTOSIZE_USEHEADER);//再按标题宽度设置 
			int temp2 = m_listReturn.GetColumnWidth(i); 
			m_listReturn.SetColumnWidth(i,max(temp1,temp2)); 
		}//end-for 
		m_listReturn.SetRedraw();//开启重画功能,让系统重画已经设置好的列表控件 
		m_listReturn.Invalidate();//发送WM_PAINT消息 
	}//end-if 
} 
 
/*=========================SetColumnName============================================== 
说明: 
	为List控件设置列名 
====================================================================================*/ 
void CReturnDlg::SetColumnName() 
{ 
	int index = 0; 
	//设置列名 
	m_listReturn.InsertColumn(index++,"序号",LVCFMT_LEFT);//序号不是ID 
	m_listReturn.InsertColumn(index++,"书名",LVCFMT_LEFT); 
	m_listReturn.InsertColumn(index++,"索书号",LVCFMT_LEFT); 
	m_listReturn.InsertColumn(index++,"文学书",LVCFMT_LEFT); 
	m_listReturn.InsertColumn(index++,"借阅日期",LVCFMT_LEFT); 
	m_listReturn.InsertColumn(index++,"还书日期",LVCFMT_LEFT); 
	m_listReturn.InsertColumn(index++,"图书馆",LVCFMT_LEFT); 
} 
 
/*=========================RefreshColumnData========================================== 
说明: 
	刷新列表控件中的数据 
====================================================================================*/ 
void CReturnDlg::RefreshColumnData() 
{ 
	CString szItem; 
	//首先清空所有数据 
	m_listReturn.DeleteAllItems(); 
	//将用户所借书籍列表中数据在List控件中显示 
	for (int index = 0; index < m_userBooks.GetSize(); index ++) 
	{ 
		//ID 
		szItem.Format("%d",index + 1); 
		m_listReturn.InsertItem(index,szItem); 
		//书名 
		m_listReturn.SetItemText(index,1,m_userBooks.GetAt(index).name ); 
		//书号 
		m_listReturn.SetItemText(index,2,m_userBooks.GetAt(index).bookNumber ); 
		//文学书 
		if (m_userBooks.GetAt(index).IsLiterature) 
		{ 
			szItem = _T("是"); 
		} 
		else 
		{ 
			szItem = _T("否"); 
		} 
		m_listReturn.SetItemText(index,3,szItem); 
		//借书时间 
		szItem = m_userBooks.GetAt(index).borrowTime.Format("%Y-%m-%d"); 
		m_listReturn.SetItemText(index,4,szItem); 
		//还书时间 
		szItem = m_userBooks.GetAt(index).returnTime.Format("%Y-%m-%d"); 
		m_listReturn.SetItemText(index,5,szItem); 
		//图书馆 
		m_listReturn.SetItemText(index,6,m_userBooks.GetAt(index).LibName); 
		//将书籍在Array中的位置绑定 
		m_listReturn.SetItemData(index,index); 
	} 
	//设置列宽 
	SizeAllColumn(); 
} 
 
/*=========================AlignControls============================================== 
说明: 
	初始化界面时或重画面板时重排列控件位置 
====================================================================================*/ 
void CReturnDlg::AlignControls() 
{ 
	//*=========首先调整自身大小(与父窗口一样)========*// 
	CRect rect; 
	this->GetParent()->GetWindowRect(&rect); 
	ScreenToClient(&rect); 
	this->MoveWindow(&rect); 
	//*=================================================*// 
 
	//*==============调整按钮位置=======================*// 
	//调整还书按钮位置 
	CRect rButton; 
	m_btnDel.GetWindowRect(&rButton); 
	ScreenToClient(&rButton); 
	int	height = rButton.Height();		//按钮高度 
	int	width  = rButton.Width();		//按钮长度	 
	rButton.bottom = rect.bottom;		//按钮底与面板底对齐 
	rButton.top = rect.bottom - height; 
	rButton.left = rect.Width()/2 - width/2; 
	rButton.right = rect.Width()/2 + width/2; 
	m_btnDel.MoveWindow(&rButton);		//设置新位置 
	//*=================================================*// 
	m_btnDel.SetFlat(FALSE); 
	m_btnDel.SetIcon(IDI_BOOK); 
	m_btnDel.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); 
	m_btnDel.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(255,0,0)); 
	//*==============调整列表控件位置===================*// 
	//将客户区进行修整 
	rect.DeflateRect(10,10,10,10); 
	rect.bottom -= height;//减去按钮高度 
	m_listReturn.MoveWindow(&rect); 
	//*=================================================*// 
} 
 
void CReturnDlg::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	//重画时重新排列控件位置 
	AlignControls(); 
	// TODO: Add your message handler code here 
	 
	// Do not call CDialog::OnPaint() for painting messages 
} 
 
/*=========================OnButtonDel=============================================== 
说明: 
	处理删除事件函数 
====================================================================================*/ 
void CReturnDlg::OnButtonDel()  
{ 
	//对用户的选择进行判断 
	POSITION pos = m_listReturn.GetFirstSelectedItemPosition(); 
	if (!pos) 
	{ 
		AfxMessageBox("请选择您要删除的书籍,一次可选多本。"); 
		return; 
	}	 
	//首先提出删除警告 
	if (AfxMessageBox("继续操作将永久删除所选书籍信息!是否要继续?",MB_YESNO) == IDNO) 
	{ 
		return; 
	} 
	//*=============获取用户选择的数据项================*// 
	CArray temp_books; 
	int			nItem; 
	BOOKINFO	bookinfo; 
	temp_books.Append(m_userBooks); 
	m_userBooks.RemoveAll();//将其清空 
	while(pos) 
	{ 
		nItem = m_listReturn.GetNextSelectedItem(pos);//nItem从0开始计算 
		//首先获取该书在队列中的位置 
		DWORD d = m_listReturn.GetItemData(nItem); 
		bookinfo = temp_books.GetAt((int)d); 
		bookinfo.Id = 0;//设置标记 
		temp_books.SetAt((int)d,bookinfo); 
	}//end-while 
	//将被设置标记的书籍删除 
	for (int i = 0; i < temp_books.GetSize(); i ++) 
	{ 
		if (temp_books.GetAt(i).Id != 0) 
		{ 
			m_userBooks.Add(temp_books.GetAt(i)); 
		} 
	} 
	//*=================================================*// 
	//重新绘制列表数据 
	RefreshColumnData(); 
 
	//*==============保存数据到数据库===================*// 
	CPBMSView *pMyView = (CPBMSView *)GetParent(); 
	pMyView->DataExchange(3);//调用父窗口(即View窗口)函数实现数据存储 
	//*=================================================*//		 
} 
 
//是否可修改 
void CReturnDlg::MyEnableCtrl(BOOL enable) 
{ 
	m_btnDel.EnableWindow(enable); 
}