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

📄 qcombobox.cpp

📁 Visual C++下的界面设计
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		 pDC->MoveTo(r.left, r.bottom - 2);
		 pDC->LineTo(r.left, r.top);
		 pDC->LineTo(r.right - 1, r.top);
	}
	CBitmap bitmapOEM;
	if (bitmapOEM.LoadOEMBitmap(OBM_COMBO))
	{
		CDC dcMem;
		if (dcMem.CreateCompatibleDC(pDC))
		{
			BITMAP b; bitmapOEM.GetBitmap(&b);
			int leftC = max((r.Width() - b.bmWidth) / 2,1);
			int topC = max((r.Height() - b.bmHeight) / 2,1);
			if (bDown)
			{
				leftC++;
				topC++;
			}
			CBitmap* pOldBitmap = dcMem.SelectObject(&bitmapOEM);
			pDC->BitBlt(r.left + leftC, r.top + topC, r.Width() - (leftC + 1), r.Height() - (topC), &dcMem, 0,0, SRCCOPY);
			pOldBitmap = dcMem.SelectObject(pOldBitmap);
		}
	}
}

// Function name	: CQComboBox::OnPaint
// Description	    : On Draw function
// Return type		: void 
void CQComboBox::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	CRect r; GetClientRect(r);
	r.right--;

	dc.MoveTo(r.left, r.bottom);
	 dc.LineTo(r.left, r.top);
	 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	: CQComboBox::Resize
// Description	    : Call to remove the edit and list controls
// Return type		: void 
void CQComboBox::Resize()
{
	ASSERT (GetListBox());
	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());
	GetListBox()->MoveWindow(r.left, r.top, r.Width(), r.Height());
}

// Function name	: CQComboBox::DropDown
// Description	    : DropDown
// Return type		: void 
// Argument         : BOOL bDown
void CQComboBox::DropDown(BOOL bDown)
{
	if (IsWindowVisible())
		if (IsDropedDown() != bDown)
		{
			Resize();
			LoadPartial(0, +GetVisibleCount());
			GetListBox()->ShowWindow(bDown ? SW_SHOW : SW_HIDE);
			if (bDown)
				GetEdit()->SetFocus();
		}
}

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

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

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

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

	CWnd::OnLButtonUp(nFlags, point);
}

// Function name	: CQComboBox::OnMouseMove
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CQComboBox::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	: CQComboBox::ReleaseButton
// Description	    : Call to release the capture and image of button. After SetButton()
// Return type		: void 
void CQComboBox::ReleaseButton()
{
	if (m_bCaptured)
	{
		ReleaseCapture();
		CDC* pDC = GetDC();
			DrawButton(pDC, m_rectBtn);
		ReleaseDC(pDC);
		m_bCaptured = FALSE;
	}
}

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

// Function name	: CQComboBox::ForwardMessage
// Description	    : This function is called by ListBoxWindowProc
// Return type		: void 
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
BOOL CQComboBox::ForwardMessage(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	ASSERT (GetListBox());
	switch (nMsg)
	{
		case WM_MOUSEMOVE:
		case WM_LBUTTONDOWN:
		{
			BOOL bOutside = TRUE;
			int nIndex = GetListBox()->ItemFromPoint(CPoint(LOWORD(wParam), HIWORD(lParam)), bOutside);
			if (GetCurrentItem() != nIndex)
				SetCurrentItem(nIndex);
			if (nMsg == WM_LBUTTONDOWN)
			{
				DropDown(FALSE);
				SelectCurrentItem();
			}
			break;
		}
		case WM_VSCROLL:
		{
			CListBox* pListBox = GetListBox();
			UINT nScrollCode =  (int) LOWORD(wParam);
			int nHM = NULL, nStep = NULL;
			switch ( nScrollCode )
			{
				case SB_LINEDOWN:
				case SB_PAGEDOWN:
					{
						nHM = GetVisibleCount();
						nStep = (nScrollCode == SB_LINEDOWN ? 1 : nHM);
					}
				case SB_PAGEUP:
				case SB_LINEUP:
				{
					if (!nHM)
					{
						nHM = -GetVisibleCount();
						nStep = (nScrollCode == SB_LINEUP ? -1 : nHM);
					}
					int nPos = pListBox->GetTopIndex();
					LoadPartial( nPos, nHM);
					pListBox->SetTopIndex(nPos + nStep);
					pListBox->SetScrollPos(SB_VERT,  pListBox->GetTopIndex());
					break;
				}
				case SB_THUMBPOSITION:
				{
					int nPos = LoadPartial(HIWORD(wParam), GetVisibleCount());
					pListBox->SetScrollPos(SB_VERT,  HIWORD(wParam));
					pListBox->SetTopIndex(nPos);
					break;
				}
			}
			return FALSE;
			break;
		}
	}
	return TRUE;
}

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

}

// Function name	: CQComboBox::OnCommand
// Description	    : When something is happen in edit control, notify the list control
// Return type		: BOOL 
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
BOOL CQComboBox::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	: CQComboBox::Search
// Description	    : Look for the lpszFindItem
// Return type		: void 
// Argument         : LPCTSTR lpszFindItem
void CQComboBox::Search(LPCTSTR lpszFindItem)
{
	if (CListBox* pListBox = GetListBox())
	{
		LoadPartial(0, GetVisibleCount());
		int nItem = pListBox->FindString(-1, lpszFindItem);
		if (nItem < 0)
		{
			int tLine = LinePartial(lpszFindItem);
			LoadPartial(tLine - GetVisibleCount(), 2 * GetVisibleCount());
			nItem = pListBox->FindString(-1, lpszFindItem);
		}
		SetCurrentItem(nItem);
	}
}

// Function name	: CQComboBox::SelectCurrentItem
// Description	    : Select the current item of list. Called if user click the mouse, oor press ENTER
// Return type		: void 
void CQComboBox::SelectCurrentItem()
{
	int nIndex = GetCurrentItem();
	if (nIndex != LB_ERR)
	{
		CString sItem; GetListBox()->GetText(nIndex, sItem);
		GetListBox()->SetScrollPos(SB_VERT, GetListBox()->GetTopIndex());
		GetEdit()->SetWindowText(sItem);
		//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	: CQComboBox::GetCurrentItem
// Description	    : Get current item from list control
// Return type		: int 
int CQComboBox::GetCurrentItem()
{
	return GetListBox()->GetCurSel();
}

// Function name	: CQComboBox::SetCurrentItem
// Description	    : Set current item from list control to nIndex
// Return type		: void 
// Argument         : int nIndex
void CQComboBox::SetCurrentItem(int nIndex)
{
	GetListBox()->SetCurSel(nIndex);
}

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

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

// Function name	: CQComboBox::SetMultipleHeight
// Description	    : 
// Return type		: int 
// Argument         : int nMHeight
int CQComboBox::SetMultipleHeight(int nMHeight)
{
	int nResult = m_nMultipleHeight;
	m_nMultipleHeight = abs(nMHeight);
	if (::IsWindow(m_hWnd))
	{
		Resize();
		m_nCountVisible = GetVisibleCount();
	}
	return nResult;
}

// Function name	: CQComboBox::GetCountItem

⌨️ 快捷键说明

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