• 2004-04-29

    [原创] CAutoCompleteCombo - [VC开发专辑]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://junglesong.yourblog.org/logs/163416.html

    #if !defined(AFX_AUTOCOMPLETECOMBO_H__5FC0B924_A76A_4C95_AC30_EFC12F83BCBE__INCLUDED_) #define AFX_AUTOCOMPLETECOMBO_H__5FC0B924_A76A_4C95_AC30_EFC12F83BCBE__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // AutoCompleteCombo.h : header file // ///////////////////////////////////////////////////////////////////////////// // CAutoCompleteCombo window class CAutoCompleteCombo : public CComboBox { // Construction public: CAutoCompleteCombo(); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAutoCompleteCombo) public: virtual BOOL PreTranslateMessage(MSG* pMsg); //}}AFX_VIRTUAL // Implementation public: virtual ~CAutoCompleteCombo(); // Generated message map functions protected: //{{AFX_MSG(CAutoCompleteCombo) afx_msg void OnEditupdate(); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: BOOL m_bAutoComplete; }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_AUTOCOMPLETECOMBO_H__5FC0B924_A76A_4C95_AC30_EFC12F83BCBE__INCLUDED_) // AutoCompleteCombo.cpp : implementation file // #include "stdafx.h" #include "AutoCompleteCombo.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAutoCompleteCombo CAutoCompleteCombo::CAutoCompleteCombo() { m_bAutoComplete=TRUE; } CAutoCompleteCombo::~CAutoCompleteCombo() { } BEGIN_MESSAGE_MAP(CAutoCompleteCombo, CComboBox) //{{AFX_MSG_MAP(CAutoCompleteCombo) ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditupdate) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CAutoCompleteCombo message handlers BOOL CAutoCompleteCombo::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message==WM_KEYDOWN) { m_bAutoComplete=TRUE; int nVirtKey=(int)(pMsg->wParam); if(nVirtKey==VK_DELETE || nVirtKey==VK_BACK) { m_bAutoComplete=FALSE; } } return CComboBox::PreTranslateMessage(pMsg); } void CAutoCompleteCombo::OnEditupdate() { // TODO: Add your control notification handler code here if(!m_bAutoComplete) return; // Get current text concept in the combobox CString strText; GetWindowText(strText); int nLength=strText.GetLength(); // Get selected text range DWORD dwCurSel=GetEditSel(); WORD dStart=LOWORD(dwCurSel); WORD dEnd=HIWORD(dwCurSel); // Serach String if(SelectString(-1,strText)==CB_ERR) { SetWindowText(strText); if(dwCurSel!=CB_ERR) { SetEditSel(dStart,dEnd); } } // Select Text if(dEnd<nLength && dwCurSel!=CB_ERR) { SetEditSel(dStart,dEnd); } else { SetEditSel(nLength,-1); } } ///////////////////////////////////////////////////////////////////////////// // CTestAutoCompleteComboDlg message handlers BOOL CTestAutoCompleteComboDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application’s main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_combo.AddString("America"); m_combo.AddString

    收藏到:Del.icio.us




发表评论

您将收到博主的回复邮件
记住我