[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

Need some help with ComboBox

Asked by tsp2002 in Windows MFC Programming

The code comes from http://www.codeguru.com/cpp/controls/combobox/multicolumncombos/article.php/c1819/
Hello,
one last thing....how can I change the color of the bar from grey (see hardcopy) to blue.
Because of this CustomControl I don“t know how to change the color.
Please help.
500 points.
Best regards,
Thomas
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
667:
668:
669:
670:
671:
672:
673:
674:
675:
676:
677:
678:
679:
680:
681:
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
693:
694:
695:
696:
697:
698:
699:
700:
701:
702:
703:
704:
705:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716:
717:
718:
719:
720:
721:
722:
723:
724:
725:
726:
727:
728:
729:
730:
731:
732:
733:
734:
735:
736:
737:
738:
739:
740:
741:
742:
743:
744:
745:
746:
747:
748:
749:
750:
751:
752:
753:
754:
755:
756:
757:
758:
759:
760:
761:
762:
763:
764:
765:
766:
767:
768:
769:
770:
771:
772:
773:
774:
775:
776:
777:
778:
779:
780:
781:
782:
783:
784:
785:
786:
787:
788:
789:
790:
791:
792:
793:
794:
795:
796:
797:
798:
799:
800:
801:
802:
803:
804:
805:
806:
807:
808:
809:
810:
811:
812:
813:
814:
815:
816:
817:
818:
819:
820:
821:
822:
823:
824:
825:
826:
827:
828:
829:
830:
831:
832:
833:
834:
835:
836:
837:
838:
839:
840:
841:
842:
843:
844:
845:
846:
847:
848:
849:
850:
851:
852:
853:
854:
855:
856:
857:
858:
859:
860:
861:
862:
863:
864:
865:
866:
867:
868:
869:
870:
871:
872:
873:
874:
875:
876:
877:
878:
879:
880:
881:
882:
883:
884:
885:
886:
887:
888:
889:
890:
891:
892:
893:
894:
895:
896:
897:
898:
899:
900:
901:
902:
903:
904:
905:
906:
907:
908:
909:
910:
911:
912:
913:
914:
915:
916:
917:
918:
919:
920:
921:
922:
923:
924:
925:
926:
927:
928:
929:
930:
931:
932:
933:
/************************************
  REVISION LOG ENTRY
  Revision By: Mihai Filimon
  Revised on 9/16/98 2:20:27 PM
  Comments: MultiColumnComboBox.cpp : implementation file
 ************************************/
 
#include "stdafx.h"
#include "MultiColumnComboBox.h"
#include <math.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnComboBox
 
CBrush CMultiColumnComboBox::m_brBkGnd(defaultRGBBkGnd);
CFont CMultiColumnComboBox::m_font;
 
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	: CMultiColumnComboBox::CMultiColumnComboBox
// Description	    : default constuctor
// Return type		: 
CMultiColumnComboBox::CMultiColumnComboBox(int nColumnKey)
{
	RegClassMultiColumnComboBox();
	m_pListCtrl = NULL;
	m_pEdit = NULL;
	m_bCaptured = FALSE;
	m_nColumnKey = nColumnKey;
	SetRateWidth(0.0);
	SetMultipleHeight();
	m_bFirstShow = TRUE; 
}
 
// Function name	: CMultiColumnComboBox::~CMultiColumnComboBox
// Description	    : virtual destructor
// Return type		: 
CMultiColumnComboBox::~CMultiColumnComboBox()
{
}
 
BEGIN_MESSAGE_MAP(CMultiColumnComboBox, CWnd)
	//{{AFX_MSG_MAP(CMultiColumnComboBox)
	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_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnComboBox message handlers
UINT CMultiColumnComboBox::m_nSelChange = NULL;
 
// Function name	: CMultiColumnComboBox::RegClassMultiColumnComboBox
// Description	    : Register this window class
// Return type		: BOOL 
BOOL CMultiColumnComboBox::RegClassMultiColumnComboBox()
{
	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_font.GetSafeHandle())
		{
			//At the first call set the new font
			m_font.CreateFontIndirect(&logFontPages);
		}
	}
	return bResult;
}
 
CMultiColumnComboBox* CMultiColumnComboBox::m_pActiveMCBox = NULL;
CMultiColumnComboBox::CWindowProcs CMultiColumnComboBox::m_wndProcs;
// Function name	: CMultiColumnComboBox::ListCtrlWindowProc
// Description	    : ListControl window procedure
// Return type		: LRESULT CALLBACK 
// Argument         : HWND hWnd
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CALLBACK CMultiColumnComboBox::ListCtrlWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	CMultiColumnComboBox* pOwner  = NULL;
	if (pOwner = CMultiColumnComboBox::m_pActiveMCBox)
		pOwner->ForwardMessage(nMsg, wParam, lParam);
	LRESULT lResult = CallWindowProc( CMultiColumnComboBox::m_wndProcs.GetOldListCtrlProcedure(hWnd), hWnd, nMsg, wParam, lParam );
	return lResult;
}
 
// Function name	: CMultiColumnComboBox::ParentWindowProc
// Description	    : Parent window procedure.
// Return type		: LRESULT CALLBACK 
// Argument         : HWND hWnd
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CALLBACK CMultiColumnComboBox::ParentWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	if (CMultiColumnComboBox* pOwner = CMultiColumnComboBox::m_pActiveMCBox)
	{
		switch (nMsg)
		{
			case WM_COMMAND:
			case WM_CAPTURECHANGED:
			case WM_SYSCOMMAND:
			case WM_LBUTTONDOWN:
			case WM_NCLBUTTONDOWN:
			{
				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 = CMultiColumnComboBox::m_wndProcs.GetOldParentProcedure(hWnd);
	ASSERT (wndProc);
	return CallWindowProc( wndProc, hWnd, nMsg, wParam, lParam );
}
 
// Function name	: CMultiColumnComboBox::EditWindowProc
// Description	    : Edit window procedure
// Return type		: LRESULT CALLBACK 
// Argument         : HWND hWnd
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CALLBACK CMultiColumnComboBox::EditWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	CMultiColumnComboBox* pOwner = CMultiColumnComboBox::m_pActiveMCBox;
	switch (nMsg)
	{
		case WM_SETFOCUS:
			{
				CMultiColumnComboBox::m_pActiveMCBox = (CMultiColumnComboBox*)CWnd::FromHandle(::GetParent(hWnd));
				break;
			}
		case WM_KILLFOCUS:
			{
				if (pOwner)
				{
					pOwner->DropDown(FALSE);
					CMultiColumnComboBox::m_pActiveMCBox = NULL;
				}
				break;
			}
		case WM_KEYDOWN:
		case WM_SYSKEYDOWN:
			{
				if (pOwner)
					switch ((int) wParam)
					{
						case VK_UP:
						case VK_DOWN:
							{
								if (GetAsyncKeyState(VK_MENU) >= 0)
								{
									pOwner->SetCurrentItem(pOwner->GetCurrentItem() + ((int) wParam == VK_UP ? -1 : +1));
									pOwner->SelectCurrentItem();
									break;
								}
							}
						case defaultDropDownKey:
							{
								pOwner->DropDown(!pOwner->IsDropedDown());
								pOwner->CaptureListCtrl();
								break;
							}
					}
				break;
			}
	}
	return CallWindowProc( CMultiColumnComboBox::m_wndProcs.GetOldEditProcedure(hWnd), hWnd, nMsg, wParam, lParam );
}
 
// Function name	: CMultiColumnComboBox::OnInit
// Description	    : Init the control
// Return type		: BOOL 
BOOL CMultiColumnComboBox::OnInit()
{
	//Do not call twice
	ASSERT (m_pEdit == NULL && m_pListCtrl == NULL);
	ASSERT (m_font.GetSafeHandle());
	SetFont(&m_font);
	ModifyStyle(WS_OVERLAPPED , WS_TABSTOP);
	m_pEdit		= new CEdit();
	m_pListCtrl = new CListCtrl();
	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_pListCtrl->Create(WS_BORDER | WS_CHILD | defaultListCtrlStyle , CRect(0,0,0,0), GetDesktopWindow(), IDLISTCTRL))
		{
			//Set the reference to this object in user data dword
			::SetWindowLong(m_pListCtrl->m_hWnd, GWL_USERDATA, (long)this);
			::SetWindowLong(m_pListCtrl->m_hWnd, GWL_STYLE, GetWindowLong(m_pListCtrl->m_hWnd, GWL_STYLE) | WS_CLIPSIBLINGS | WS_OVERLAPPED);
			m_pListCtrl->ModifyStyleEx(0, WS_EX_TOOLWINDOW);
			m_pListCtrl->SetWindowPos(&CWnd::wndTopMost,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
			ListView_SetExtendedListViewStyle(m_pListCtrl->m_hWnd, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
			Resize();
			m_wndProcs.AddEdit(GetEdit(), EditWindowProc);
			m_wndProcs.AddListCtrl(GetListCtrl(), ListCtrlWindowProc);
			m_wndProcs.AddParent(GetParent(), ParentWindowProc);
			return TRUE;
		}
	}
	return FALSE;
}
 
// Function name	: CMultiColumnComboBox::PreSubclassWindow
// Description	    : Call to subclass window
// Return type		: void 
void CMultiColumnComboBox::PreSubclassWindow() 
{
	CWnd::PreSubclassWindow();
 
	// If this is dynamical created then do not call OnInit
	if (AfxGetThreadState()->m_pWndInit == 0)
		OnInit();
}
 
// Function name	: CMultiColumnComboBox::OnDestroy
// Description	    : Remove all dependent datas.
// Return type		: void 
void CMultiColumnComboBox::OnDestroy() 
{
	CWnd::OnDestroy();
 
	m_wndProcs.RemoveEdit(GetEdit());
	m_wndProcs.RemoveListCtrl(GetListCtrl());
	m_wndProcs.RemoveParent(GetParent());
	
	if (CListCtrl* pList = GetListCtrl())
		delete pList;
 
	if (CEdit* pEdit = GetEdit())
		delete pEdit;
 
	m_pListCtrl = NULL;
	m_pEdit = NULL;
 
}
 
// Function name	: CMultiColumnComboBox::GetListCtrl
// Description	    : return the list control for 
// Return type		: CListCtrl* 
CListCtrl* CMultiColumnComboBox::GetListCtrl()
{
	return m_pListCtrl;
}
 
// Function name	: CMultiColumnComboBox::GetEdit
// Description	    : retirn  pointer to edit control inside of thsi control
// Return type		: CEdit* 
CEdit* CMultiColumnComboBox::GetEdit()
{
	return m_pEdit;
}
 
// Function name	: CMultiColumnComboBox::DrawButton
// Description	    : Draw down button
// Return type		: void 
// Argument         : CDC * pDC
// Argument         : CRect r
// Argument         : BOOL bDown
void CMultiColumnComboBox::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);
		 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 = (r.Width() - b.bmWidth) / 2;
			int topC = (r.Height() - b.bmHeight) / 2;
			if (bDown)
			{
				leftC++;
				topC++;
			}
			CBitmap* pOldBitmap = dcMem.SelectObject(&bitmapOEM);
			pDC->BitBlt(r.left + leftC, r.top + topC, r.Width(), r.Height(), &dcMem, 0,0, SRCCOPY);
			pOldBitmap = dcMem.SelectObject(pOldBitmap);
		}
	}
}
 
// Function name	: CMultiColumnComboBox::OnPaint
// Description	    : On Draw function
// Return type		: void 
void CMultiColumnComboBox::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	: CMultiColumnComboBox::Resize
// Description	    : Call to remove the edit and list controls
// Return type		: void 
void CMultiColumnComboBox::Resize()
{
	ASSERT (GetListCtrl());
	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+100;
	r.right += int(m_dWidthList * r.Width());
	GetListCtrl()->MoveWindow(r.left, r.top, r.Width(), r.Height());
}
 
// Function name	: CMultiColumnComboBox::DropDown
// Description	    : DropDown
// Return type		: void 
// Argument         : BOOL bDown
void CMultiColumnComboBox::DropDown(BOOL bDown)
{
	if (IsWindowVisible())
	{
		if (IsDropedDown() != bDown)
		{
			Resize();
			GetListCtrl()->ShowWindow(bDown ? SW_SHOW : SW_HIDE);
			if (bDown)
				GetEdit()->SetFocus();
			else
				ReleaseCapture();
		}
	}
}
 
// Function name	: CMultiColumnComboBox::IsDropedDown
// Description	    : If the list control is dropped downd, return TRUE
// Return type		: BOOL 
BOOL CMultiColumnComboBox::IsDropedDown()
{
	return GetListCtrl()->IsWindowVisible();
}
 
// Function name	: CMultiColumnComboBox::OnWindowPosChanged
// Description	    : 
// Return type		: void 
// Argument         : WINDOWPOS FAR* lpwndpos
void CMultiColumnComboBox::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
{
	CWnd::OnWindowPosChanged(lpwndpos);
	Resize();
}
 
// Function name	: CMultiColumnComboBox::OnLButtonDown
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CMultiColumnComboBox::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (m_rectBtn.PtInRect(point))
	{
		SetButton();
		DropDown(!IsDropedDown());
	}
	
	CWnd::OnLButtonDown(nFlags, point);
}
 
// Function name	: CMultiColumnComboBox::OnLButtonUp
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CMultiColumnComboBox::OnLButtonUp(UINT nFlags, CPoint point) 
{
	ReleaseButton();
	CaptureListCtrl();
	CWnd::OnLButtonUp(nFlags, point);
}
 
// Function name	: CMultiColumnComboBox::OnMouseMove
// Description	    : 
// Return type		: void 
// Argument         : UINT nFlags
// Argument         : CPoint point
void CMultiColumnComboBox::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	: CMultiColumnComboBox::ReleaseButton
// Description	    : Call to release the capture and image of button. After SetButton()
// Return type		: void 
void CMultiColumnComboBox::ReleaseButton()
{
	if (m_bCaptured)
	{
		ReleaseCapture();
		CDC* pDC = GetDC();
			DrawButton(pDC, m_rectBtn);
		ReleaseDC(pDC);
		m_bCaptured = FALSE;
		GetListCtrl()->SetCapture();
	}
}
 
// Function name	: CMultiColumnComboBox::SetButton
// Description	    : 
// Return type		: void 
void CMultiColumnComboBox::SetButton()
{
	if (!m_bCaptured)
	{
		SetCapture();
		CDC* pDC = GetDC();
			DrawButton(pDC, m_rectBtn, TRUE);
		ReleaseDC(pDC);
		m_bCaptured = TRUE;
	}
}
 
// Function name	: CMultiColumnComboBox::ForwardMessage
// Description	    : This function is called by ListCtrlWindowProc
// Return type		: void 
// Argument         : UINT nMsg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
void CMultiColumnComboBox::ForwardMessage(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	ASSERT (GetListCtrl());
	switch (nMsg)
	{
		case WM_MOUSEMOVE:
		case WM_NCMOUSEMOVE:
		{
			::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
			GetListCtrl()->SetCapture();
		}
		case WM_LBUTTONDOWN:
		{
			CPoint point(LOWORD(lParam), HIWORD(lParam));
			CRect rectClient; GetListCtrl()->GetClientRect(rectClient);
			CRect rectWindow; GetListCtrl()->GetWindowRect(rectWindow);
			CPoint pointScreen(point);
			GetListCtrl()->ClientToScreen(&pointScreen);
			LPARAM lPoint = MAKELPARAM(pointScreen.x, pointScreen.y);
			UINT ht = GetListCtrl()->SendMessage(WM_NCHITTEST,0,lPoint);
			if (ht == HTCLIENT)
			{
				int nIndex = GetListCtrl()->HitTest(CPoint(LOWORD(lParam), HIWORD(lParam)));
				if (GetCurrentItem() != nIndex)
					SetCurrentItem(nIndex);
			}
			else 
				if (rectWindow.PtInRect(pointScreen))
					if (nMsg == WM_LBUTTONDOWN)
					{
						ReleaseCapture();
						GetListCtrl()->SendMessage(WM_NCLBUTTONDOWN, ht, lPoint);
						break;
					};
			if (nMsg == WM_LBUTTONDOWN)
			{
				DropDown(FALSE);
				SelectCurrentItem();
			}
			break;
		}
	}
}
 
// Function name	: CMultiColumnComboBox::OnSetFocus
// Description	    : When control have focus then edit will take the focus
// Return type		: void 
// Argument         : CWnd* pOldWnd
void CMultiColumnComboBox::OnSetFocus(CWnd* pOldWnd) 
{
	CWnd::OnSetFocus(pOldWnd);
	
	GetEdit()->SetFocus();
 
}
 
// Function name	: CMultiColumnComboBox::OnCommand
// Description	    : When something is happen in edit control, notify the list control
// Return type		: BOOL 
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
BOOL CMultiColumnComboBox::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	: CMultiColumnComboBox::Search
// Description	    : Look for the lpszFindItem
// Return type		: void 
// Argument         : LPCTSTR lpszFindItem
void CMultiColumnComboBox::Search(LPCTSTR lpszFindItem)
{
	if (CListCtrl* pListCtrl = GetListCtrl())
	{
		LV_FINDINFO fInfo;
		 fInfo.flags = LVFI_PARTIAL | LVFI_STRING;
		 fInfo.psz = lpszFindItem;
		int nItem = pListCtrl->FindItem(&fInfo);
		SetCurrentItem(nItem);
		pListCtrl->EnsureVisible(nItem, FALSE); 
	}
}
 
// Function name	: CMultiColumnComboBox::SelectCurrentItem
// Description	    : Select the current item of list. Called if user click the mouse, oor press ENTER
// Return type		: void 
void CMultiColumnComboBox::SelectCurrentItem()
{
	int nIndex = GetCurrentItem();
	GetEdit()->SetWindowText(GetListCtrl()->GetItemText(nIndex, m_nColumnKey));
	//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	: CMultiColumnComboBox::GetCurrentItem
// Description	    : Get current item from list control
// Return type		: int 
int CMultiColumnComboBox::GetCurrentItem()
{
	return GetListCtrl()->GetNextItem(-1, LVNI_SELECTED);
}
 
// Function name	: CMultiColumnComboBox::SetCurrentItem
// Description	    : Set current item from list control to nIndex
// Return type		: void 
// Argument         : int nIndex
void CMultiColumnComboBox::SetCurrentItem(int nIndex)
{
	GetListCtrl()->SetItemState(nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
}
 
// Function name	: CMultiColumnComboBox::OnCreate
// Description	    : Call OnInit if control is created dynamically
// Return type		: int 
// Argument         : LPCREATESTRUCT lpCreateStruct
int CMultiColumnComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	OnInit();
	
	return 0;
}
 
// Function name	: CMultiColumnComboBox::SetRateWidth
// Description	    : This function will changes the width of inside listcontrol
// Return type		: double 
// Argument         : double dWidthList
double CMultiColumnComboBox::SetRateWidth(double dWidthList)
{
	double dResult = m_dWidthList;
	m_dWidthList = fabs(dWidthList);
	return dResult;
}
 
// Function name	: CMultiColumnComboBox::SetMultipleHeight
// Description	    : 
// Return type		: int 
// Argument         : int nMHeight
int CMultiColumnComboBox::SetMultipleHeight(int nMHeight)
{
	int nResult = m_nMultipleHeight;
	m_nMultipleHeight = abs(nMHeight);
	return nResult;
}
 
// Function name	: CMultiColumnComboBox::WindowProc
// Description	    : 
// Return type		: LRESULT 
// Argument         : UINT message
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT CMultiColumnComboBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if (GetEdit())
		switch ( message )
		{
			case WM_SETTEXT:
			case WM_GETTEXT:
			{
				GetEdit()->SendMessage( message, wParam, lParam );
				break;
			}
		}
	
	return CWnd::WindowProc(message, wParam, lParam);
}
 
// Function name	: CMultiColumnComboBox::OnSize
// Description	    : resize combo if needs
// Return type		: void 
// Argument         : UINT nType
// Argument         : int cx
// Argument         : int cy
void CMultiColumnComboBox::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);
	
	Resize();
	
}
 
// Function name	: CMultiColumnComboBox::CaptureListCtrl
// Description	    : Capture listcontrol to know when the drop will be up
// Return type		: void 
void CMultiColumnComboBox::CaptureListCtrl()
{
	if (IsDropedDown())
		GetListCtrl()->SetCapture();
}
####################################################
/************************************
  REVISION LOG ENTRY
  Revision By: Mihai Filimon
  Revised on 9/16/98 2:20:55 PM
  Comments: MultiColumnComboBox.h : header file
 ************************************/
 
#if !defined(AFX_MULTICOLUMNCOMBOBOX_H__1385ADA1_4D4F_11D2_8693_0040055C08D9__INCLUDED_)
#define AFX_MULTICOLUMNCOMBOBOX_H__1385ADA1_4D4F_11D2_8693_0040055C08D9__INCLUDED_
 
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
 
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnComboBox window
 
#include <afxtempl.h>
 
#define defaultRGBBkGnd RGB(192,192,192)
#define wndClassName _T("MultiColumnComboBox")
#define IDLISTCTRL	0x3E8
#define IDEDIT		0x3E9
#define defaultEditStyle ES_AUTOHSCROLL | ES_NOHIDESEL | 0x200
#define defaultListCtrlStyle LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_NOCOLUMNHEADER
#define defaultSizeDY 21
#define defaultSizeDX 14
#define defaultDropDownKey VK_F4
#define defaultSelChange _T("MCCBN_SELCHANGE")
 
class CMultiColumnComboBox : public CWnd
{
// Construction
public:
	static UINT m_nSelChange;
	class CWindowProcs
	{
	public:
		CWindowProcs()
		{
		};
	protected:
		// Store infos about one window, such as window procedure
		struct SWindowProcedure
		{
			CWnd* m_pWnd;
			WNDPROC m_oldWndProcWnd;
			SWindowProcedure(CWnd* pWnd, WNDPROC wndProc)
			{
				if (m_pWnd = pWnd)
					m_oldWndProcWnd = (WNDPROC)::SetWindowLong(m_pWnd->m_hWnd, GWL_WNDPROC, (long)wndProc);
			};
			virtual ~SWindowProcedure()
			{
				if (m_pWnd)
					::SetWindowLong(m_pWnd->m_hWnd, GWL_WNDPROC, (long)m_oldWndProcWnd);
			}
		};
		// Maps with window procedure of all windows controls registered
		CMap<HWND,HWND, SWindowProcedure*, SWindowProcedure*> m_mapWindowProcedureEdit;
		CMap<HWND,HWND, SWindowProcedure*, SWindowProcedure*> m_mapWindowProcedureListCtrl;
		CMap<HWND,HWND, SWindowProcedure*, SWindowProcedure*> m_mapWindowProcedureParent;
	public:
			// Add, Get, Remove Edit elements
			void AddEdit(CWnd* pEdit, WNDPROC wndProc)
			{
				m_mapWindowProcedureEdit[pEdit->m_hWnd] = new SWindowProcedure(pEdit, wndProc);
			};
			SWindowProcedure* GetEditStructure(HWND hWnd)
			{
				SWindowProcedure* pEditSW = NULL;
				m_mapWindowProcedureEdit.Lookup(hWnd, pEditSW);
				return pEditSW;
			};
			WNDPROC GetOldEditProcedure(HWND hWnd)
			{
				return GetEditStructure(hWnd)->m_oldWndProcWnd;
			}
			void RemoveEdit(CWnd* pEdit)
			{
				if (SWindowProcedure* pSW = GetEditStructure(pEdit->m_hWnd))
				{
					delete pSW;
					m_mapWindowProcedureEdit.RemoveKey(pEdit->m_hWnd);
				}
			};
			// Add, Get, Remove ListControl elements
			void AddListCtrl(CWnd* pListCtrl, WNDPROC wndProc)
			{
				m_mapWindowProcedureListCtrl[pListCtrl->m_hWnd] = new SWindowProcedure(pListCtrl, wndProc);
			};
			SWindowProcedure* GetListCtrlStructure(HWND hWnd)
			{
				SWindowProcedure* pListCtrlSW = NULL;
				m_mapWindowProcedureListCtrl.Lookup(hWnd, pListCtrlSW);
				return pListCtrlSW;
			};
			WNDPROC GetOldListCtrlProcedure(HWND hWnd)
			{
				return GetListCtrlStructure(hWnd)->m_oldWndProcWnd;
			}
			void RemoveListCtrl(CWnd* pListCtrl)
			{
				if (SWindowProcedure* pSW = GetListCtrlStructure(pListCtrl->m_hWnd))
				{
					delete pSW;
					m_mapWindowProcedureListCtrl.RemoveKey(pListCtrl->m_hWnd);
				}
			};
			// Add, Get, Remove Parent elements
			void AddParent(CWnd* pParentWnd, WNDPROC wndProc)
			{
				ASSERT (pParentWnd != NULL);
				if (!GetParentStructure(pParentWnd->m_hWnd))
					m_mapWindowProcedureParent[pParentWnd->m_hWnd] = new SWindowProcedure( pParentWnd, wndProc );
			};
			SWindowProcedure* GetParentStructure(HWND hWnd)
			{
				SWindowProcedure* pParentSW = NULL;
				m_mapWindowProcedureParent.Lookup(hWnd, pParentSW);
				return pParentSW;
			};
			WNDPROC GetOldParentProcedure(HWND hWnd)
			{
				return GetParentStructure(hWnd)->m_oldWndProcWnd;
			}
			void RemoveParent(CWnd* pParent)
			{
				if (SWindowProcedure* pSW = GetParentStructure(pParent->m_hWnd))
				{
					delete pSW;
					m_mapWindowProcedureParent.RemoveKey(pParent->m_hWnd);
				}
			};
	};
 
	static CWindowProcs m_wndProcs;
 
	CMultiColumnComboBox(int nColumnKey = 0);
 
// Attributes
public:
 
// Operations
public:
 
// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMultiColumnComboBox)
	protected:
	virtual void PreSubclassWindow();
	virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	//}}AFX_VIRTUAL
 
// Implementation
public:
	virtual int SetMultipleHeight(int nMHeight = 5);
	virtual double SetRateWidth(double dWidthList = 0);
	virtual void SetCurrentItem(int nIndex);
	virtual int GetCurrentItem();
	void ForwardMessage(UINT nMsg, WPARAM wParam, LPARAM lParam);
	virtual BOOL IsDropedDown();
	virtual void DropDown(BOOL bDown = TRUE);
	virtual CListCtrl* GetListCtrl();
	virtual CEdit* GetEdit();
	static BOOL RegClassMultiColumnComboBox();
	virtual ~CMultiColumnComboBox();
	virtual void CaptureListCtrl();
 
	// Statical functions
	static LRESULT CALLBACK ListCtrlWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
	static LRESULT CALLBACK EditWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
	static LRESULT CALLBACK ParentWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
 
	// Generated message map functions
protected:
	BOOL m_bFirstShow;
	int m_nMultipleHeight;
	double m_dWidthList;
	virtual void SelectCurrentItem();
	int m_nColumnKey;
	virtual void Search(LPCTSTR lpszFindItem);
	BOOL OnInit();
	static CMultiColumnComboBox* m_pActiveMCBox;
	virtual void DrawButton(CDC* pDC, CRect r, BOOL bDown = FALSE);
	virtual void Resize();
	CListCtrl* m_pListCtrl;
	CEdit* m_pEdit;
	static CFont m_font;
	static CBrush m_brBkGnd;
	//{{AFX_MSG(CMultiColumnComboBox)
	afx_msg void OnDestroy();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnWindowPosChanged(WINDOWPOS FAR* lpwndpos);
	afx_msg void OnPaint();
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnSetFocus(CWnd* pOldWnd);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
private:
	BOOL m_bCaptured;
	void SetButton();
	void ReleaseButton();
	CRect m_rectBtn;
};
 
/////////////////////////////////////////////////////////////////////////////
 
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
 
#endif // !defined(AFX_MULTICOLUMNCOMBOBOX_H__1385ADA1_4D4F_11D2_8693_0040055C08D9__INCLUDED_)
Attachments:
 
hardcopy
hardcopy
 
[+][-]10/22/09 06:04 AM, ID: 25633616Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/22/09 06:10 AM, ID: 25633683Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/22/09 07:23 AM, ID: 25634548Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Windows MFC Programming
Sign Up Now!
Solution Provided By: Zoppo
Participating Experts: 2
Solution Grade: A
 
[+][-]10/22/09 11:27 AM, ID: 25637421Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625