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

📄 qcombobox.cpp

📁 Visual C++下的界面设计
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/************************************
  REVISION LOG ENTRY
  Revision By: Mihai Filimon
  Revised on 9/16/98 2:20:27 PM
  Comments: QComboBox.cpp : implementation file
 ************************************/

#include "stdafx.h"
#include "QComboBox.h"
#include <math.h>
#include <afxpriv.h>

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

/////////////////////////////////////////////////////////////////////////////
// CQComboBox

#define NotifyWindow(pWnd,message,wParam,lParam) if (pWnd) pWnd->SendMessage(message, (WPARAM)wParam, (LPARAM)lParam)

CBrush CQComboBox::m_brBkGnd(defaultRGBBkGnd);
CFont CQComboBox::m_font;
CMap<CQComboBox*,CQComboBox*, BOOL, BOOL> CQComboBox::m_mapUnloadedQCombos;

static const LOGFONT logFontPages =
{
/*LONG lfHeight*/8,
/*LONG lfWidth*/0,
/*LONG lfEscapement*/0,
/*LONG lfOrientation*/0,
/*LONG lfWeight*/FW_NORMAL,
/*BYTE lfItalic*/FALSE,
/*BYTE lfUnderline*/FALSE,
/*BYTE lfStrikeOut*/FALSE,
/*BYTE lfCharSet*/ANSI_CHARSET,
/*BYTE lfOutPrecision*/0,
/*BYTE lfClipPrecision*/0,
/*BYTE lfQuality*/DEFAULT_QUALITY,
/*BYTE lfPitchAndFamily*/DEFAULT_PITCH,
/*CHAR lfFaceName[LF_FACESIZE]*/_T("MS Sans Serif")
};

// Function name	: CQComboBox::CQComboBox
// Description	    : default constuctor
// Return type		: 
CQComboBox::CQComboBox(TLine fctLine, TLinePartial fctLinePartial, LPARAM lParam)
{
	m_lParam = lParam;
	m_QuickLoader.SetParent(this);
	RegClassQComboBox();
	m_pListBox = NULL;
	m_pEdit = NULL;
	m_bCaptured = FALSE;
	m_fctLine = fctLine;
	m_fctLinePartial = fctLinePartial;
	m_nCountVisible = 0;
	SetRateWidth(0.0);
	SetMultipleHeight();
	m_fctLoadFunction = LoadPartialListBox;
	m_mapUnloadedQCombos[this] = !IsAlreadyLoad();

	//When you callthe contructor, you need to attach two function.
	//The first, for giving line number nIndex
	//The second, for fiving numer of line which start with a given string.
	// If you don't write that functions, especialy m_fctLine, you will have some suprises
	ASSERT(m_fctLine);
}

// Function name	: CQComboBox::~CQComboBox
// Description	    : virtual destructor
// Return type		: 
CQComboBox::~CQComboBox()
{
	m_mapUnloadedQCombos.RemoveKey(this);
}

BEGIN_MESSAGE_MAP(CQComboBox, CWnd)
	//{{AFX_MSG_MAP(CQComboBox)
	ON_WM_DESTROY()
	ON_WM_LBUTTONDOWN()
	ON_WM_WINDOWPOSCHANGED()
	ON_WM_PAINT()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_SETFOCUS()
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQComboBox message handlers
UINT CQComboBox::m_nSelChange = NULL;
UINT CQComboBox::m_nLoading = NULL;
UINT CQComboBox::m_nLoaded = NULL;

// Function name	: CQComboBox::RegClassQComboBox
// Description	    : Register this window class
// Return type		: BOOL 
BOOL CQComboBox::RegClassQComboBox()
{
	WNDCLASS wndClass;
		wndClass.style = CS_DBLCLKS;
		wndClass.lpfnWndProc = ::DefWindowProc;
		wndClass.cbClsExtra = NULL;
		wndClass.cbWndExtra = NULL;
		wndClass.hInstance = AfxGetInstanceHandle();
		wndClass.hIcon = NULL;
		wndClass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
		wndClass.hbrBackground = (HBRUSH)m_brBkGnd;
		wndClass.lpszMenuName = NULL;
		wndClass.lpszClassName = wndClassName;
	BOOL bResult = AfxRegisterClass(&wndClass);
	if (bResult)
	{
		if (!m_nSelChange)
			m_nSelChange = RegisterWindowMessage(defaultSelChange);
		if (!m_nLoading)
			m_nLoading = RegisterWindowMessage(defaultLoading);
		if (!m_nLoaded)
			m_nLoaded = RegisterWindowMessage(defaultLoaded);
		if (!m_font.GetSafeHandle())
		{
			//At the first call set the new font
			m_font.CreateFontIndirect(&logFontPages);
		}
	}
	return bResult;
}

CQComboBox* CQComboBox::m_pActiveMCBox = NULL;
CQComboBox::CWindowProcs CQComboBox::m_wndProcs;
// Function name	: CQComboBox::ListBoxWindowProc
// Description	    : ListControl window procedure
// Return type		: LRESULT CALLBACK 
// Argument         : HWND hWnd
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CALLBACK CQComboBox::ListBoxWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	if (CQComboBox* pOwner = CQComboBox::m_pActiveMCBox)
		if (!pOwner->ForwardMessage(nMsg, wParam, lParam))
			return NULL;
	return CallWindowProc( CQComboBox::m_wndProcs.GetOldListBoxProcedure(hWnd), hWnd, nMsg, wParam, lParam );
}

// Function name	: CQComboBox::ParentWindowProc
// Description	    : Parent window procedure.
// Return type		: LRESULT CALLBACK 
// Argument         : HWND hWnd
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CALLBACK CQComboBox::ParentWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	CQComboBox* pOwner = CQComboBox::m_pActiveMCBox;
	CQComboBox* pFirst = GetFirstQComboBox();

	{
		switch (nMsg)
		{
			case WM_KICKIDLE:
			{
				//Start to load items, after 3 seconds.
				if (pFirst)
					pFirst->SetTimer(QIDTIMERSTARTLOADITEMS, QTIMESTARTLOADITEMS, NULL);
				break;
			}
			case WM_COMMAND:
			case WM_SYSCOMMAND:
			case WM_LBUTTONDOWN:
			case WM_NCLBUTTONDOWN:
			{
				if (pFirst)
				{
					pFirst->KillTimer(QIDTIMERSTARTLOADITEMS);
					pFirst->KillTimer(QIDTIMERLOADITEMS);
				};

				if (pOwner)
				{
					BOOL bDropped = pOwner->IsDropedDown();
					pOwner->DropDown(FALSE);
					if (nMsg == WM_COMMAND)
						if (LOWORD(wParam) == IDOK)
						{
							pOwner->SelectCurrentItem();
							return FALSE;
						}
						else
							if (LOWORD(wParam) == IDCANCEL)
								if (bDropped)
									return FALSE;
					break;
				}
			}
		}
	};
	WNDPROC wndProc = CQComboBox::m_wndProcs.GetOldParentProcedure(hWnd);
	ASSERT (wndProc);
	return CallWindowProc( wndProc, hWnd, nMsg, wParam, lParam );
}

// Function name	: CQComboBox::EditWindowProc
// Description	    : Edit window procedure
// Return type		: LRESULT CALLBACK 
// Argument         : HWND hWnd
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CALLBACK CQComboBox::EditWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	CQComboBox* pOwner = CQComboBox::m_pActiveMCBox;
	switch (nMsg)
	{
		case WM_SETFOCUS:
			{
				CQComboBox::m_pActiveMCBox = (CQComboBox*)CWnd::FromHandle(::GetParent(hWnd));
				break;
			}
		case WM_KILLFOCUS:
			{
				if (pOwner)
				{
					pOwner->DropDown(FALSE);
					CQComboBox::m_pActiveMCBox = NULL;
				}
				break;
			}
		case WM_KEYDOWN:
		case WM_SYSKEYDOWN:
			{
				int nStep = 0;
				if (pOwner)
					switch ((int) wParam)
					{
						case VK_NEXT:
							{
								if (nStep == 0)
									nStep = +pOwner->GetVisibleCount();
							}
						case VK_PRIOR:
							{
								if (nStep == 0)
									nStep = -pOwner->GetVisibleCount();
							}
						case VK_UP:
							{
								if (nStep == 0)
									nStep = -1;
							}
						case VK_DOWN:
							{
								if (nStep == 0)
									nStep = +1;
								if (abs(nStep) > 1 || ((abs(nStep) == 1) && GetAsyncKeyState(VK_MENU) >= 0))
								{
									pOwner->LoadPartial(pOwner->m_QuickLoader.GetItemLine(pOwner->GetCurrentItem()) + nStep, nStep/abs(nStep) * pOwner->GetVisibleCount());
									pOwner->SetCurrentItem(pOwner->GetCurrentItem() + nStep);
									pOwner->SelectCurrentItem();
									break;
								}
							}
						case defaultDropDownKey:
							{
								pOwner->DropDown(!pOwner->IsDropedDown());
								break;
							}
					}
				break;
			}
	}
	return CallWindowProc( CQComboBox::m_wndProcs.GetOldEditProcedure(hWnd), hWnd, nMsg, wParam, lParam );
}

// Function name	: CQComboBox::OnInit
// Description	    : Init the control
// Return type		: BOOL 
BOOL CQComboBox::OnInit()
{
	// You must already attach a function who give me a line.
	ASSERT (m_fctLine != NULL);
	ASSERT (m_font.GetSafeHandle());
	SetFont(&m_font);
	ModifyStyle(WS_OVERLAPPED , WS_TABSTOP);
	m_pEdit		= new CEdit();
	m_pListBox = new CListBox();
	if (m_pEdit->Create(WS_CHILD | WS_VISIBLE | defaultEditStyle, CRect(0,0,0,0), this, IDEDIT ))
	{
		ModifyStyleEx(0, WS_EX_STATICEDGE);
		m_pEdit->SetFont(&m_font);
		if (m_pListBox->Create(WS_BORDER | WS_CHILD | defaultListBoxStyle , CRect(0,0,0,0), GetDesktopWindow(), IDListBox))
		{
			m_pListBox->SetFont(&m_font);
			//Set the refernce to this object in user data dword
			::SetWindowLong(m_pListBox->m_hWnd, GWL_USERDATA, (long)this);
			::SetWindowLong(m_pListBox->m_hWnd, GWL_STYLE, GetWindowLong(m_pListBox->m_hWnd, GWL_STYLE) | WS_CLIPSIBLINGS | WS_OVERLAPPED);
			m_pListBox->ModifyStyleEx(0, WS_EX_TOOLWINDOW);
			m_pListBox->SetWindowPos(&CWnd::wndTopMost,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
			ListView_SetExtendedListViewStyle(m_pListBox->m_hWnd, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
			Resize();
			m_wndProcs.AddEdit(GetEdit(), EditWindowProc);
			m_wndProcs.AddListBox(GetListBox(), ListBoxWindowProc);
			m_wndProcs.AddParent(GetParent(), ParentWindowProc);
			m_nCountVisible = GetVisibleCount();
			return TRUE;
		}
	}
	return FALSE;
}

// Function name	: CQComboBox::PreSubclassWindow
// Description	    : Call to subclass window
// Return type		: void 
void CQComboBox::PreSubclassWindow() 
{
	CWnd::PreSubclassWindow();

	OnInit();
}

// Function name	: CQComboBox::OnDestroy
// Description	    : Remove all dependent datas.
// Return type		: void 
void CQComboBox::OnDestroy() 
{
	CWnd::OnDestroy();

	m_wndProcs.RemoveEdit(GetEdit());
	m_wndProcs.RemoveListBox(GetListBox());
	m_wndProcs.RemoveParent(GetParent());
	
	if (CListBox* pList = GetListBox())
		delete pList;

	if (CEdit* pEdit = GetEdit())
		delete pEdit;

	m_pListBox = NULL;
	m_pEdit = NULL;

}

// Function name	: CQComboBox::GetListBox
// Description	    : return the list control for 
// Return type		: CListBox* 
CListBox* CQComboBox::GetListBox()
{
	return m_pListBox;
}

// Function name	: CQComboBox::GetEdit
// Description	    : retirn  pointer to edit control inside of thsi control
// Return type		: CEdit* 
CEdit* CQComboBox::GetEdit()
{
	return m_pEdit;
}

// Function name	: CQComboBox::DrawButton
// Description	    : Draw down button
// Return type		: void 
// Argument         : CDC * pDC
// Argument         : CRect r
// Argument         : BOOL bDown
void CQComboBox::DrawButton(CDC * pDC, CRect r, BOOL bDown)
{
	CPen penWhite(PS_SOLID,1,RGB(255,255,255));
	CPen penBlack(PS_SOLID,1,RGB(0,0,0));
	pDC->FrameRect(r,&CBrush(RGB(128,128,128)));
	if (!bDown)
	{
		pDC->SelectObject(&penWhite);

⌨️ 快捷键说明

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