一.常用状态
CComboBox控件又称作组合框控件,其有三种形态可供选择:
1.简单组合框(Simple)
2.下拉组合框(Drop-down)
3.下拉列表式组合框(Drop-down list).
二.常用设置属性:
1.type属性:里面一共有三个选项.就是其三种形式, 我们常用的是后两种形态,其区别就是Dropdown的编辑区为可编辑控件,而droplist为静态控件.
2.Da
3.Sort 属性:对添加到下拉列表框中的数据自动进行排序,如果你不想他改变你的显示序列,把他设置成False.
三.常用函数:
控件是一个窗口.其基类是为CWnd.所以CComboBox也可以使用CWnd的一些函数,它自己的函数有30几种.
注:m_cbox 为CComboBox控件的关联变量,m_end为EDIT控件的关联变量
1. GetCount() 函数:
功能:用以获得列表框中的选项数目.
原型:int GetCount() const;
示例:
int i;
CString str;
i=m_cbox.GetCount();
str.Format ("%d",i);
m_end="一共有"+str+"个数据";
UpdateData(FALSE);
2. GetCurSel() 函数:
功能:用以得到用户选中下拉列表框中数据的索引值.返回的值是重0开始的,如果没有选择任何选项将会返回-1。
原型:Int GetCurSel() const
示例:
int i;
CString str;
i=m_cbox.GetCurSel ();
str.Format ("%d",i+1);
if (i==-1) m_end="你什么都没有选";
else m_end="你选的是第"+str+"项";
UpdateData(FALSE);
3. SetCurSel函数:
功能:选取列表框中的一个选项,索引从0开始,-1为什么都不选.当你需要把列表框中已经选取的内容清空时就可以使用这个函数;
示例:
if(m_i<3)
m_cbox.SetCurSel (m_i++);//m_i为int型变量 用以计数
else
{
m_cbox.SetCurSel (-1);
m_i=0;
}
4. SetEditSel函数:
功能:设置编辑区中蓝色选中字段的长度
原型:BOOL SetEditSel( intnStartChar,int nEndChar );
说明:nStartChar是起始的位置,当设置为-1时为不选择任何字段,nEndChar是结束位置.还有一个对应的函数GetEditSel是取得其位置,在此不详解.
示例:
m_cbox.SetEditSel (3,5);//在编辑框中打入一串数字在按此按钮就能看到效果,其作用就是选中第3到第5个字符作为热点
5. Clear() Cut() Paste() Copy()
这四个函数我放在一起说,因为他们都是针对编辑框中选定的文字进行编辑的函数,很多初学者很不明白这4个函数,使用这些函数没有作用,其实不然,这些函数对于在编辑框中的内容不进行处理,他真正处理的是你用鼠标选中的内容,对于Clear()函数很多书中解释都是有误的,很多书中对他的解释是清除当前选中的文本,我认为这样的解释是有歧异的,真正的解释是把你刚选中的文本释放掉,也就是不选,而不是把选中的删除,真正的删除选中的文本的函数是Cut(),Copy()是复制选中的文本,Paste()是粘贴到光标处.其演示就不写在程序中,有兴趣的朋友可以自己试试.
6. LimitText()函数
功能:其作用是限制在编辑框中键入的文本长度
原型:BOOL LimitText(int nMaxChars);
说明:当nMaxChars为0时,不是不能输入,而是长度最大化为65535字节
7. GetLBText()函数
原型:void GetLBText( int nIndex, CString&rString ).
说明:nIndex为列表框中数据的索引值,rString 为字符串,其作用就是把索引号为nIndex的数据放到rString变量中.与其对应的GetLBTextLen(intnIndex)函数就是得到索引号为nIndex中数据的长度
示例:
m_cbox.GetLBText (2,m_end); // GetLBText按钮
UpdateData(0);
CString str; //GetLBTextLen按钮
int i=m_cbox.GetLBTextLen (2);
str.Format ("%d",i);
m_end=str;
UpdateData(FALSE);
8. AddString() DeleteString() InsertString() ResetContent() 四个函数放在一起,他们都是针对下拉列表框的处理函数,
AddString(LPCTSTR lpszString) 是在列表框的尾巴添加一条数据,参数为字符串.
DeleteString(UINT nIndex)为删除指定索引为nIndex的数据. InsertString(int nIndex, LPCTSTR lpszString)在索引nIndex处插入IpszString的内容.
ResetContent()是清除列表框中所有的内容.
9. SetDroppedWidth(UINT nWidth) 函数.
用于设定下拉列表框的最小宽度,当下拉列表框中的数据很长不能完全显示时,我们可以使用这个函数把列表框的宽度设置的大些.同样GetDroppedWidth()是返回列表框的宽度.
//下拉列表框的自动伸长
CString str;
CSize sz;
int dx=0;
CDC* pDC = pmyComboBox.GetDC();
for (int i=0;i < pmyComboBox.GetCount();i++)
{
pmyComboBox.GetLBText( i, str );
sz = pDC->GetTextExtent(str);
if (sz.cx > dx)
dx = sz.cx;
}
pmyComboBox.ReleaseDC(pDC);
dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE);
pmyComboBox.SetDroppedWidth(dx);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCComboBoxDlg::On
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::On
}
}
// If you add a minimize button to your dialog, you will need the co
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCComboBoxDlg::On
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::On
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCComboBoxDlg::On
{
return (HCURSOR) m_hIcon;
}
HBRUSH CCComboBoxDlg::On
{
HBRUSH hbr = CDialog::On
/* switch (nctlcolor) {
case ctlcolor_edit:
break;
case ctlcolor_listbox:
if (listwidth > 0) {
// new width of a listbox is defined
CRect rect;
pwnd->GetWindowRect(&rect);
if (rect.Width() != listwidth) {
rect.right = rect.left + listwidth;
pwnd->MoveWindow(&rect);
}
}
break;
}
*/
// TODO: Return a different brush if the default is not desired
return hbr;
}
评论