VC每日一练,虽然简单,不动手试一下不能真正记住。

  1.  
  2. 大气象
  3.  
  4. CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1);
  5.  
  6. comboBox->InsertString(0,_T("9:30 "));
  7. comboBox->InsertString(1, _T("10:30 ")); 
  8. comboBox->SetCurSel(1); //设置选中的项
  9. //取得选中的值
  10. CString selStr;
  11. int nIndex = comboBox->GetCurSel();//取得选中的索引
  12. comboBox->GetLBText(nIndex,selStr);
  13.  
  14. MessageBox(selStr);

默认ComboBox显示一个很短的下拉框,很不方便。这里有个函数,可以让你设置下拉列表的高度,很方便。
先在头文件中声明:

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

再在源文件中定义:

  1.  
  2. 大气象
  3.  
  4. void CMySdiView::set_DropDownSize(CComboBox& box, UINT LinesToDisplay) 
  5. /*-------------------------------------------------------------------------- 
  6. * Purpose: Set the proper number of lines in a drop-down list or 
  7. * combo box. 
  8. * Description: Resizes the combo box window to fit the proper number 
  9. * of lines. The window must exist before calling this function. 
  10. * This function should be called when the combo box is created, and when 
  11. * the font of the combo box changes. (e.g. WM_SETTINGCHANGE) 
  12. * Testing needed: 
  13. * Are there cases where SM_CYBORDER should be used instead of SM_CYEDGE? 
  14. * owner-draw variable height combo box 
  15. * Subclassed combo box with horizontal scroll-bar 
  16. * Returns: nothing 
  17. * Author: KTM 
  18. *--------------------------------------------------------------------------*/ 
  19. { 
  20.     ASSERT(IsWindow(box)); // Window must exist or SetWindowPos won't work 
  21.  
  22.     CRect cbSize; // current size of combo box 
  23.     int Height; // new height for drop-down portion of combo box 
  24.  
  25.     box.GetClientRect(cbSize); 
  26.     Height = box.GetItemHeight(-1); // start with size of the edit-box portion 
  27.     Height += box.GetItemHeight(0) * LinesToDisplay; // add height of lines of text 
  28.  
  29.     // Note: The use of SM_CYEDGE assumes that we're using Windows '95 
  30.     // Now add on the height of the border of the edit box 
  31.     Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges 
  32.  
  33.     // The height of the border of the drop-down box 
  34.     Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges 
  35.  
  36.     // now set the size of the window 
  37.     box.SetWindowPos(NULL, // not relative to any other windows 
  38.     0, 0, // TopLeft corner doesn't change 
  39.     cbSize.right, Height, // existing width, new height 
  40.     SWP_NOMOVE | SWP_NOZORDER // don't move box or change z-ordering. 
  41.     ); 
  42. } 

再在OnInitialUpdate()函数中调用:

  1. CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1);
  2. set_DropDownSize(*comboBox,5);// 第二个参数决定高度是显示几行
  3. UpdateData(false);

VC CComboBox用法总结的更多相关文章

  1. VC:CString用法整理(转载)

    1.CString::IsEmpty BOOL IsEmpty( ) const; 返回值:如果CString 对象的长度为0,则返回非零值:否则返回0. 说明:此成员函数用来测试一个CString ...

  2. VC++ GetSafeHwnd用法

    GetSafeHwnd HWND GetSafeHwnd() const; 当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数. ...

  3. C++ 学习资料搜寻与学习(第一期)(未完待续)

    一.图形图像类 [Visual C++]vs2008/2005正确打开vs2010所创建项目的几种方法 jlins 2012-04-12 14:38 [Visual C++]关于无法打开包括文件:“S ...

  4. Vc++ 控件用法总结之List Control

    1.新建对话框MFC,在对话框上放一个ListCtrl ID:IDC_PATH View:Report 2.为ListCtrl添加变量 右击->添加变量m_wndPath 3.找到OnInitD ...

  5. VC中TRACE()的用法

    个人总结:最近看网络编程是碰到了TRACE语句,不知道在哪里输出,查了一晚上资料也没找出来,今天终于在CSDN上找到了,真是个高地方啊,方法如下: 1.在MFC中加入TRACE语句 2.在TOOLS- ...

  6. VC++定义全局变量及extern用法

    基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern有两 ...

  7. VC中TRACE ASSERT VERIFY之用法

    一.TRACE宏     当选择了Debug目标,并且afxTraceEnabled变量被置为TRUE时,TRACE宏也就随之被激活了.但在程序的Release版本中,它们是被完全禁止的.下面是一个典 ...

  8. 【转】VC中MessageBox与AfxMessageBox用法与区别

    原文网址:http://blog.csdn.net/holybin/article/details/28403109 一.MessageBox()用法 1.函数原型 Messagebox函数在Win3 ...

  9. VC/MFC中的CComboBox控件使用详解

    CComboBox控件详解 CComboBox控件又称作组合框控件,其有三种形态可供选择,1.简单组合框(Simple)2.下拉组合框(Drop-down)3.下拉列表式组合框(Drop-down l ...

随机推荐

  1. Spring——(一)IoC

    1. 什么是IOC IOC:inversion of Control 控制反转. 控制反转:即控制权由应用程序代码转到了外部容器.(反转:就是控制权的转移).--降低业务对象之间的依赖程度,即实现了解 ...

  2. SQL基本语句以及示例

    基本语句: /*dorp colunm*/ 语法:ALTER TABLE 表名   DROP COLUMN 要删除的字段 验证财务转换的正确性,查询以下两个表是否有数据 /*表连接inner jion ...

  3. CODE[VS] 1230 元素查找

    1.题目戳这里 2.代码: #include<iostream> #include<algorithm> using namespace std; int n,m,a[1000 ...

  4. SAP 创建物料主数据分类视图特性

    1.CL01创建物料分类 2.去CT04中去创建特性值 创建完成之后保存, 3.创建物料的分类视图,选择相应的特性值

  5. Centos7.0安装配置PHP7.0

    YUM安装所需开发包 yum install wget make gcc gcc-c++ bison autoconf patch \ pcre-devel zlib-devel openssl-de ...

  6. Quartz.net 定式调度任务

    再用Quartz 做任务调度作业时,有以下步骤: ISchedulerFactory schedFact = new StdSchedulerFactory(); IScheduler _sched; ...

  7. 我眼中的项目leader

    个人觉得项目leader应该具备一下基础: 1.技术能力 2.领导能力 3.过滤产品不合理需求能力 4.项目周期把控能力

  8. 安装odoo服务

    sysv init 服务 从 odoo 源码 debian 目录 拷贝 init 至 /etc/init.d/ 并 更名为 odoo cd /opt/odoo sudo cp /opt/odoo/de ...

  9. <head></head>

    <!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8&qu ...

  10. 一个Email

    Email 1.接受表单数据并用单独变量保存起来,判断用户协议,这个是必须同意的.2.判断验证码,利用验证码类Captcha.3.判断用户名,密码,邮箱规则,利用Verify类验证.4.判断唯一性,邮 ...