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

📄 multicolumncombobox.cpp

📁 多列功能的组合框
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	 dc.LineTo(r.right, r.top);
	 dc.LineTo(r.right, r.bottom);

	r.left = r.right - defaultSizeDX;
	r.InflateRect(0,-2);r.bottom++;r.right--;r.left++;
	m_rectBtn = r;

	DrawButton(&dc, m_rectBtn);
}

// Function name	: CMultiColumnComboBox::Resize
// Description	    : Call to remove the edit and list controls
// Return type		: void 
void CMultiColumnComboBox::Resize()
{
	ASSERT (GetListCtrl());
	CRect r;
	GetWindowRect(r);
	SetWindowPos(0, 0, 0, r.Width(), defaultSizeDY, SWP_NOMOVE | SWP_NOZORDER | SWP_NOMOVE);
	// Set the height and width of edit control
	GetClientRect(r);
	r.InflateRect(-1,-2); r.bottom++;
	r.right -= defaultSizeDX;
	GetEdit()->MoveWindow(r.left, r.top, r.Width(), r.Height());
	// Set the height and width of list control
	GetWindowRect(r);
	int dy = r.Height();
	r.top = r.bottom;
	r.left++;r.right--;
	r.bottom += m_nMultipleHeight * dy;
	r.right += int(m_dWidthList * r.Width());
	GetListCtrl()->MoveWindow(r.left, r.top, r.Width(), r.Height());
}

// Function name	: CMultiColumnComboBox::DropDown
// Description	    : DropDown
// Return type		: void 
// Argument         : BOOL bDown
void CMultiColumnComboBox::DropDown(BOOL bDown)
{
	if (IsWindowVisible())
	{
		if (IsDropedDown() != bDown)
		{
			Resize();
			GetListCtrl()->ShowWindow(bDown ? SW_SHOW : SW_HIDE);
			if (bDown)
				GetEdit()->SetFocus();
			else
				ReleaseCapture();
		}
	}
}

// Function name	: CMultiColumnComboBox::IsDropedDown
// Description	    : If the list control is dropped downd, return TRUE
// Return type		: BOOL 
BOOL CMultiColumnComboBox::IsDropedDown()
{
	return GetListCtrl()->IsWindowVisible();
}

// Function name	: CMultiColumnComboBox::OnWindowPosChanged
// Description	    : 
// Return type		: void 
// Argument         : WINDOWPOS FAR* lpwndpos
void CMultiColumnComboBox::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
{
	CWnd::OnWindowPosChanged(lpwndpos);
	Resize();
}

// Function name	: CMultiColumnComboBox::OnLButtonDown
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CMultiColumnComboBox::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (m_rectBtn.PtInRect(point))
	{
		SetButton();
		DropDown(!IsDropedDown());
	}
	
	CWnd::OnLButtonDown(nFlags, point);
}

// Function name	: CMultiColumnComboBox::OnLButtonUp
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CMultiColumnComboBox::OnLButtonUp(UINT nFlags, CPoint point) 
{
	ReleaseButton();
	CaptureListCtrl();
	CWnd::OnLButtonUp(nFlags, point);
}

// Function name	: CMultiColumnComboBox::OnMouseMove
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CMultiColumnComboBox::OnMouseMove(UINT nFlags, CPoint point) 
{
	if (m_bCaptured)
	{
		CPoint p; ::GetCursorPos(&p);
		ScreenToClient(&p);
		if (!m_rectBtn.PtInRect(p))
			ReleaseButton();
	}
	
	CWnd::OnMouseMove(nFlags, point);
}

// Function name	: CMultiColumnComboBox::ReleaseButton
// Description	    : Call to release the capture and image of button. After SetButton()
// Return type		: void 
void CMultiColumnComboBox::ReleaseButton()
{
	if (m_bCaptured)
	{
		ReleaseCapture();
		CDC* pDC = GetDC();
			DrawButton(pDC, m_rectBtn);
		ReleaseDC(pDC);
		m_bCaptured = FALSE;
		GetListCtrl()->SetCapture();
	}
}

// Function name	: CMultiColumnComboBox::SetButton
// Description	    : 
// Return type		: void 
void CMultiColumnComboBox::SetButton()
{
	if (!m_bCaptured)
	{
		SetCapture();
		CDC* pDC = GetDC();
			DrawButton(pDC, m_rectBtn, TRUE);
		ReleaseDC(pDC);
		m_bCaptured = TRUE;
	}
}

// Function name	: CMultiColumnComboBox::ForwardMessage
// Description	    : This function is called by ListCtrlWindowProc
// Return type		: void 
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
void CMultiColumnComboBox::ForwardMessage(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	ASSERT (GetListCtrl());
	switch (nMsg)
	{
		case WM_MOUSEMOVE:
		case WM_NCMOUSEMOVE:
		{
			::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
			GetListCtrl()->SetCapture();
		}
		case WM_LBUTTONDOWN:
		{
			CPoint point(LOWORD(lParam), HIWORD(lParam));
			CRect rectClient; GetListCtrl()->GetClientRect(rectClient);
			CRect rectWindow; GetListCtrl()->GetWindowRect(rectWindow);
			CPoint pointScreen(point);
			GetListCtrl()->ClientToScreen(&pointScreen);
			LPARAM lPoint = MAKELPARAM(pointScreen.x, pointScreen.y);
			UINT ht = GetListCtrl()->SendMessage(WM_NCHITTEST,0,lPoint);
			if (ht == HTCLIENT)
			{
				int nIndex = GetListCtrl()->HitTest(CPoint(LOWORD(lParam), HIWORD(lParam)));
				if (GetCurrentItem() != nIndex)
					SetCurrentItem(nIndex);
			}
			else 
				if (rectWindow.PtInRect(pointScreen))
					if (nMsg == WM_LBUTTONDOWN)
					{
						ReleaseCapture();
						GetListCtrl()->SendMessage(WM_NCLBUTTONDOWN, ht, lPoint);
						break;
					};
			if (nMsg == WM_LBUTTONDOWN)
			{
				DropDown(FALSE);
				SelectCurrentItem();
			}
			break;
		}
	}
}

// Function name	: CMultiColumnComboBox::OnSetFocus
// Description	    : When control have focus then edit will take the focus
// Return type		: void 
// Argument         : CWnd* pOldWnd
void CMultiColumnComboBox::OnSetFocus(CWnd* pOldWnd) 
{
	CWnd::OnSetFocus(pOldWnd);
	
	GetEdit()->SetFocus();

}

// Function name	: CMultiColumnComboBox::OnCommand
// Description	    : When something is happen in edit control, notify the list control
// Return type		: BOOL 
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
BOOL CMultiColumnComboBox::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	if (LOWORD(wParam) == IDEDIT)
		if (HIWORD(wParam) == EN_CHANGE)
		{
			ASSERT( GetEdit() && GetEdit()->GetDlgCtrlID() == IDEDIT);
			CString text,t ;
			GetEdit()->GetWindowText(t);
			Search(t);
		}
	
	return CWnd::OnCommand(wParam, lParam);
}

// Function name	: CMultiColumnComboBox::Search
// Description	    : Look for the lpszFindItem
// Return type		: void 
// Argument         : LPCTSTR lpszFindItem
void CMultiColumnComboBox::Search(LPCTSTR lpszFindItem)
{
	if (CListCtrl* pListCtrl = GetListCtrl())
	{
		LV_FINDINFO fInfo;
		 fInfo.flags = LVFI_PARTIAL | LVFI_STRING;
		 fInfo.psz = lpszFindItem;
		int nItem = pListCtrl->FindItem(&fInfo);
		SetCurrentItem(nItem);
		pListCtrl->EnsureVisible(nItem, FALSE); 
	}
}

// Function name	: CMultiColumnComboBox::SelectCurrentItem
// Description	    : Select the current item of list. Called if user click the mouse, oor press ENTER
// Return type		: void 
void CMultiColumnComboBox::SelectCurrentItem()
{
	int nIndex = GetCurrentItem();
	GetEdit()->SetWindowText(GetListCtrl()->GetItemText(nIndex, m_nColumnKey));
	//Notify the parent that one item was changed
	if (nIndex >= 0)
		if (CWnd* pParent = GetParent())
			pParent->SendMessage(m_nSelChange, (WPARAM)GetDlgCtrlID(), (LPARAM)m_hWnd);
}

// Function name	: CMultiColumnComboBox::GetCurrentItem
// Description	    : Get current item from list control
// Return type		: int 
int CMultiColumnComboBox::GetCurrentItem()
{
	return GetListCtrl()->GetNextItem(-1, LVNI_SELECTED);
}

// Function name	: CMultiColumnComboBox::SetCurrentItem
// Description	    : Set current item from list control to nIndex
// Return type		: void 
// Argument         : int nIndex
void CMultiColumnComboBox::SetCurrentItem(int nIndex)
{
	GetListCtrl()->SetItemState(nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
}

// Function name	: CMultiColumnComboBox::OnCreate
// Description	    : Call OnInit if control is created dynamically
// Return type		: int 
// Argument         : LPCREATESTRUCT lpCreateStruct
int CMultiColumnComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	OnInit();
	
	return 0;
}

// Function name	: CMultiColumnComboBox::SetRateWidth
// Description	    : This function will changes the width of inside listcontrol
// Return type		: double 
// Argument         : double dWidthList
double CMultiColumnComboBox::SetRateWidth(double dWidthList)
{
	double dResult = m_dWidthList;
	m_dWidthList = fabs(dWidthList);
	return dResult;
}

// Function name	: CMultiColumnComboBox::SetMultipleHeight
// Description	    : 
// Return type		: int 
// Argument         : int nMHeight
int CMultiColumnComboBox::SetMultipleHeight(int nMHeight)
{
	int nResult = m_nMultipleHeight;
	m_nMultipleHeight = abs(nMHeight);
	return nResult;
}

// Function name	: CMultiColumnComboBox::WindowProc
// Description	    : 
// Return type		: LRESULT 
// Argument         : UINT message
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CMultiColumnComboBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if (GetEdit())
		switch ( message )
		{
			case WM_SETTEXT:
			case WM_GETTEXT:
			{
				GetEdit()->SendMessage( message, wParam, lParam );
				break;
			}
		}
	
	return CWnd::WindowProc(message, wParam, lParam);
}

// Function name	: CMultiColumnComboBox::OnSize
// Description	    : resize combo if needs
// Return type		: void 
// Argument         : UINT nType
// Argument         : int cx
// Argument         : int cy
void CMultiColumnComboBox::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);
	
	Resize();
	
}

// Function name	: CMultiColumnComboBox::CaptureListCtrl
// Description	    : Capture listcontrol to know when the drop will be up
// Return type		: void 
void CMultiColumnComboBox::CaptureListCtrl()
{
	if (IsDropedDown())
		GetListCtrl()->SetCapture();
}

⌨️ 快捷键说明

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