// TreeComboBox.cpp : implementation file // #include "stdafx.h" #include "TreeComboBox3.h" #include "ylgl.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTreeComboBox3 CTreeComboBox3::CTreeComboBox3() :m_bTree(FALSE) ,m_bAlertBkg(FALSE) ,m_bAlertText(FALSE) ,m_droppedHeight(250) ,m_droppedWidth(100) { m_hBrushAlert = CreateSolidBrush(COLOR_ALERT); } CTreeComboBox3::~CTreeComboBox3() { DeleteObject(m_hBrushAlert); } BEGIN_MESSAGE_MAP(CTreeComboBox3, CComboBox) //{{AFX_MSG_MAP(CTreeComboBox3) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONDBLCLK() ON_WM_CTLCOLOR_REFLECT() //}}AFX_MSG_MAP ON_MESSAGE(WMU_CLOSE_CONTROL,OnCloseControl) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTreeComboBox3 message handlers void CTreeComboBox3::PreSubclassWindow() { // TODO: Add your specialized code here and/or call the base class CComboBox::PreSubclassWindow(); CRect rect(0, 0, 0, 0); #ifdef VC60 DWORD dwStyle = WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ; m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL); #else// CTreeCtrl会自己调CWnd的CreateEx传递WC_TREEVIEW //DWORD dwStyle = WS_POPUPWINDOW | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ; DWORD dwStyle = CBS_DROPDOWNLIST | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUP ; // m_Tree.CreateEx(0, dwStyle, rect, GetParent(), NULL); // VS2008中,CtrlTree不允许调用WS_POPUP或WS_POPUPWINDOW属性,否则Debug模式出错,同时CTreeCtrl调用CreateEx,会再调用Create,而不是CWnd::CreateEx; ((CWnd&)m_Tree).CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), NULL); // VS2008中,CtrlTree不允许调用WS_POPUP或WS_POPUPWINDOW属性,否则Debug模式出错; #endif //#ifdef VC60 m_Tree.Init(this); GetClientRect(rect); SetDroppedWidth(rect.Width()); SetDroppedHeight(m_droppedHeight); #ifdef VC60 dwStyle = 0x03 & GetStyle(); ASSERT(dwStyle == CBS_DROPDOWNLIST); #endif } BOOL CTreeComboBox3::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN) { DisplayTree(); return TRUE; } return CComboBox::PreTranslateMessage(pMsg); } void CTreeComboBox3::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_bTree = ! m_bTree; if(m_bTree)DisplayTree(); // CComboBox::OnLButtonDown(nFlags, point); } void CTreeComboBox3::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default OnLButtonDown(nFlags,point); // CComboBox::OnLButtonDblClk(nFlags, point); } void CTreeComboBox3::TreeCtrlDone() { CWnd* pParent = GetParent(); if(pParent != NULL) { CString str;GetWindowText(str); if(m_pretext!=str) { m_pretext=str; WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(),CBN_CLOSEUP); pParent->SendMessage(WM_COMMAND,wParam,(LPARAM)m_hWnd); } } } LRESULT CTreeComboBox3::OnCloseControl(WPARAM wParam, LPARAM lParam) { m_Tree.ShowWindow(SW_HIDE); m_bTree = FALSE; HTREEITEM hItemChanged = (HTREEITEM)lParam; if(hItemChanged) { SetTitle(m_Tree.GetItemText(hItemChanged)); } AlertBkg(FALSE); TreeCtrlDone(); return 1; } HBRUSH CTreeComboBox3::CtlColor(CDC* pDC, UINT nCtlColor) { HBRUSH hbr = NULL; // TODO: Change any attributes of the DC here if(nCtlColor == CTLCOLOR_EDIT) { if(! m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)); else pDC->SetTextColor(COLOR_RED); if(! m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW)); else pDC->SetBkColor(COLOR_ALERT); if(m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert; pDC->SetBkMode(TRANSPARENT); } // TODO: Return a different brush if the default is not desired return hbr; } void CTreeComboBox3::DisplayTree() { CRect rc; GetWindowRect(rc); rc.top = rc.bottom + 1; rc.right = GetDroppedWidth(); rc.bottom = GetDroppedHeight(); CString strText(_T("")); GetWindowText(strText); m_Tree.Display(rc, strText); // SetCurSel(-1); } void CTreeComboBox3::SetTitle(CString sTitle) { ResetContent(); AddString(sTitle); SetCurSel(0); } void CTreeComboBox3::RefDroppedWidth() { CRect rect; GetClientRect(rect); SetDroppedWidth(rect.Width()); InitTree(); } void CTreeComboBox3::InitTree() { CTreeCtrl& Tree = GetTreeCtrl(); CString bm="xxxyyyzzz"; HTREEITEM parent; for(int i=0; iGetSize(); i++) { if(bm!=m_pList1array->ElementAt(i).ElementAt(4)) { bm=m_pList1array->ElementAt(i).ElementAt(4); parent = Tree.InsertItem(bm); Tree.InsertItem("",parent); Tree.InsertItem(m_pList1array->ElementAt(i).ElementAt(1),parent); } else { Tree.InsertItem(m_pList1array->ElementAt(i).ElementAt(1),parent); } } }