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

📄 cjflatcombobox.cpp

📁 此次上传的使linux下的文件传输协议
💻 CPP
字号:

#include "stdafx.h"
#include "Prog.h"
#include "CJFlatComboBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCJFlatComboBox

CCJFlatComboBox::CCJFlatComboBox()
{
	m_bLBtnDown	= FALSE;
	m_bPainted	= FALSE;
	m_bHasFocus	= FALSE;
	m_bAutoComp = TRUE;

	m_clrBtnHilite  = ::GetSysColor(COLOR_BTNHILIGHT);
	m_clrBtnShadow  = ::GetSysColor(COLOR_BTNSHADOW);
	m_clrBtnFace    = ::GetSysColor(COLOR_BTNFACE);
	m_nOffset		= ::GetSystemMetrics(SM_CXHTHUMB);

	::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
	m_font.CreateFontIndirect(&m_lf);

}

CCJFlatComboBox::~CCJFlatComboBox()
{
	m_font.DeleteObject();
}

IMPLEMENT_DYNAMIC(CCJFlatComboBox, CComboBox)

BEGIN_MESSAGE_MAP(CCJFlatComboBox, CComboBox)
	//{{AFX_MSG_MAP(CCJFlatComboBox)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_CONTROL_REFLECT(CBN_SETFOCUS,   OnSetFocus)
	ON_CONTROL_REFLECT(CBN_KILLFOCUS,  OnKillFocus)
	ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
	ON_WM_SYSCOLORCHANGE()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCJFlatComboBox message handlers

void CCJFlatComboBox::OnMouseMove(UINT nFlags, CPoint point) 
{
	SetTimer(1,10,NULL);
	OnTimer(1);
	CComboBox::OnMouseMove(nFlags, point);
}

void CCJFlatComboBox::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_bLBtnDown = TRUE;
	CComboBox::OnLButtonDown(nFlags, point);
}

void CCJFlatComboBox::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_bLBtnDown = FALSE;
	Invalidate();
	CComboBox::OnLButtonUp(nFlags, point);
}

void CCJFlatComboBox::OnTimer(UINT nIDEvent) 
{
	POINT pt;
	GetCursorPos(&pt);
	CRect rcItem;
	GetWindowRect(&rcItem);

	// OnLButtonDown, show pressed.
	if (m_bLBtnDown==TRUE) {
		KillTimer (1);
		if (m_bPainted == TRUE) {
			DrawCombo( pressed, m_clrBtnShadow, m_clrBtnHilite );
			m_bPainted = FALSE;
		}
		return;
	}

	// If mouse leaves, show flat.
	if (!rcItem.PtInRect(pt) && !m_bHasFocus) 
	{
		KillTimer (1);
		if (m_bPainted == TRUE) {
			DrawCombo( normal, m_clrBtnFace, m_clrBtnFace );
			m_bPainted = FALSE;
		}
		return;
	}

	// On mouse over, show raised.
	else 
	{
		if(m_bHasFocus)
		{
			if (m_bPainted == TRUE)
				return;
			else 
			{
				m_bPainted = TRUE;
				DrawCombo( focus, m_clrBtnShadow, m_clrBtnHilite );
			}
		}
		else
		{
			if (m_bPainted == TRUE)
				return;
			else 
			{
				m_bPainted = TRUE;
				DrawCombo( raised, m_clrBtnShadow, m_clrBtnHilite );
			}
		}
	}
	
	CComboBox::OnTimer(nIDEvent);
}

void CCJFlatComboBox::OnPaint() 
{
	ModifyStyleEx (WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE,
		0, SWP_FRAMECHANGED);
	
	Default();

	CPoint pt;
	GetCursorPos(&pt);

	CRect rcItem;
	GetWindowRect(&rcItem);
	if ((rcItem.PtInRect(pt)))
	{
		if(m_bHasFocus)
			DrawCombo( raised, m_clrBtnShadow, m_clrBtnHilite );
		else
			DrawCombo( focus, m_clrBtnShadow, m_clrBtnHilite );
	}
	else
	{
		DrawCombo( normal, m_clrBtnFace, m_clrBtnFace );
	}
}

void CCJFlatComboBox::DrawCombo(STATE eState, COLORREF clrTopLeft, COLORREF clrBottomRight)
{
	CRect rcItem;
	GetClientRect(&rcItem);
	CDC* pDC = GetDC();
	
	pDC->Draw3dRect(rcItem,RGB(123,158,189),RGB(123,158,189));   //绘制最外面的边框

	rcItem.DeflateRect(1,1);
	pDC->Draw3dRect(rcItem,RGB(255,255,255),RGB(255,255,255));   //绘制最外面的边框

	CBrush m_brush;
	CPen penBlue;
	CPen* pOldPen;
	CBrush brushRed;
	CBrush* pOldBrush;

	if(!IsWindowEnabled())
	{
		m_brush.CreateSolidBrush(RGB(192,192,192));
		pDC->FillRect(rcItem,&m_brush);
	}

	rcItem.DeflateRect(1,1);
	rcItem.left = rcItem.right-m_nOffset;

	CPoint pts[3];
	pts[0].x = rcItem.left + 4;
	pts[0].y = rcItem.top+5;
	pts[1].x = rcItem.right-4;
	pts[1].y = rcItem.top + 5;
	pts[2].x = rcItem.left+rcItem.Width()/2;
	pts[2].y = rcItem.top + 9;

	if(!IsWindowEnabled())
	{
//		m_brush.CreateSolidBrush(RGB(192,192,192));
//		pDC->FillRect(rcItem,&m_brush);
		penBlue.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
		pOldPen = pDC->SelectObject(&penBlue);
		brushRed.CreateSolidBrush(RGB(0, 0, 0));
		pOldBrush = pDC->SelectObject(&brushRed);
		pDC->Polygon(pts, 3);
		pDC->SelectObject(pOldPen);
		pDC->SelectObject(pOldBrush);
		ReleaseDC(pDC);
		return;
	}
	switch (eState)
	{
	case normal:

		m_brush.CreateSolidBrush(RGB(225,233,241));
		pDC->FillRect(rcItem,&m_brush);
		penBlue.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
		pOldPen = pDC->SelectObject(&penBlue);
		brushRed.CreateSolidBrush(RGB(0, 0, 0));
		pOldBrush = pDC->SelectObject(&brushRed);
		pDC->Polygon(pts, 3);
		pDC->SelectObject(pOldPen);
		pDC->SelectObject(pOldBrush);

		break;

	case raised:
		m_brush.CreateSolidBrush(RGB(194,210,224));
		pDC->FillRect(rcItem,&m_brush);
		penBlue.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
		pOldPen = pDC->SelectObject(&penBlue);
		brushRed.CreateSolidBrush(RGB(0, 0, 0));
		pOldBrush = pDC->SelectObject(&brushRed);
		pDC->Polygon(pts, 3);
		pDC->SelectObject(pOldPen);
		pDC->SelectObject(pOldBrush);
		break;

	case pressed:
		m_brush.CreateSolidBrush(RGB(194,210,224));
		pDC->FillRect(rcItem,&m_brush);
		penBlue.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
		pOldPen = pDC->SelectObject(&penBlue);
		brushRed.CreateSolidBrush(RGB(0, 0, 255));
		pOldBrush = pDC->SelectObject(&brushRed);
		pDC->Polygon(pts, 3);
		pDC->SelectObject(pOldPen);
		pDC->SelectObject(pOldBrush);
		break;

	case focus:
		m_brush.CreateSolidBrush(RGB(194,210,224));
		pDC->FillRect(rcItem,&m_brush);
		penBlue.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
		pOldPen = pDC->SelectObject(&penBlue);
		brushRed.CreateSolidBrush(RGB(0, 0, 255));
		pOldBrush = pDC->SelectObject(&brushRed);
		pDC->Polygon(pts, 3);
		pDC->SelectObject(pOldPen);
		pDC->SelectObject(pOldBrush);
		break;
	}

	ReleaseDC(pDC);
}

void CCJFlatComboBox::OnSetFocus()
{
	m_bHasFocus = TRUE;
	DrawCombo( focus, m_clrBtnShadow, m_clrBtnHilite );
//	if(pChildComboBox!=NULL)
//	{
//		pChildComboBox->ResetContent();
//	}
}

void CCJFlatComboBox::OnKillFocus() 
{
	m_bHasFocus = FALSE;
	DrawCombo( normal, m_clrBtnFace, m_clrBtnFace );
}

BOOL CCJFlatComboBox::PreTranslateMessage(MSG* pMsg) 
{
	// Catch the Alt key so we don't choke if focus 
	// is going to an owner drawn button
	if (pMsg->message == WM_SYSCHAR)
		return TRUE;
	return CComboBox::PreTranslateMessage(pMsg);
}

void CCJFlatComboBox::OnEditUpdate() 
{
	// if we are not to auto update the text, get outta here
	if (!m_bAutoComp) 
		return;
	
	// Get the text in the edit box
	CString str;
	GetWindowText(str);
	int nLength = str.GetLength();
	
	// Currently selected range
	DWORD dwCurSel = GetEditSel();
	WORD dStart = LOWORD(dwCurSel);
	WORD dEnd   = HIWORD(dwCurSel);
	
	// Search for, and select in, and string in the combo box that is prefixed
	// by the text in the edit box
	if (SelectString(-1, str) == CB_ERR)
	{
		SetWindowText(str);		// No text selected, so restore what was there before
		if (dwCurSel != CB_ERR)
		{
			SetEditSel(dStart, dEnd);	//restore cursor postion
		}
	}
	
	// Set the text selection as the additional text that we have added
	if (dEnd < nLength && dwCurSel != CB_ERR)
	{
		SetEditSel(dStart, dEnd);
	}
	else
	{
		SetEditSel(nLength, -1);
	}
}

void CCJFlatComboBox::OnSysColorChange() 
{
	CComboBox::OnSysColorChange();

	// re-initialize system colors.
	m_clrBtnHilite  = ::GetSysColor(COLOR_BTNHILIGHT);
	m_clrBtnShadow  = ::GetSysColor(COLOR_BTNSHADOW);
	m_clrBtnFace    = ::GetSysColor(COLOR_BTNFACE);
}

void CCJFlatComboBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CComboBox::OnKeyDown(nChar, nRepCnt, nFlags);
	switch(nChar)
	{
	case VK_UP:
	case VK_DOWN:
		ShowDropDown();
		break;
	case 13:
		break;
	default:
		break;
	}
}


void CCJFlatComboBox::ReconstructFont()
{
	m_font.DeleteObject();
	BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
	ASSERT(bCreated);
	SetFont(&m_font);
}

void CCJFlatComboBox::SetFontName(const CString& strFont)
{
	strcpy(m_lf.lfFaceName,strFont);
	ReconstructFont();
	RedrawWindow();
}

void CCJFlatComboBox::SetFontBold(BOOL bBold)
{
	m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
	ReconstructFont();
	RedrawWindow();
}

void CCJFlatComboBox::SetFontUnderline(BOOL bSet)
{
	m_lf.lfUnderline = bSet;
	ReconstructFont();
	RedrawWindow();
}

void CCJFlatComboBox::SetFontItalic(BOOL bSet)
{
	m_lf.lfItalic = bSet;
	ReconstructFont();
	RedrawWindow();
}

void CCJFlatComboBox::SetFontSize(int nSize)
{
	nSize=-(int)(nSize*1.333);
	m_lf.lfHeight = nSize;
	ReconstructFont();
	RedrawWindow();
}

CString CCJFlatComboBox::GetFontName()
{
	return CString(m_lf.lfFaceName);
}

BOOL CCJFlatComboBox::GetFontB()
{
	if(m_lf.lfWeight==FW_BOLD)
		return TRUE;
	else
		return FALSE;
}

BOOL CCJFlatComboBox::GetFontU()
{
	return m_lf.lfUnderline;
}

BOOL CCJFlatComboBox::GetFontI()
{
	return m_lf.lfItalic;
}

int CCJFlatComboBox::GetFontSize()
{
	return -(int)(m_lf.lfHeight/1.333);
}

void CCJFlatComboBox::Reset()
{
	ResetContent();
	aryData.RemoveAll();
}

long CCJFlatComboBox::AddString1(LPCTSTR sText, LPCTSTR sLsh)
{
	int nPos=aryData.Add(sLsh);
	long lRet=AddString(sText);
	SetItemData(lRet,nPos);
	return lRet;
}

CString CCJFlatComboBox::GetLsh(long nIndex)
{
	CString strResult;
	if(nIndex>-1)
	{
		int nPos=GetItemData(nIndex);
		if(nPos>-1 && nPos<aryData.GetSize())
		{
			strResult=aryData.GetAt(nPos);
		}
	}
	return strResult;
}

long CCJFlatComboBox::InsertString1(long nIndex, LPCTSTR sText, LPCTSTR sLsh)
{
	int nPos=aryData.Add(sLsh);
	long lRet=InsertString(nIndex,sText);
	SetItemData(lRet,nPos);
	return lRet;
}

CString CCJFlatComboBox::GetCurLsh()
{
	return GetLsh(GetCurSel());
}

BOOL CCJFlatComboBox::SetCurLsh(LPCTSTR sCurLsh)
{
	CString s1,s2;
	s1=sCurLsh;
	for(int i=0;i<GetCount();i++)
	{
		s2=GetLsh(i);
		if(s1==s2)
		{
			SetCurSel(i);
			break;
		}
	}
	return TRUE;
}


void CCJFlatComboBox::SetFontLog(LOGFONT &plf, int nType)
{
	CString str;
	m_lf.lfCharSet=plf.lfCharSet;
	m_lf.lfClipPrecision=plf.lfClipPrecision;
	m_lf.lfEscapement=plf.lfEscapement;
	str=CString(plf.lfFaceName);
	strcpy(m_lf.lfFaceName,str);
	
	switch(nType)
	{
	case 1:
		m_lf.lfHeight=theApp.jns.DoubleToInt(plf.lfHeight);
		break;
	case 2:
		m_lf.lfHeight=theApp.jns.DoubleToInt(plf.lfHeight/15.3);
		break;
	default:
		break;
	}
	m_lf.lfItalic=plf.lfItalic;
	m_lf.lfOrientation=plf.lfOrientation;
	m_lf.lfOutPrecision=plf.lfOutPrecision;
	m_lf.lfPitchAndFamily=plf.lfPitchAndFamily;
	m_lf.lfQuality=plf.lfQuality;
	m_lf.lfStrikeOut=plf.lfStrikeOut;
	m_lf.lfUnderline=plf.lfUnderline;
	m_lf.lfWeight=plf.lfWeight;
	m_lf.lfWidth=plf.lfWidth;
	ReconstructFont();
	RedrawWindow();
}

⌨️ 快捷键说明

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