// TreeComboBox2.cpp : implementation file // #include "stdafx.h" #include "TreeComboBox2.h" #include "ylgl.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define WM_USER_ComboBox_END WM_USER+1001 ///////////////////////////////////////////////////////////////////////////// // CTreeComboBox2 CTreeComboBox2::CTreeComboBox2() :m_bTree(FALSE) ,m_bAlertBkg(FALSE) ,m_bAlertText(FALSE) ,m_droppedHeight(250) ,m_droppedWidth(100) { m_hBrushAlert = CreateSolidBrush(COLOR_ALERT); m_pMsgParent=NULL; m_mode=0; } CTreeComboBox2::~CTreeComboBox2() { DeleteObject(m_hBrushAlert); } BEGIN_MESSAGE_MAP(CTreeComboBox2, CComboBox) //{{AFX_MSG_MAP(CTreeComboBox2) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONDBLCLK() ON_WM_CTLCOLOR_REFLECT() ON_WM_SETFOCUS() //}}AFX_MSG_MAP ON_MESSAGE(WMU_CLOSE_CONTROL,OnCloseControl) END_MESSAGE_MAP() void CTreeComboBox2::OnSetFocus(CWnd* pOldWnd) { CComboBox::OnSetFocus(pOldWnd); m_bExchange = TRUE; } void CTreeComboBox2::SetCtrlData(DWORD dwData) { m_dwData = dwData; } DWORD CTreeComboBox2::GetCtrlData() { return m_dwData; } ///////////////////////////////////////////////////////////////////////////// // CTreeComboBox2 message handlers void CTreeComboBox2::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 = WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUPWINDOW; // 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); InitTree(); } BOOL CTreeComboBox2::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; } /* if(pMsg->message == WM_KEYDOWN) { if(pMsg->wParam == VK_RETURN) { CWnd* pParent = this->GetParent(); m_bExchange = TRUE; ::PostMessage(pParent->GetSafeHwnd(),WM_USER_EDIT_END,m_bExchange,1); } else if(pMsg->wParam == VK_ESCAPE) { CWnd* pParent = this->GetParent(); m_bExchange = FALSE; ::PostMessage(pParent->GetSafeHwnd(),WM_USER_EDIT_END,m_bExchange,1); } }*/ return CComboBox::PreTranslateMessage(pMsg); } void CTreeComboBox2::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 CTreeComboBox2::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default OnLButtonDown(nFlags,point); // CComboBox::OnLButtonDblClk(nFlags, point); } void CTreeComboBox2::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 CTreeComboBox2::OnCloseControl(WPARAM wParam, LPARAM lParam) { TreeCtrlDone(); m_Tree.ShowWindow(SW_HIDE); m_bTree = FALSE; HTREEITEM hItemChanged = (HTREEITEM)lParam; if(hItemChanged) { SetTitle(m_Tree.GetItemText(hItemChanged)); } AlertBkg(FALSE); if(m_pMsgParent) { if(m_mode) ::PostMessage(m_pMsgParent->GetSafeHwnd(),WM_USER+1003,m_bExchange,0); else ::PostMessage(m_pMsgParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0); } return 1; } HBRUSH CTreeComboBox2::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 CTreeComboBox2::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 CTreeComboBox2::SetTitle(CString sTitle) { ResetContent(); AddString(sTitle); SetCurSel(0); } void CTreeComboBox2::RefDroppedWidth() { CRect rect; GetClientRect(rect); SetDroppedWidth(rect.Width()); } void CTreeComboBox2::InitTree() { CTreeCtrl& Tree = GetTreeCtrl(); CString bm="xxxyyyzzz"; HTREEITEM parent; for(int i=0; i