VC每日一练,虽然简单,不动手试一下不能真正记住。
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article395855/20241017/56b4eed73d3648999b0fdc4e626ee256.gif)
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 -> 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 set_DropDownSize(CComboBox & box, UINT LinesToDisplay);
再在源文件中定义:
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article395855/20241017/56b4eed73d3648999b0fdc4e626ee256.gif)
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.
);
}
/* --------------------------------------------------------------------------
* 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 );
set_DropDownSize( * comboBox, 5 );// 第二个参数决定高度是显示几行
UpdateData( false );
本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/greatverve/archive/2010/11/11/ccombobox.html,如需转载请自行联系原作者