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

📄 gfxincombo.cpp

📁 我自己整理的一些VC源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GfxInCombo.cpp : implementation file
//

#include "stdafx.h"
#include "SellMan.h"


#include "GfxInCombo.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGfxInCombo

CGfxInCombo::CGfxInCombo(int iItem, int iSubItem, CString sInitText, CStringArray * pComboArray, bool _bEditable, bool _bMouseSelect)
{
	bMouseSelect = _bMouseSelect;
	pArray = pComboArray;
	m_iItem = iItem;
	m_iSubItem = iSubItem;
	m_bESC = FALSE;
	hArrow = LoadBitmap(NULL, MAKEINTRESOURCE(32738));
	ASSERT(hArrow);
	m_sInitText = sInitText;
	wndList.pCombo = this;
	wndEdit.pCombo = this;
	wndStatic.pCombo = this;
	iButtonDx = GetSystemMetrics(SM_CXHSCROLL);
//	pFont = NULL;
	hFontHandle = NULL;
	bEditable = _bEditable;
	m_bAutoComplete = true;
	iCurSel = -1;

	bProcessed = false;
	bLightBorder = true;

	crColor = GetSysColor(COLOR_WINDOWTEXT);
}

CGfxInCombo::~CGfxInCombo()
{
	if (pArray)
	{
		pArray->RemoveAll();
		delete pArray;
	}
}


BEGIN_MESSAGE_MAP(CGfxInCombo, CWnd)
	//{{AFX_MSG_MAP(CGfxInCombo)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_KILLFOCUS()
	ON_WM_NCDESTROY()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_CHAR()
	ON_WM_SETFOCUS()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_SELENDOK, OnSelendOk)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CGfxInCombo message handlers

int CGfxInCombo::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	CRect rc;
	GetClientRect(rc);
	CRect btrc(rc.left, rc.top, rc.right - iButtonDx, rc.bottom);

	CFont* font = hFontHandle ? CFont::FromHandle(hFontHandle) : GetFont();
	SetFont(font);

	if (bEditable)
	{
		wndEdit.bMouseSelect = bMouseSelect;
		wndEdit.Create(WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL, btrc, this, GetDlgCtrlID() + 1);
		wndEdit.SetFont(font);
		wndEdit.m_dwData = m_dwData;
		wndEdit.SetWindowText(m_sInitText);

				
		CHARFORMAT chf;
		chf.cbSize = sizeof(CHARFORMAT);
		chf.crTextColor = crColor;
		chf.dwMask = CFM_COLOR;

		CHARRANGE cr;
		wndEdit.GetSel(cr);

		wndEdit.SetSel(0,-1);
		wndEdit.SetDefaultCharFormat(chf);
		wndEdit.SetSelectionCharFormat(chf);

		wndEdit.SetSel(cr);
	}
	else
	{
		wndStatic.Create(m_sInitText, WS_VISIBLE|WS_CHILD, btrc, this, GetDlgCtrlID() + 1);
		wndStatic.SetFont(font);

		wndStatic.crColor = crColor;
	}
	iCurSel = FindStringExact(-1, m_sInitText);
	SetFocus();
	wndList.Create(WS_BORDER|WS_CHILD|WS_VSCROLL|LVS_SHOWSELALWAYS|LVS_SINGLESEL|LVS_REPORT|LVS_NOCOLUMNHEADER, CRect( 0, 0, 0, 0 ), GetParent(), GetDlgCtrlID() + 2);
	wndList.SetExtendedStyle( LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
	wndList.SetTextColor( RGB( 0, 0, 192 ) );
	return 0;
}

void CGfxInCombo::OnPaint() 
{
	CPaintDC dc(this);

	CRect rc;
	GetClientRect(rc);
	CRect btrc(rc.right - iButtonDx, rc.top, rc.right, rc.bottom);
	DrawButton(&dc, wndList.GetSafeHwnd() && wndList.IsWindowVisible());
}

void CGfxInCombo::DrawButton(CDC * pDC, const bool bPressed)
{
	CRect rc;
	GetClientRect(rc);
	CRect btrc(rc.right - iButtonDx, rc.top, rc.right, rc.bottom);
	pDC->FillSolidRect(btrc, GetSysColor(COLOR_3DFACE));

	CBitmap * pBmp = CBitmap::FromHandle(hArrow);

	CDC dcImage;
	if (dcImage.CreateCompatibleDC(pDC))
	{ 
		BITMAP bm;
		pBmp->GetBitmap(&bm);

		int x = btrc.left + (btrc.Width() - bm.bmWidth) / 2;
		int y = btrc.top + (btrc.Height() - bm.bmHeight) / 2;
		CBitmap* pOldBitmap = dcImage.SelectObject(pBmp);
		pDC->BitBlt(x, y, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
		dcImage.SelectObject(pOldBitmap);
	}

	if (bPressed) 
	{
		pDC->Draw3dRect(btrc, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
		if (!bLightBorder)
		{
			btrc.InflateRect(-1,-1);
			pDC->Draw3dRect(btrc, RGB(0,0,0), GetSysColor(COLOR_3DFACE));
		}
	}
	else 
	{
		if (!bLightBorder)
		{
			pDC->Draw3dRect(btrc, GetSysColor(COLOR_3DFACE),RGB(0,0,0));
			btrc.InflateRect(-1,-1);
		}
		pDC->Draw3dRect(btrc, GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
	}

}

void CGfxInCombo::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);
	if (wndStatic.GetSafeHwnd()) wndStatic.SetWindowPos(0,0,0,cx - iButtonDx, cy, SWP_NOZORDER);
	if (wndEdit.GetSafeHwnd())   wndEdit.SetWindowPos(0,0,0,cx - iButtonDx, cy, SWP_NOZORDER);
}

bool CGfxInCombo::Create(DWORD dwFlag, CRect rc, CWnd * pParent, int id)
{
	return CWnd::Create(NULL, "", dwFlag|WS_CHILD, rc, pParent, id) ? true : false;
}

void CGfxInCombo::OnKillFocus(CWnd* pNewWnd) 
{
	CWnd::OnKillFocus(pNewWnd);
	if (m_bESC)
	{
		//PostMessage(WM_CLOSE);
		DestroyWindow();
		return;
	}
	
	if (!IsChild(pNewWnd) && pNewWnd->GetSafeHwnd() != wndList.GetSafeHwnd()) 
	{
		ProcessSelect();
		DestroyWindow();
		//PostMessage(WM_CLOSE);
	}
}

void CGfxInCombo::OnNcDestroy() 
{
	CWnd::OnNcDestroy();
//	delete this;	
}

BOOL CGfxInCombo::OnEraseBkgnd(CDC* pDC) 
{
	CRect rc;
	GetClientRect(rc);
	pDC->FillSolidRect(rc, GetSysColor(COLOR_WINDOW));
	return true;	
}

void CGfxInCombo::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CRect rc, rc1;
	GetClientRect(rc);
	CFont* font = GetParent()->GetFont(); 
	{
		CClientDC dc(this);
		CFont * of = dc.SelectObject(font);
		CSize sz = dc.GetTextExtent("X");
		sz.cy += 3;
		dc.SelectObject(of);
		int icy = pArray ? sz.cy * (pArray->GetSize()+1) : 100;
		if (icy > 100) icy = 100;
		if (icy == 0) icy = 20;
		rc1.SetRect(rc.left, rc.bottom, rc.right, rc.bottom + icy);
	}

	rc.left = rc.right - iButtonDx;
	if (pArray && pArray->GetSize() > 0 && rc.PtInRect(point))
	{
		//if (wndList.GetSafeHwnd()) wndList.PostMessage(WM_CLOSE);//wndList.DestroyWindow();
		//else
		if (wndList.GetSafeHwnd())
		{
			ClientToScreen(rc1);
			GetParent()->ScreenToClient(rc1);

			//wndList.Create(WS_BORDER|WS_VISIBLE|WS_CHILD|WS_VSCROLL|LBS_SORT, rc1, GetParent(), GetDlgCtrlID() + 2);
			//wndList.Create(WS_BORDER|WS_VISIBLE|WS_CHILD|WS_VSCROLL|LVS_SHOWSELALWAYS|LVS_SINGLESEL|LVS_REPORT|LVS_NOCOLUMNHEADER, rc1, GetParent(), GetDlgCtrlID() + 2);
			/*if (pArray)
			{
				for (int t = 0; t < pArray->GetSize(); t++)
					wndList.AddString((*pArray)[t]);
			}*/
			//int icy = wndList.GetItemHeight(0) * pArray->GetSize();
			int nWidth = 0;
			int count = wndList.GetHeaderCtrl()->GetItemCount();
			for( int i = 0; i < count; i ++ ) nWidth += wndList.GetColumnWidth( i );
			if( nWidth < rc1.Width() ) nWidth = rc1.Width();
			wndList.ShowWindow( SW_SHOW );
			int icy = 15 * wndList.GetItemCount();
			wndList.SetWindowPos(&wndTopMost, rc1.left,rc1.top,nWidth + 17/*rc1.Width()*/,/*icy > 100 ? 100 : icy*/150, SWP_NOZORDER);
			wndList.SetFont(font);

			CString str;
			
			if (bEditable) wndEdit.GetWindowText(str);
			else wndStatic.GetWindowText(str);
			
			//wndList.SetCurSel(wndList.FindStringExact(-1, str));
			LVFINDINFO info;
			int nIndex;
			count = wndList.GetItemCount();
			CRect rcItem;
			
			char szTmp[ 255 ];
			memset( szTmp, 0, 255 );
			info.flags = LVFI_PARTIAL|LVFI_STRING;
			strcpy( szTmp, str.Left( str.Find( " " ) ) );
			info.psz = szTmp;
			nIndex = wndList.FindItem( &info );
			if( nIndex >= 0 )
			{
				wndList.SetItemState( nIndex, LVIS_FOCUSED | LVIS_SELECTED, 0xFFFF );
				wndList.GetItemRect( 0, &rcItem, LVIR_BOUNDS );
				wndList.Scroll( CSize( 0, -count * rcItem.Height() ) );
				wndList.Scroll( CSize( 0, nIndex * rcItem.Height() ) );
			}
			str.ReleaseBuffer();
			wndList.SetFocus();
		}
		InvalidateButton();
	}
	else
	{
		//if (wndList.GetSafeHwnd()) wndList.DestroyWindow();
		wndList.ShowWindow( SW_HIDE );
		InvalidateButton();
	}
	CWnd::OnLButtonDown(nFlags, point);
}

void CGfxInCombo::InvalidateButton()
{
	CRect rc;
	GetClientRect(rc);
	CRect btrc(rc.right - iButtonDx, rc.top, rc.right, rc.bottom);
	InvalidateRect(btrc);
}

BOOL CGfxInCombo::PreTranslateMessage(MSG* pMsg) 
{
	CWnd * pParent = GetParent();
	ASSERT(pParent);
	if (pMsg->message == WM_NCHITTEST)
	{
		int x = LOWORD(pMsg->lParam);
		int y = HIWORD(pMsg->lParam);
		CPoint pt(x,y);
		CWnd *pw = WindowFromPoint(pt);
		if (pw && !IsChild(pw) && pw->GetSafeHwnd() != wndList.GetSafeHwnd()) 
		{
			//PostMessage(WM_CLOSE);
			DestroyWindow();
			return TRUE;

⌨️ 快捷键说明

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