Automatic filtering selection combination box

zhaozj2021-02-17  52

After two days of encoding debugging, a combination of automatic filtering is completed, with the following advantages: 1. Do not hinder the original function 2 of the combination frame. Depending on the word that the user has entered in the edit box, the matching item is filtered from the list option of the combo box and displayed using the drop-down list for users to choose. 3. No special settings are required. Generate a CCOMBOX from View / ClassWizard / Member Variables, then replace CCOMBOX to caccomboBOX in the source file; or let ClassWizard know caccomboBox (delete myapp.clw, select View / ClassWizard, press Tips to regenerate myApp.clw, absolutely Most of the time can be successful), generate a caccomboBox directly from View / ClassWizard / Member Variables. 4. You can make it a combo box with other similar functions (automatically fill in. ").

Due to time rush, there is no write detailed note. Interested friends, please wait a few days later to ask me. Contact: linger0822@163.net

code show as below:

File: // ---------------------------------------------- ------------------------------- // File: AccomboBox.h //// DESC: / / According to the user The words have been entered in the box, filter out the matching item from the list options of the combo box, and display the drop-down list to option for the user to select /// copyright (c) 2001 Eaglefly Studio. /// ORIGINAL AUTHOR: zhengpeng. Lan // Author: // // Create Time: 2001/10/10 // Modify Time: 2001/10/11 // File: // ----------------- -------------------------------------------------- ---------- # if! defined (AFX_ACCOMBOBOX_H__81CBBD04_3955_4076_A688_74D3EA9730D9__INCLUDED _) # define AFX_ACCOMBOBOX_H__81CBBD04_3955_4076_A688_74D3EA9730D9__INCLUDED_

#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000

#include "afxtempl.h"

// AccomBOBOX.H: header file // class caccomboBOBOX; /// cwndForAccomboBox windowTypedEf struct {int Nid; cstring strdepict;

Typedef Carray CinfofoCComboBoxArray;

Class CWndForAcComboBox: public cwnd {friend caccomboBox; private: // construction lpctstr lpwndcls; // constructionpublic: cwndForaccomboBOX ();

// attributespublic: ccomboBox * m_pcomboBox; int m_nmaxcount; cinfoforaccomboBoxArray M_AInfo; int m_nmousepos; int m_nbeginshow;

inline void RemoveAll () {m_aInfo.RemoveAll (); CWnd :: SetScrollRange (SB_VERT, 0,0);} inline void Add (CInfoForACComboBox & info) {m_aInfo.Add (info);} inline int GetSize () {return m_aInfo. GetSize ();} inline void ShowList () {if (IsWindow (GetSafeHwnd ())) {ShowWindow (SW_SHOWNOACTIVATE);}} // Operationspublic: // Overrides // ClassWizard generated virtual function overrides file: // {{AFX_VIRTUAL ( CWndForAcComboBox) PUBLIC: Virtual Bool Create (CCOMBOBOX * PWND); file: //}} AFX_VIRTUAL

// ImplementationPublic: Virtual ~ CWndForAcComboBox ();

// Generated message map functionsprotected: file: // {{AFX_MSG (CWndForACComboBox) afx_msg void OnDestroy (); afx_msg void OnPaint (); afx_msg void OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnLButtonUp (UINT nFlags, CPoint point); afx_msg BOOL OnMouseWheel (UINT nFlags, short zDelta, CPoint pt); afx_msg void onKillFocus (CWnd * pNewWnd); afx_msg void OnMouseMove (UINT nFlags, CPoint point); afx_msg void OnVScroll (UINT nSBCode, UINT nPos, CScrollBar * Pscrollbar); File: //}} AFX_MSG DECLARE_MESSAGE_MAP ()};

/// caccomboBOX Window

Class CaccomboBox: Public CCOMBOBOX {private: Ced * m_pedit;

// constructionPublic: caccomboBOX ();

// Attributespublic: CWndForACComboBox m_wndList; inline void WindowMove (void) {if (IsWindow (m_wndList.GetSafeHwnd ())) m_wndList.Invalidate (FALSE);} // Operationspublic: virtual void HandleCompletion ();

// Overrides // ClassWizard generated virtual function overrides file: // {{AFX_VIRTUAL (CACComboBox) protected: virtual BOOL OnCommand (WPARAM wParam, LPARAM lParam); virtual void PreSubclassWindow (); file: //}} AFX_VIRTUAL

// ImplementationPublic: Virtual ~ caccomboBOX ();

// generated message map functionsprotace: file: // {AFX_MSG (caccomboBox) AFX_MSG void onkillfocus (); AFX_MSG VOID ONSELCHANGE (); File: //}} AFX_MSGDECLARE_MESSAGE_MAP ()};

/

/ file: // {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.

#ndif //! defined (AFX_ACCOMBOBOX_H__81CBBD04_3955_4076_A688_74D3EA9730D9__INCLUDED_)

File: // ---------------------------------------------- ------------------------------- // File: AccomboBox.cpp //// DESC: / / According to the user The words have been entered in the box, filter out the matching item from the list options of the combo box, and display the drop-down list to option for the user to select /// copyright (c) 2001 Eaglefly Studio. /// ORIGINAL AUTHOR: zhengpeng. Lan // Author: // // Create Time: 2001/10/10 // Modify Time: 2001/10/11 // File: // ----------------- -------------------------------------------------- ------------

#include "stdafx.h" #include "accomboBOX.H"

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

/// CACComboBox

CaccomboBox :: caccomboBox (): ccomboBox () {m_pedit = null;

CACCOMBOBOX :: ~ caccomboBox () {if (m_pedit) delete m_pedit;}

BEGIN_MESSAGE_MAP (CACComboBox, CComboBox) file: // {{AFX_MSG_MAP (CACComboBox) ON_CONTROL_REFLECT (CBN_KILLFOCUS, OnKillfocus) ON_CONTROL_REFLECT (CBN_SELCHANGE, OnSelchange) file: //}} AFX_MSG_MAPEND_MESSAGE_MAP ()

/// CaccomboBOX Message Handlers /// CWNDFORACComboBox

CWndForACComboBox :: CWndForACComboBox (): CWnd () {lpWndCls = AfxRegisterWndClass (0); m_pComboBox = NULL; m_nMaxCount = 8; m_aInfo.SetSize (0,8); m_nMousePos = -1; m_nBeginShow = 0;}

CWndForAccomboBox :: ~ cWndForAccomboBox () {

}

BEGIN_MESSAGE_MAP (CWndForACComboBox, CWnd) file: // {{AFX_MSG_MAP (CWndForACComboBox) ON_WM_DESTROY () ON_WM_PAINT () ON_WM_KEYDOWN () ON_WM_LBUTTONUP () ON_WM_MOUSEWHEEL () ON_WM_KILLFOCUS () ON_WM_MOUSEMOVE () ON_WM_VSCROLL () file: //}} AFX_MSG_MAPEND_MESSAGE_MAP () / // CWndFoCcomboBox Message Handlers

BOOL CWNDFORACCOMBOX :: Create (CCOMBOBOX * PWND) {Assert (PWND);

CRECT RECT; CWND * PPARENTWND = PWND-> getParent ();

PWND-> getWindowRect (& Rect); // pparentWnd-> ScreenToClient (& Re);

BOOL bRet = CWnd :: CreateEx (WS_EX_TOPMOST | WS_EX_TOOLWINDOW, / lpWndCls, NULL, WS_POPUP | WS_VSCROLL, rect.left, rect.top, rect.Width (), rect.Height (), pParentWnd-> GetSafeHwnd (), NULL, NULL);

IF (BRET) SETOWNER (PParentWnd);

M_PCOMBOBOX = PWnd; Return Bret;

Void CWndForAccomboBOX :: ONDESTROY () {CWnd :: ONDESTROY (); M_AINFO.REMOVEALL ();

Void CWndForAccomboBox :: onpaint () {create; cfont * pfont; logfont logfont;

if (m_pComboBox) {ASSERT (:: IsWindow (m_pComboBox-> GetSafeHwnd ())); CWnd * pParentWnd = m_pComboBox-> GetParent (); m_pComboBox-> GetWindowRect (& rect); pFont = pParentWnd-> GetFont (); ASSERT ( PFONT); PFONT-> GetLogfont (& logfont);

if (logFont.lfHeight <0) logFont.lfHeight = -logFont.lfHeight; if (m_aInfo.GetSize ()> m_nMaxCount) {logFont.lfWidth = m_nMaxCount * logFont.lfHeight;} else {logFont.lfWidth = m_aInfo.GetSize () * logfont.lfheight;} if (logfont.lfwidth rect.bottom> :: getSystemMetrics (sm_cottom) {rect.bottom = Rect.top; Rect.top = Rect.bottom - logfont.lfwidth - 2; if (RECT). TOP <0) Rect.top = 0;} else {rect.top = Rect.bottom; Rect.Bottom = Rect.top logfont.lfwidth 2;} Rect.right = Rect.LEFT M_PCOMBOBOX-> getDroppedWidth () ; // pparentWnd-> ScreenToClient (& Rect); MoveWindow (& Re); if (Rect.Height ()

File: //dc.fillsolidRect (0, 0, Rect.width (), Rect.Height (), RGB (255, 255, 255)); dc.Rectangle (0, 0, Rect.width (), Rect.Height ()) ; logFont.lfWidth = 1; dc.SelectObject (pFont); int m_nBeginShow = CWnd :: GetScrollPos (SB_VERT); for (int i = m_nBeginShow; i

Void CWndForAccomboBOX :: ONLBUTTONUP (UINT NFLAGS, CPOINT) {cfont * pfont; logfont logfont;

if (m_pComboBox) {pFont = m_pComboBox-> GetParent () -> GetFont (); pFont-> GetLogFont (& logFont); if (logFont.lfHeight <0) logFont.lfHeight = -logFont.lfHeight; m_nMousePos = (point.y - 1) / logFont.lfHeight CWnd :: GetScrollPos (SB_VERT); if (m_nMousePos> = 0 && m_nMousePos SetCurSel (m_aInfo [m_nMousePos] .nid); ShowWindow (SW_HIDE);} CWnd :: ONLBUTTONUP (NFLAGS, POINT);

BOOL CWndForACComboBox :: OnMouseWheel (UINT nFlags, short zDelta, CPoint pt) {// TODO: Add your message handler code here and / or call default return CWnd :: OnMouseWheel (nFlags, zDelta, pt);}

void CWndForACComboBox :: OnKillFocus (CWnd * pNewWnd) {CWnd :: OnKillFocus (pNewWnd);!! if (pNewWnd-> GetSafeHwnd () = m_pComboBox-> GetSafeHwnd () && pNewWnd-> GetSafeHwnd () = GetSafeHwnd () && IsWindow ( GetSafeHwnd ())) ShowWindow (SW_HIDE);} void CWndForACComboBox :: OnMouseMove (UINT nFlags, CPoint point) {CFont * pFont; LOGFONT logFont; :: ShowCursor (TRUE); if (m_pComboBox) {pFont = m_pComboBox-> GetParent ( ) -> GetFont (); pFont-> GetLogFont (& logFont); if (logFont.lfHeight <0) logFont.lfHeight = -logFont.lfHeight; logFont.lfWidth = point.y / logFont.lfHeight CWnd :: GetScrollPos (SB_VERT ); If (logfont.lfwidth! = M_nmousepos) {m_nmousepos = logfont.lfwidth; invalidate ();}} cWnd :: onMousemove (nflags, point);

Void CWndForAcComboBox :: ONVSCROLL (uint npos, cscrollbar * pscrollbar) {if (m_ainfo.get :: ONVScroll (nsbcode, npos, pscrollbar); return;}

Switch (nsbcode) {// case sb_bottom: // scroll to bottom. // Case SB_ENDSCROLL: / / End Scroll. Case SB_Linedown: // Scroll One Line Down. {Rect Rc; Logfont Logfont; cfont * pfont;

GetClientRect (& rc); pfont = m_pcomboBox-> getParent () -> getFont (); pfont-> getLogfont (& logfont); assert (logfont.lfheight! = 0); if (logfont.lfheight <0) logfont.lfheight = - Logfont.lfheight; rc.left = (rc.bottom - rc.top) / logfont.lfheight;

INT NPOS = GetScrollPos (SB_VERT) 1; IF (Npos rc.left <= m_AINFO.GETSIZE ()) {setscrollpos (SB_VERT, NPOS); invalidate ();}} Break; Case SB_LineUp: // Scroll One Line Up {INT NPOS = GetScrollPos (SB_VERT) - 1; IF (NPOS> = 0) {setscrollpos (SB_VERT, NPOS); Invalidate ();}} Break; Case SB_Pagedown: // Scroll One Page Down. {Rect Rc; logfont Logfont; cfont * pfont; getClientRect (& rc); pfont = m_pcomboBox-> getParent () -> getFont (); pfont-> getLogfont (& logfont); assert (logfont.lfheight! = 0); if (logfont.lfheight <0); if (logfont.lfheight <0); if (logfont.lfheight <0); if ) logfont.lfheight = -logfont.lfheight;

Rc.top = rc.bottom / logfont.lfheight; int npos = getScrollPos (SB_VERT); if (NpOS M_AINFO.GETSIZE ) - rc.top) npos = m_ainfo.getize () - rc.top; setscrollpos (sb_vert, npos); invalidate ();}} Break; Case SB_PageUp: // scroll one page up. {Rect rc; logfont logfont; Cfont * pfont;

GetClientRect (& rc); pfont = m_pcomboBox-> getParent () -> getFont (); pfont-> getLogfont (& logfont); assert (logfont.lfheight! = 0); if (logfont.lfheight <0) logfont.lfheight = - Logfont.lfheight;

Rc.top = rc.bottom / logfont.lfheight; int npos = getScrollPos (SB_VERT); if (npos> 0) {npos - = rc.top; if (npos <0) NPOS = 0; setscrollpos (SB_VERT, NPOS) ; Invalidate ();}} break; // case SB_THUMBPOSITION:. // Scroll to the absolute position The current position is provided in nPos case SB_THUMBTRACK: // Drag scroll box to specified position The current position is provided in nPos... {Setscrollpos; invalidate ();} Break; // case sb_top: // scroll to top} CWnd :: ONVSCROLL (NSBCODE, NPOS, PSCROLLBAR);} void caccombox :: handlecompletion () {// Make Sure We can 'talk' to the edit control if (m_pedit == null) {m_pedit = new CEDIT (); m_pedit-> subclasswindow (getdlgitem (1001) -> getsafehwnd ());}

// Save the State of the Edit Control CString Text, BestText; CinfoforaccomboBox Info; Int n = getCount ();

M_wndlist.removeall (); m_pedit-> getWindowText (text); cpoint ptcaret = m_pedit-> getCaretPos ();

// int start, end; // m_pedit-> getsel (start, end);

// Perform actual completion int bestIndex = -1; int startFrom = int_max; int.com; for (int x = 0; x

From = info.strdepict.find (text);

IF (from! = -1) {m_wndlist.add (info); if (from! = -1 && from

IF (BestIndex! = -1) {// restore the edit control m_pedit-> setWindowText (besttext); m_ped.get-> setsel (text.getLength (), bestText.getLength ()); m_pedit-> setcaretpos (ptcaret); if (m_wndList.GetSize () = 0!) {m_wndList.ShowList (); m_wndList.Invalidate ();} else {m_wndList.ShowWindow (SW_HIDE);}} BOOL CACComboBox :: OnCommand (WPARAM wParam, LPARAM lParam) {if (HiWord (WPARAM) == en_change) {handlecompletion (); return true;} return ccomboBox :: oncommand (wparam, lparam);

Void caccombobox :: preSubclassWindow () {ccomboBox :: PRESUBCLASSWINDOW ();

m_wndlist.create (this);}

Void caccombobox :: onkillfocus () {m_wndlist.onkillfocus (getfocus ());}

Void caccombobox :: onselchange () {m_wndlist.showwindow (sw_hide);

转载请注明原文地址:https://www.9cbs.com/read-29430.html

New Post(0)