⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formlistctrl.cpp

📁 基于WINDOWS mobile 的用于创建一个窗体和自定义试图的工程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		CFormItem*	pItem;

		pItem = (CFormItem*)GetItemData(m_iEdit);
		if(pItem)
			return pItem->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
	}
	
	return CBaseListCtrl::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}


//---------------------------------------------------------------------------
//
//	CFormListCtrl message handlers
//
//---------------------------------------------------------------------------


// CFormListCtrl::OnCustomDraw
//
//		Custom-draws the list
//
void CFormListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMLVCUSTOMDRAW*	pLVCD = (NMLVCUSTOMDRAW*) pNMHDR;
	DWORD			dwDrawStage;
	int				iItem;

	*pResult = CDRF_DODEFAULT;

	dwDrawStage = pLVCD->nmcd.dwDrawStage;

	iItem = pLVCD->nmcd.dwItemSpec;

	if(dwDrawStage == CDDS_PREPAINT)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if(dwDrawStage == CDDS_ITEMPREPAINT)
	{
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
	}
	else if(dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
	{
		CFormItem*	pItem = (CFormItem*) pLVCD->nmcd.lItemlParam;

		if(pItem)
		{
			pLVCD->clrText		= RGB(  0,   0,   0);
			pLVCD->clrTextBk	= RGB(255, 255, 255);
			*pResult = pItem->CustomDraw(this, pLVCD);
		}

	}
	else if(dwDrawStage == (CDDS_ITEMPOSTPAINT | CDDS_SUBITEM))
	{
		CFormItem*	pItem = (CFormItem*) pLVCD->nmcd.lItemlParam;

		if(pItem)
		{
			pLVCD->clrText		= RGB(  0,   0,   0);
			pLVCD->clrTextBk	= RGB(255, 255, 255);
			*pResult = pItem->CustomDraw(this, pLVCD);
		}
	}
}


// CFormListCtrl::OnGetDispInfo
//
//		Retrieves the display information
//
void CFormListCtrl::OnGetDispInfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO*	pDispInfo = (LV_DISPINFO*)pNMHDR;
	int				iItem;

	iItem	= pDispInfo->item.iItem;

	if(pDispInfo->item.mask & LVIF_TEXT)
	{
		CFormItem*	pItem = (CFormItem*) pDispInfo->item.lParam;

		if(pItem)
			_tcscpy(pDispInfo->item.pszText, pItem->RenderData(pDispInfo));
	}
	
	*pResult = 0;
}


// CFormListCtrl::OnMouseMove
//
//		The user is dragging the mouse on the control
//
void CFormListCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
	if(m_bResizing)
	{
		DrawDivider(m_ptClick.x);

		DrawDivider(point.x);

		m_ptClick.x = point.x;

		return;
	}

	CBaseListCtrl::OnMouseMove(nFlags, point);
}


// CFormListCtrl::OnLButtonDown
//
//		Avoid having MFC handle tap-and-hold
//
void CFormListCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	int		iSubItem,
			iItem		= HitTestEx(point, &iSubItem),
			xDivider	= GetColumnWidth(0);

	//
	// Save the point for later - the control may need it
	//
	m_ptClick = point;

	//
	// Now, check if there is an active item being edited
	//
	if(m_iEdit != -1 && !CloseEditor())
		return;

	//
	// Check if the user is clicking the divider
	//
	if(HIDPIABS(point.x - xDivider) < SCALEX(4))
	{
		m_bResizing = true;

		DrawDivider(point.x);
		return;
	}

	//
	// Do the default processing for the control (MFC stuff)
	//
	Default();

	//
	// Check for any existing selection and deselect it
	//
	if(m_iSelected != -1)
	{
		SelectItem(m_iSelected, FALSE);
		m_iSelected = -1;
	}

	//
	// Now, check if there is a new item to be edited
	//
	if(iItem != -1 && m_bEditable)
	{
		CFormItem*	pEdit = (CFormItem*) GetItemData(iItem);

		if(pEdit && pEdit->IsEnabled())
		{
			//
			// Check if the item has options and if the user is
			// clicking on the caption
			//
			if(pEdit->HasOptions() && iSubItem == 0)
			{
				pEdit->DrawOptions(TRUE);
				m_iEdit = iItem;
				RedrawItems(iItem, iItem);

				ItemOptions(pEdit);

				pEdit->DrawOptions(FALSE);
				RedrawItems(iItem, iItem);
				m_iEdit = -1;
			}
			else
			{
				//
				// Display the editor
				//
				m_iEdit		= iItem;
				m_iSelected = iItem;
				EnsureVisible(iItem, FALSE);
				if(pEdit->ShowEditor(this, TRUE, m_iEdit, iSubItem))
				{
					pEdit->Select(this, TRUE, iItem);
					RedrawItems(m_iEdit, m_iEdit);
				}
				else
					m_iEdit = -1;
			}
		}
		else
			MessageBeep(MB_ICONEXCLAMATION);
	}
}


// CFormListCtrl::OnLButtonUp
//
//		The user released the left mouse button (or stylus)
//		Finish any resizing process
//
void CFormListCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(m_bResizing)
	{
		CRect	rc;

		GetClientRect(&rc);

		SetColumnWidth(0, point.x);
		SetColumnWidth(1, rc.Width() - point.x - GetSystemMetrics(SM_CXVSCROLL));

		InvalidateRect(NULL, TRUE);
	}
	m_bResizing = false;

	CBaseListCtrl::OnLButtonUp(nFlags, point);
}


// CFormListCtrl::OnDestroy
//
//		The form is about to be destroyed
//
void CFormListCtrl::OnDestroy() 
{
	if(m_iEdit != -1)
	{
		CFormItem*	pEdit = (CFormItem*) GetItemData(m_iEdit);

		if(pEdit)
			pEdit->ShowEditor(this, FALSE, m_iEdit, 1);

		m_iEdit = -1;
	}

	CBaseListCtrl::OnDestroy();
}


// CFormListCtrl::OnCommand
//
//		Redirects notifications to the active child
//
BOOL CFormListCtrl::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	if(LOWORD(wParam) == IDC_FORM_ITEM && m_iEdit != -1)
	{
		WORD		wNotify	= HIWORD(wParam);
		CFormItem*	pEdit	= (CFormItem*) GetItemData(m_iEdit);
		
		if(pEdit && pEdit->OnCtrlNotify(this, m_iEdit, wNotify))
			return TRUE;
	}

	return CBaseListCtrl::OnCommand(wParam, lParam);
}


// CFormListCtrl::OnAdjustSip
//
//		Adjusts the current item position for the SIP
//
LRESULT CFormListCtrl::OnAdjustSip(WPARAM wParam, LPARAM)
{
	if(m_iEdit != -1)
	{
		CFormItem*	pEdit	= (CFormItem*) GetItemData(m_iEdit);
		CRect		rc;

		if(wParam)
			EnsureVisible(m_iEdit, FALSE);
		GetSubItemRect(m_iEdit, 1, rc);
		pEdit->MoveEditor(rc);
	}

	return 0;
}


// CFormListCtrl::OnSelectNext
//
//		Selects the next item
//
LRESULT CFormListCtrl::OnSelectNext(WPARAM, LPARAM)
{
	if(m_iSelected == -1)
	{
		m_iSelected = 0;
		SelectItem(m_iSelected, TRUE);
	}
	else if(m_iSelected >= 0 && m_iSelected < GetItemCount())
	{
		SelectItem(m_iSelected, FALSE);
		++m_iSelected;

		if(m_iSelected < GetItemCount())
			SelectItem(m_iSelected, TRUE);
		else
			m_iSelected = -1;
	}
	else
	{
		m_iSelected = -1;
	}

	return 0;
}


// CFormListCtrl::OnSelectPrev
//
//		Selects the previous item
//
LRESULT CFormListCtrl::OnSelectPrev(WPARAM, LPARAM)
{
	if(m_iSelected == -1)
	{
		m_iSelected = GetItemCount() - 1;
		SelectItem(m_iSelected, TRUE);
	}
	else if(m_iSelected >= 0 && m_iSelected < GetItemCount())
	{
		SelectItem(m_iSelected, FALSE);
		--m_iSelected;

		if(m_iSelected >= 0)
			SelectItem(m_iSelected, TRUE);
		else
			m_iSelected = -1;
	}
	else
	{
		m_iSelected = -1;
	}

	return 0;
}


// CFormListCtrl::OnCloseEditor
//
//		Closes the current editor, if any
//
LRESULT CFormListCtrl::OnCloseEditor(WPARAM, LPARAM)
{
	CloseEditor();

	return 0;
}


// CFormListCtrl::OnVScroll
//
//		The form is being vertically scrolled
//
void CFormListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	//
	// Scroll
	//
	CBaseListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);

	//
	// Adjust editor
	//
	OnAdjustSip(0,0);
}


// CFormListCtrl::OnSize
//
//		The form list was resized
//
void CFormListCtrl::OnSize(UINT nType, int cx, int cy)
{
	CBaseListCtrl::OnSize(nType, cx, cy);

	if(nType == SIZE_RESTORED)
	{
		// Adjust the second column so it fills up the space between the first column 
		// and the vertical scroll bar.
		SetColumnWidth(1, cx - GetColumnWidth(0) - GetSystemMetrics(SM_CXVSCROLL));

		if(m_iEdit != -1)
		{
			CFormItem*	pEdit = (CFormItem*) GetItemData(m_iEdit);
			CRect		rc;

			if(GetSubItemRect(m_iEdit, 1, rc))
				pEdit->MoveEditor(rc);
		}
	}
}


// CFormListCtrl::OnKeyDown
//
//		Process the keyboard down messages
//
void CFormListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	switch(nChar)
	{
	case VK_UP:
		OnSelectPrev(0, 0);
		break;

	case VK_DOWN:
		OnSelectNext(0, 0);
		break;

	case VK_RETURN:
		CloseEditor();
		if(m_iSelected >= 0 && m_iSelected < GetItemCount())
		{
			CFormItem*	pEdit = (CFormItem*) GetItemData(m_iSelected);

			m_iEdit = m_iSelected;
			EnsureVisible(m_iEdit, TRUE);
			if(pEdit->ShowEditor(this, TRUE, m_iEdit, 1))
				RedrawItems(m_iEdit, m_iEdit);
			else
				m_iEdit = -1;
		}
		break;

	case VK_LEFT:
		MoveDivider(-(int)nRepCnt * 2);
		break;

	case VK_RIGHT:
		MoveDivider(nRepCnt * 2);
		break;

	default:
		CBaseListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
	}
}


// CFormListCtrl::OnKeyUp
//
//		A key has just been released.
//		When the up or down keys are pressed on the edit control, the parent (this window) gets the focus and
//		receives the corresponding key up events.
//
void CFormListCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	switch(nChar)
	{
	case VK_DOWN:
		if(m_iEdit != -1)
		{
			if(CloseEditor())
				OnSelectNext(0, 0);
		}
		break;

	case VK_UP:
		if(m_iEdit != -1)
		{
			if(CloseEditor())
				OnSelectPrev(0, 0);
		}
		break;

	default:
		CBaseListCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
	}
}

⌨️ 快捷键说明

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