VC CComboBox用法總結

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   ar   for   

VC每日一練,雖然簡單,不動手試一下不能真正記住。

大氣象CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1);

comboBox->InsertString(0,_T("9:30 "));
comboBox->InsertString(1, _T("10:30 ")); 
comboBox->SetCurSel(1); //設定選中的項
//取得選中的值
CString selStr;
int nIndex = comboBox->GetCurSel();//取得選中的索引
comboBox->GetLBText(nIndex,selStr);

MessageBox(selStr);

 預設ComboBox顯示一個很短的下拉框,很不方便。這裡有個函數,可以讓你設定下拉式清單的高度,很方便。
先在標頭檔中聲明:

 

public:
    void set_DropDownSize(CComboBox& box, UINT LinesToDisplay);

 

 再在源檔案中定義:

大氣象void CMySdiView::set_DropDownSize(CComboBox& box, UINT LinesToDisplay) 
/*-------------------------------------------------------------------------- 
* Purpose: Set the proper number of lines in a drop-down list or 
* combo box. 
* Description: Resizes the combo box window to fit the proper number 
* of lines. The window must exist before calling this function. 
* This function should be called when the combo box is created, and when 
* the font of the combo box changes. (e.g. WM_SETTINGCHANGE) 
* Testing needed: 
* Are there cases where SM_CYBORDER should be used instead of SM_CYEDGE? 
* owner-draw variable height combo box 
* Subclassed combo box with horizontal scroll-bar 
* Returns: nothing 
* Author: KTM 
*--------------------------------------------------------------------------*/ 
{ 
    ASSERT(IsWindow(box)); // Window must exist or SetWindowPos won‘t work 

    CRect cbSize; // current size of combo box 
    int Height; // new height for drop-down portion of combo box 

    box.GetClientRect(cbSize); 
    Height = box.GetItemHeight(-1); // start with size of the edit-box portion 
    Height += box.GetItemHeight(0) * LinesToDisplay; // add height of lines of text 

    // Note: The use of SM_CYEDGE assumes that we‘re using Windows ‘95 
    // Now add on the height of the border of the edit box 
    Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges 

    // The height of the border of the drop-down box 
    Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges 

    // now set the size of the window 
    box.SetWindowPos(NULL, // not relative to any other windows 
    0, 0, // TopLeft corner doesn‘t change 
    cbSize.right, Height, // existing width, new height 
    SWP_NOMOVE | SWP_NOZORDER // don‘t move box or change z-ordering. 
    ); 
} 

 

再在OnInitialUpdate()函數中調用:

CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1);
set_DropDownSize(*comboBox,5);// 第二個參數決定高度是顯示幾行
UpdateData(false);

 

VC CComboBox用法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.