1. 程式人生 > >MFC控制元件之Combo Box

MFC控制元件之Combo Box

下拉鍊表Combo-box Control

 

常用屬性:

Sort:對新增到列表框的字串進行自動排序。(對指定位置的元素項無效)

Type:有三個型別

      Simple:沒有下拉按鈕,可以輸入字串,可以通過上下左右切換顯示的item。

      Dropdown:有下拉按鈕,可以輸入,可以下拉選擇item。

      下拉列表:有下拉按鈕,不可以輸入,可以下拉選擇item。

 

列表框常用訊息對映巨集


     ON_CBN_DBLCLK                                               滑鼠雙擊
     ON_CBN_DROPDOWN                                       列表框被彈出

     ON_CBN_KILLFOCUS / ON_CBN_SETFOCUS   在輸入框失去 / 得到輸入焦點時產生
     ON_CBN_SELCHANGE                                       列表框中選擇的行發生改變

     ON_CBN_EDITUPDATE                                       輸入框中內容被更新

 

控制元件焦點問題

      通常要判斷控制元件是否獲得了焦點,可以用GetFocus()函式
      例如:if(GetFocus() == GetDlgItem(IDC_EDIT_VALUE2))//判斷焦點是否在編輯框IDC_EDIT_VALUE2內。
      但是combobox 的焦點不同,因為它是由edit和listbox兩部分組成的
      所以獲得焦點要用GetParent():if((GetFocus()->GetParent()) == GetDlgItem(IDC_COMBO_CF))

 

常用操作:

    //下拉鍊表新增item
    m_combox_one.AddString(_T("zero"));
    m_combox_one.AddString(_T("one"));
    m_combox_one.AddString(_T("four"));
    m_combox_one.AddString(_T("five"));
    m_combox_one.AddString(_T("six"));
    m_combox_one.AddString(_T("seven"));
    m_combox_one.AddString(_T("eight"));
    m_combox_one.AddString(_T("nine"));
    m_combox_one.AddString(_T("ten"));
    //下拉鍊表指定位置插入item
    m_combox_one.InsertString(2, _T("insert two"));
    m_combox_one.InsertString(3, _T("insert three"));

    //設定下拉鍊表最大顯示item個數
    int maxshowitem = 5;
    m_combox_one.SetMinVisibleItems(maxshowitem);

    //通過index設定需要顯示的item內容
    int nshowindex = 2;
    m_combox_one.SetCurSel(nshowindex);

    //從下拉鍊表得到被選擇顯示的item的index
    int ngetIndex = m_combox_one.GetCurSel();

    //獲取指定index的item的內容
    int nIndex = 0;
    CString strCBText;
    m_combox_one.GetLBText(nIndex, strCBText);

    //通過控制元件獲取被選中顯示的item的內容
    CString strWinText;
    GetDlgItem(IDC_COMBOX_ONE)->GetWindowTextW(strWinText);

    //通過item內容查詢其index
    int nStartAfter = 0;
    CString itemstring = _T("four");
    int nfindstringIndex = m_combox_one.FindStringExact(nStartAfter, itemstring);
    nfindstringIndex = m_combox_one.FindString(nStartAfter, itemstring);

    //通過item內容,指定其顯示的item,並且返回item的index
    CString itemcontain= _T("insert three");
    int nincludestringIndex = m_combox_one.SelectString(nStartAfter, itemcontain);//刪除指定index位置的item
    int ndeleteindex = 5;
    m_combox_one.DeleteString(ndeleteindex);

    // ((CComboBox*)GetDlgItem(IDC_COMBOX_ONE))等效於m_combox_one
    //取得item數
    int iCount = ((CComboBox*)GetDlgItem(IDC_COMBOX_ONE))->GetCount();

    //清除目前所有項
    //m_combox_one.ResetContent();

    //設定或得到輸入框中被選中的字元位置
    int nStartChar = 1;
    int nEndChar = 3;
    m_combox_one.SetEditSel(nStartChar, nEndChar);
    DWORD editsel=m_combox_one.GetEditSel(); //取不到結果?????

    //設定輸入框中可輸入的最大字元數。
    int nMaxChars = 20;
    m_combox_one.LimitText(nMaxChars);

 

    //通過item內容查詢其index
    int nStartAfter = 0;
    CString itemstring = _T("four");
    int nfindstringIndex = m_combox_one.FindStringExact(nStartAfter, itemstring);
    itemstring = _T("fo");//查詢字首匹配的item
    nfindstringIndex = m_combox_one.FindString(nStartAfter, itemstring);