VC——combo控件自动实现功能

呵呵,今天运气很好,突然发现一个实现combo控件按照字母搜索的类,很方便!
// ComboBoxInput.cpp : implementation file
//完成自动检索游戏名称的功能
#include "stdafx.h"
#include "newestnjn.h"
#include "ComboBoxInput.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CComboBoxInput
CComboBoxInput::CComboBoxInput()
{
m_bAutoComplete = TRUE;
}
CComboBoxInput::~CComboBoxInput()
{
}

BEGIN_MESSAGE_MAP(CComboBoxInput, CComboBox)
//{{AFX_MSG_MAP(CComboBoxInput)
ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditupdate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CComboBoxInput message handlers
//处理消息循环
BOOL CComboBoxInput::PreTranslateMessage(MSG* pMsg)
{
//捕捉按键消息
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 CComboBoxInput::OnEditupdate()
{
  if (!m_bAutoComplete)
      return;
  //获得编辑框的字符串
  CString str;
  GetWindowText(str);
  int nLength = str.GetLength();
  //当前选择范围
  DWORD dwCurSel = GetEditSel();
  WORD dStart = LOWORD(dwCurSel);
  WORD dEnd   = HIWORD(dwCurSel);
  //在列表框中搜索与编辑框中匹配的字符串,然后使其被选择
  if (SelectString(-1, str) == CB_ERR)
  {
      SetWindowText(str);
      if (dwCurSel != CB_ERR)
        SetEditSel(dStart, dEnd);
  }
  if (dEnd < nLength && dwCurSel != CB_ERR)
      SetEditSel(dStart, dEnd);
  else
      SetEditSel(nLength, -1);
}
#if !defined(AFX_COMBOBOXINPUT_H__713B246A_384B_4638_9A43_094C4EEFA674__INCLUDED_)
#define AFX_COMBOBOXINPUT_H__713B246A_384B_4638_9A43_094C4EEFA674__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ComboBoxInput.h : header file
//
/
// CComboBoxInput window
class CComboBoxInput : public CComboBox
{
// Construction
public:
CComboBoxInput();
BOOL m_bAutoComplete;  //是否智能输入
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComboBoxInput)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CComboBoxInput();
// Generated message map functions
protected:
//{{AFX_MSG(CComboBoxInput)
afx_msg void OnEditupdate();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_COMBOBOXINPUT_H__713B246A_384B_4638_9A43_094C4EEFA674__INCLUDED_)
在stdafx.h里面加#include "ComboBoxInput.h"即可,呵呵
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值