www.pudn.com > Shutdown.rar > Y_Cls_ListCtrl_.cpp


// Y_Cls_ListCtrl_.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Y_Cls_ListCtrl_.h" 
#include "Resource.h" 
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// Y_Cls_ListCtrl_ 
 
Y_Cls_ListCtrl_::Y_Cls_ListCtrl_() 
{ 
	Old_iItem = 0 ; 
	Old_iSubItem = 0; 
	m_pMyButton = NULL; 
	m_pEdit = NULL; 
	m_pComboBox = NULL; 
	m_pMouth = NULL; 
} 
 
Y_Cls_ListCtrl_::~Y_Cls_ListCtrl_() 
{ 
} 
BEGIN_MESSAGE_MAP(Y_Cls_ListCtrl_, CListCtrl) 
	//{{AFX_MSG_MAP(Y_Cls_ListCtrl_) 
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick) 
	ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit) 
	ON_WM_HSCROLL() 
	ON_WM_VSCROLL() 
	ON_WM_DESTROY() 
	//}}AFX_MSG_MAP 
	ON_EN_KILLFOCUS(IDC_NEWEDIT,cyp_EditKillFocus)	 
	ON_BN_CLICKED(IDC_NEWMYBUTTON,OnClickMyButton) 
	ON_MESSAGE(MSG_KILLMOUTH,DeleteMouth)//不执行 
	ON_MESSAGE(MSG_SELECTDATE,SelectDate) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// Y_Cls_ListCtrl_ message handlers 
 
void Y_Cls_ListCtrl_::OnClick(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	// TODO: Add your control notification handler code here] 
	if(m_pMyButton!=NULL) 
	{ 
		delete m_pMyButton; 
		m_pMyButton = NULL; 
	} 
	if(m_pMouth!=NULL) 
	{ 
		delete m_pMouth; 
		m_pMouth =NULL; 
	} 
	//--------------------- 
	int iItem,iSubItem; 
	UINT nID = GetDlgCtrlID(); 
	CString strItemText; 
	POINT pt; 
	POINT ptScreen; 
	CString strTmp; 
	CString strTmpList[50]; 
 
 
	LVHITTESTINFO hittestinfo; 
	::GetCursorPos(&pt); 
	ptScreen = pt; 
	ScreenToClient(&pt); 
	hittestinfo.pt = pt; 
	SubItemHitTest(&hittestinfo);  
	Old_iItem= iItem = hittestinfo.iItem; 
	Old_iSubItem = iSubItem = hittestinfo.iSubItem; 
	//============================================ 
	NMLISTVIEW *pListView = (NMLISTVIEW*)pNMHDR; 
	Old_iItem= iItem = pListView->iItem; 
	Old_iSubItem = iSubItem = pListView->iSubItem; 
	//============================================ 
	strItemText=GetItemText(iItem,iSubItem); 
	GetSubItemRect(iItem,iSubItem,LVIR_BOUNDS,m_Rect); 
	m_Rect.left += 1; 
	m_Rect.right -= 1; 
	Old_Rect = m_Rect; 
	//保证条目可见 
	if(!EnsureVisible(iItem,TRUE)) 
		return; 
	//计算0列区域 
	if(iSubItem==0) 
	{ 
		int nWidth = GetColumnWidth(0); 
		m_Rect.right = m_Rect.left + nWidth; 
	} 
	m_ID = this->GetDlgCtrlID(); 
 
	if(m_ID == IDC_LIST_Progress) 
	{ 
		if(iSubItem ==0)// 
		{ 
			CRect buRect; 
			buRect = m_Rect; 
			buRect.left = m_Rect.left+3*(m_Rect.Width())/4; 
			m_pMyButton = new Y_Cls_Button_; 
			m_pMyButton->Create(_T(""), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_ICON, 
						buRect, this, IDC_NEWMYBUTTON); 
			m_pMyButton->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));					 
			m_Rect.right = m_Rect.left+m_Rect.Width()*3/4;				 
			CreateEdit(2,strItemText,1024); 
			 
		} 
		else if(iSubItem ==1||iSubItem ==2) 
		{ 
			CreateEdit(0,strItemText,2);	 
		} 
 
	} 
//	else if(m_ID == IDC_LIST_Slave){ 
//		if(iSubItem!=0) 
//		{ 
//			if(iSubItem ==2||iSubItem ==3){ 
//				this->CreateEdit(0,strItemText,3); 
//			} 
//			else{ 
//				CreateEdit(1,strItemText,5); 
//			} 
//		} 
//	} 
//	else if(m_ID == IDC_LIST_Static_Load) 
//	{ 
//		if(iSubItem ==1&&iItem!=0) 
//		{ 
//			CreateEdit(0,strItemText,4); 
//		} 
//	}	 
//	else if(m_ID==IDC_SETLG_LIST|| 
//		m_ID==IDC_SETBHG_LIST|| 
//		m_ID==IDC_SETSHG_LIST|| 
//		m_ID==IDC_SETJDC_LIST) 
//	{ 
//		if(iSubItem!=0) 
//			CreateEdit(1,strItemText,250); 
//	} 
	*pResult = 0; 
} 
 
void Y_Cls_ListCtrl_::GetComboText() 
{ 
	CString sss; 
	GetDlgItemText(IDC_NEYMYCOMBO,sss); 
	SetItemText(Old_iItem,Old_iSubItem,sss);  
	::SendMessage(this->GetParent()->m_hWnd,MSG_COMSELCHANGE,WPARAM(Old_iItem),LPARAM(Old_iSubItem));	  
} 
 
void Y_Cls_ListCtrl_::cyp_EditKillFocus() 
{ 
	//基本失去焦点事件 
	CString sss; 
	GetDlgItemText(IDC_NEWEDIT,sss); 
 
	SetItemText(Old_iItem,Old_iSubItem,sss); 
 
	if(m_pEdit != NULL) 
	{ 
		delete m_pEdit; 
		m_pEdit = NULL;	  		 
	}	 
	::SendMessage(this->GetParent()->m_hWnd,MSG_EDITFINISH,WPARAM(Old_iItem),LPARAM(Old_iSubItem));	  
} 
 
void Y_Cls_ListCtrl_::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR; 
	// TODO: Add your control notification handler code here 
	int ii = (pDispInfo->item).iItem; 
	int nS = (pDispInfo->item).mask; 
	CString str; 
	if  (ii != -1 && nS == 1)  
	{ 
		str.Format("%s",(pDispInfo->item).pszText); 
		if(str.GetLength()==0) 
		{ 
			 AfxMessageBox("请输入非空字符串!");  
			 *pResult = 0;  
			 return; 
		} 
	 
	SetItemText((pDispInfo->item).iItem, 
	                       (pDispInfo->item).iSubItem, 
	  		       (pDispInfo->item).pszText); 
	} 
	*pResult = 0; 
} 
 
void Y_Cls_ListCtrl_::OnClickMyButton() 
{ 
	DelBtn(); 
	::SendMessage(this->GetParent()->m_hWnd,MSG_CLICK_BUTTON,0,0);	  
} 
 
void Y_Cls_ListCtrl_::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(GetFocus() != this) SetFocus(); 
	CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar); 
} 
 
void Y_Cls_ListCtrl_::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(GetFocus() != this) SetFocus(); 
	CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar); 
} 
 
void Y_Cls_ListCtrl_::OnDestroy()  
{ 
	CListCtrl::OnDestroy(); 
	if(m_pMyButton!=NULL) 
	{ 
		delete m_pMyButton; 
		m_pMyButton = NULL; 
	} 
	if(m_pEdit!=NULL) 
	{ 
		delete m_pEdit; 
		m_pEdit = NULL; 
	} 
	if(m_pComboBox!=NULL) 
	{ 
		delete m_pComboBox; 
		m_pComboBox = NULL; 
	}	 
} 
 
void Y_Cls_ListCtrl_::CreateEdit(int nStyle,CString str,int nLimit) 
{	 	 
	if(nStyle==0) 
	{ 
		m_pEdit = new Y_Cls_Edit_;     
		m_pEdit->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_NOHIDESEL|ES_NUMBER	 , 
			m_Rect, this, IDC_NEWEDIT); 
		m_pEdit = m_pEdit; 
	} 
	else if(nStyle==1) 
	{ 
		m_pEdit = new Y_Cls_Edit_;     
		m_pEdit->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_NOHIDESEL,//|ES_NUMBER	 , 
			m_Rect, this, IDC_NEWEDIT); 
		m_pEdit = m_pEdit; 
	} 
	else if(nStyle ==2) 
	{ 
		m_pEdit = (Y_Cls_Edit_*)new CEdit; 
		m_pEdit->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_NOHIDESEL,//|ES_NUMBER	 , 
			m_Rect, this, IDC_NEWEDIT); 
	} 
	else 
	{ 
		m_pEdit = (Y_Cls_Edit_*)new CEdit; 
		m_pEdit->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_NOHIDESEL,//|ES_NUMBER	 , 
			m_Rect, this, IDC_NEWEDIT); 
 
	} 
	m_pEdit->SetFont(GetFont()); 
	m_pEdit->SetWindowText(str); 
	m_pEdit->SetSel(0,-1);  
	m_pEdit->SetFocus(); 
	m_pEdit->SetLimitText(nLimit); 
 
} 
//rect 大小 
//strData 要装载字符串的 
//nCount  字符串个数 
//EanbleEdit 是否允许编辑 
void Y_Cls_ListCtrl_::Create_ComboBox(CRect rect, CString *strData, int nCount,int EnableEdit) 
{ 
	CString str; 
	CRect Rect; 
	Rect = rect; 
	int i; 
	Rect.bottom += rect.Height()*8; 
	m_pComboBox  = new Y_Cls_Combox_; 
	if(EnableEdit==1) 
	m_pComboBox ->Create(WS_CHILD|WS_VISIBLE|WS_BORDER 
			|WS_VSCROLL|CBS_DROPDOWN, 
			Rect,  this, IDC_NEYMYCOMBO); 
	else 
		m_pComboBox ->Create(WS_CHILD|WS_VISIBLE|WS_BORDER 
			|CBS_DROPDOWNLIST|WS_VSCROLL,////CBS_DROPDOWN 
			Rect,  this, IDC_NEYMYCOMBO); 
	m_pComboBox->SetFont(GetFont()); 
	 
 
	m_pComboBox->Old_iItem = Old_iItem; 
	m_pComboBox->Old_iSubItem = Old_iSubItem; 
	m_pComboBox->m_ID = m_ID; 
	for(i = 0;iAddString(strData[i]); 
	} 
	 
	m_pComboBox->SetFocus(); 
	str = GetItemText(Old_iItem,Old_iSubItem); 
	if(m_pComboBox->SelectString(0,str)==CB_ERR) 
		m_pComboBox->SetCurSel(0); 
 
 
	//如果字符太长对ComboBox进行扩展 
	CSize   sz; 
	int     dx=0; 
	CDC*    pDC = m_pComboBox->GetDC(); 
	for ( i=0;i < m_pComboBox->GetCount();i++) 
	{ 
		m_pComboBox->GetLBText( i, str ); 
		sz = pDC->GetTextExtent(str); 
		 
		if (sz.cx > dx) 
			dx = sz.cx; 
	} 
	m_pComboBox->ReleaseDC(pDC); 
	 
	// Adjust the width for the vertical scroll bar and the left and 
	// right border. 
	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE); 
	 
	// Set the width of the list box so that every item is completely visible. 
	m_pComboBox->SetDroppedWidth(dx); 
	 
} 
 
LRESULT Y_Cls_ListCtrl_::DeleteMouth(WPARAM wp, LPARAM lp) 
{ 
	if(m_pMouth!=NULL) 
	{ 
		delete m_pMouth; 
		m_pMouth =NULL; 
	} 
	return 1; 
} 
LRESULT Y_Cls_ListCtrl_::SelectDate(WPARAM wp, LPARAM lp) 
{ 
	char ch_time[80]; 
	strcpy(ch_time,(char*)(wp)); 
	SetItemText(Old_iItem,Old_iSubItem,ch_time); 
	return 1; 
} 
void Y_Cls_ListCtrl_::CreateMouthCtrl() 
{ 
	CRect rc_mouth; 
	int nHeight; 
	m_pMouth = new Y_Cls_Mouth_; 
	rc_mouth.top = m_Rect.bottom; 
	rc_mouth.left = 0; 
	rc_mouth.right = rc_mouth.left+180; 
	rc_mouth.bottom = rc_mouth.top+90; 
	m_pMouth->Create(WS_DLGFRAME|WS_VISIBLE,rc_mouth,this,IDC_NEWTIMECTRL); 
 
	m_pMouth->SizeMinReq(); 
	m_pMouth->GetWindowRect(rc_mouth); 
	this->ScreenToClient(rc_mouth); 
	nHeight = rc_mouth.Height(); 
 
	rc_mouth.bottom = m_Rect.top; 
	rc_mouth.left = 0; 
	rc_mouth.right =rc_mouth.left+rc_mouth.Width(); 
	rc_mouth.top = rc_mouth.bottom -nHeight; 
 
	m_pMouth->MoveWindow(rc_mouth); 
} 
//================================================== 
void Y_Cls_ListCtrl_::SetCurSel(int curItem) 
{ 
	CPoint pt; 
	CRect rc; 
	LPARAM lp; 
	int nCount  = this->GetItemCount(); 
	if (nCount <=0) 
		return; 
	else if(curItem<0||curItem>=nCount) 
		return; 
	if(!EnsureVisible(curItem,TRUE)) 
		return; 
	GetSubItemRect(curItem,0,LVIR_BOUNDS,rc); 
 
	pt.x = 4; 
	pt.y = rc.bottom-4; 
	lp = MAKELPARAM(pt.x,pt.y); 
	this->SetFocus(); 
	this->PostMessage(WM_LBUTTONDOWN,MK_LBUTTON,lp); 
	this->PostMessage(WM_LBUTTONUP,MK_LBUTTON,lp); 
	//this->Old_iItem = curItem; 
} 
 
void Y_Cls_ListCtrl_::DelBtn() 
{ 
	if(m_pMyButton!=NULL) 
	{ 
			delete m_pMyButton; 
			m_pMyButton = NULL; 
	} 
} 
BOOL Y_Cls_ListCtrl_::DeleteItem( int nItem ) 
{ 
	DelBtn(); 
	return CListCtrl::DeleteItem(nItem); 
 
}