doclistcombobox.cpp
来自「windows ce开发技巧与实例光盘代码」· C++ 代码 · 共 261 行
CPP
261 行
// DocListComboBox.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "DocListComboBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDocListComboBox
CDocListComboBox::CDocListComboBox()
{
m_strTitle = _T("Sort By");
m_strSelectedText = _T("Name");
m_strCurrentText = m_strSelectedText;
m_bBorderState = FALSE;
m_clrBackground = ::GetSysColor(COLOR_3DFACE);
m_nLeftShift = 2;
m_nIconWidth = 0;
m_nDownWidth = 14;
m_bDownState = FALSE;
m_bBorderState = FALSE;
m_pMenuOwner = NULL;
m_bLeftAlign = TRUE;
}
CDocListComboBox::~CDocListComboBox()
{
if (m_pMenuOwner) {
delete m_pMenuOwner;
}
}
BEGIN_MESSAGE_MAP(CDocListComboBox, CStatic)
//{{AFX_MSG_MAP(CDocListComboBox)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDocListComboBox message handlers
void CDocListComboBox::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetClientRect(&rect);
CFont font;
CreateFont(font);
CFont* pOldFont = dc.SelectObject(&font);
dc.SetBkColor(m_clrBackground);
CSize szText = dc.GetTextExtent(m_strCurrentText);
m_nTestWidth = szText.cx;
int nTextYShift = (rect.Height() - szText.cy)/2;
int nTextXShift = GetLeftAlignShift() + m_nLeftShift + m_nIconWidth;
CRect rtText(CPoint(nTextXShift, nTextYShift), szText);
dc.DrawText(m_strCurrentText, &rtText, DT_LEFT | DT_TOP | DT_SINGLELINE);
DrawDownIcon(&dc);
int nRight = GetLeftAlignShift()+m_nLeftShift+m_nIconWidth+m_nTestWidth+m_nDownWidth;
int nLeft = GetLeftAlignShift();
if (m_bBorderState) {
dc.MoveTo(nLeft,0);
dc.LineTo(nRight, 0);
dc.LineTo(nRight, rect.bottom-1);
dc.LineTo(nLeft, rect.bottom-1);
dc.LineTo(nLeft, 0);
}
dc.SelectObject(pOldFont);
}
void CDocListComboBox::CreateFont(CFont &font)
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = 16;
lf.lfWeight = FW_BOLD;
lstrcpy(lf.lfFaceName, CString(_T("Tahoma")));
font.CreateFontIndirect(&lf);
}
void CDocListComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();
if (
(point.x<GetLeftAlignShift()+m_nLeftShift+m_nIconWidth+m_nTestWidth) &&
(point.x>=GetLeftAlignShift())
) {
m_bBorderState = TRUE;
Invalidate();
UpdateWindow();
} else {
TrackDropDown();
}
// CStatic::OnLButtonDown(nFlags, point);
}
void CDocListComboBox::OnLButtonUp(UINT nFlags, CPoint point)
{
ReleaseCapture();
if (m_bBorderState) {
m_bBorderState = FALSE;
CRect rect;
GetClientRect(&rect);
CRect rtName(CPoint(GetLeftAlignShift(),0), CSize(m_nLeftShift+m_nIconWidth+m_nTestWidth, rect.Height()));
if (rtName.PtInRect(point)) {
TrackDropDown();
} else {
Invalidate();
UpdateWindow();
}
}
// CStatic::OnLButtonUp(nFlags, point);
}
BOOL CDocListComboBox::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
CBrush brush;
brush.CreateSolidBrush(m_clrBackground);
pDC->FillRect(&rect, &brush);
return CStatic::OnEraseBkgnd(pDC);
}
void CDocListComboBox::DrawDownIcon(CDC *pDC)
{
ASSERT(pDC);
CRect rect;
GetClientRect(&rect);
int nXShift = GetLeftAlignShift() + m_nLeftShift + m_nIconWidth + m_nTestWidth;
int nX = nXShift + (m_nDownWidth-7)/2;
int nY = rect.bottom/2 + 0;
pDC->MoveTo(nX+0, nY);
pDC->LineTo(nX+7, nY);
pDC->MoveTo(nX+1, nY+1);
pDC->LineTo(nX+6, nY+1);
pDC->MoveTo(nX+2, nY+2);
pDC->LineTo(nX+5, nY+2);
pDC->MoveTo(nX+3, nY+3);
pDC->LineTo(nX+4, nY+3);
}
void CDocListComboBox::TrackDropDown()
{
m_bDownState = TRUE;
m_strCurrentText = m_strTitle;
Invalidate();
UpdateWindow();
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
if (m_pMenuOwner) {
CMenu *pDropDownMenu = m_pMenuOwner->GetSubMenu(0);
if (pDropDownMenu) {
pDropDownMenu->TrackPopupMenu(TPM_LEFTALIGN, GetLeftAlignShift() + rect.left, rect.bottom, GetParent(), NULL);
}
}
m_bDownState = FALSE;
m_strCurrentText = m_strSelectedText;
Invalidate();
UpdateWindow();
}
BOOL CDocListComboBox::SetDropDownMenu(UINT nIDResource)
{
if (m_pMenuOwner) {
delete m_pMenuOwner;
m_pMenuOwner = NULL;
}
m_pMenuOwner = new CMenu;
BOOL bResult = m_pMenuOwner->LoadMenu(nIDResource);
if (bResult) {
return TRUE;
} else {
return FALSE;
}
}
BOOL CDocListComboBox::SetDropDownMenu(CMenu *pMenuOwner)
{
if (m_pMenuOwner) {
delete m_pMenuOwner;
m_pMenuOwner = NULL;
}
m_pMenuOwner = pMenuOwner;
return TRUE;
}
void CDocListComboBox::SetTitleText(const CString &strTitleText)
{
m_strTitle = strTitleText;
if (m_bDownState) {
m_strCurrentText = m_strTitle;
Invalidate();
UpdateWindow();
}
}
void CDocListComboBox::SetSelectedText(const CString &strSelectedText)
{
m_strSelectedText = strSelectedText;
if (!m_bDownState) {
m_strCurrentText = m_strSelectedText;
Invalidate();
UpdateWindow();
}
}
void CDocListComboBox::SetAlign(BOOL bLeftAlign)
{
m_bLeftAlign = bLeftAlign;
}
int CDocListComboBox::GetLeftAlignShift()
{
if (m_bLeftAlign) {
return 0;
} else {
CRect rect;
GetClientRect(&rect);
return rect.Width() - m_nLeftShift - m_nIconWidth - m_nTestWidth - m_nDownWidth-1;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?