Combobox控件

来源:互联网 发布:出租屋 网络方案 编辑:程序博客网 时间:2024/05/16 00:37

1. 在控件的Propertties属性中,在data面板中 ,可以添加在列表框中默认的数据

 

2. style面板中,默认有sort功能,如果不需要可以去掉

 

3. 实现带有自动查询功能的函数 :在用户输入一段文字后,自动匹配符合的列表,并补全

void CFunDlg::OnEditupdateCombo2() //combobox的消息响应函数
{
 if(!m_Auto)
  return;
 CString str;
 m_Combo.GetWindowText(str);
 int nLength = str.GetLength();
 DWORD dwCurSel = m_Combo.GetEditSel();
 DWORD dStart = LOWORD(dwCurSel);
 DWORD dEnd = HIWORD(dwCurSel);
 if(m_Combo.SelectString(-1,str) == CB_ERR)
 {
  m_Combo.SetWindowText(str);
  if(dwCurSel != CB_ERR)
   m_Combo.SetEditSel(dStart,dEnd);
 }
 m_Combo.GetWindowText(str);
 
 if(dEnd < nLength && dwCurSel != CB_ERR)
  m_Combo.SetEditSel(dStart,dEnd);
 else
  m_Combo.SetEditSel(nLength,-1);
}

BOOL CFunDlg::PreTranslateMessage(MSG* pMsg)
{
 if(pMsg->message==WM_KEYDOWN)
 {
  m_Auto = TRUE;
  int nKey = (int)pMsg->wParam;
  if(nKey == VK_DELETE || nKey == VK_BACK)
   m_Auto = FALSE;
 }
 return CDialog::PreTranslateMessage(pMsg);
}

 

5. 列举磁盘目录

m_Combo.Dir(DDL_DRIVES|DDL_EXCLUSIVE,"");

 

6. 自动调整下拉列表宽度的ComBox  

 

HBRUSH CFunDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  //WM_CTLCOLOR
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 switch(nCtlColor)
 {
 case CTLCOLOR_EDIT:
  break;
 case CTLCOLOR_LISTBOX:
  int iItemNum=m_Combo.GetCount();
  int iWidth=0;
  CString strItem;
  CClientDC dc(this);
  int iSaveDC=dc.SaveDC();
  dc.SelectObject(GetFont());
  int iVSWidth=::GetSystemMetrics(SM_CXVSCROLL);
  for(int i=0;i<iItemNum;i++)
  {
   m_Combo.GetLBText(i,strItem);
   int iWholeWidth=dc.GetTextExtent(strItem).cx+iVSWidth;
   iWidth=max(iWidth,iWholeWidth);
  }
  iWidth+=dc.GetTextExtent("a").cx;
  dc.RestoreDC(iSaveDC);
  if(iWidth>0)
  {
   CRect rc;
   pWnd->GetWindowRect(&rc);
   if(rc.Width()!=iWidth)
   {
    rc.right=rc.left+iWidth;
    pWnd->MoveWindow(&rc);
   }
  }break;
 }
 return hbr;
}

 

 

 

原创粉丝点击