zoukankan      html  css  js  c++  java
  • MFC ComBox控件相关使用案例

    1、ComBox控件中下拉框内容从文件中读取

      首先在工具箱中拖拽ComBox控件到对话框中,ID为IDC_COMBO_Model,同时在工程目录下创建一个名字为ModelInfo文件,在CcomBoxTestDlg.h文件中申明函数 void AddModelToComboBox();接着在CcomBoxTestDlg.cpp文件中实现AddModelToComboBox()函数,实现内容如下:

     1 void CcomBoxTestDlg::AddModelToComboBox() {
     2     CComboBox* pCombo = (CComboBox*)this->GetDlgItem(IDC_COMBO_Model);
     3     pCombo->ResetContent();
     4     CString model_path = _T(".\ModelInfo");
     5     CStdioFile file;
     6     if (file.Open(model_path, CFile::modeRead | CFile::shareDenyNone))
     7     {
     8         while (TRUE)
     9         {
    10             CString str;
    11             if (!file.ReadString(str))
    12             {
    13                 break;
    14             }
    15             CString model_name = str.SpanExcluding(_T(","));//SpanIncluding()
    16             model_name.TrimLeft(_T(" "));
    17             model_name.TrimRight(_T(" "));
    18             if (model_name == _T(""))
    19             {
    20                 continue;
    21             }
    22             if (pCombo->FindStringExact(-1, model_name) == CB_ERR)
    23             {/*
    24                 CComboBox::FindStringExact
    25                 int FindStringExact(int nIndexStart,LPCTSTR lpszFind) const;
    26                 当第一个参数为-1时,则表示查询整个列表框的项目
    27                 列表中有字符串model_name返回值为0
    28                 列表中没有字符串model_name返回值为-1    CB_ERR代表-1
    29              */
    30                 pCombo->AddString(model_name);
    31             }
    32         }
    33         file.Close();
    34     }
    35 }
    View Code

      最后在OnInitDialog()中添加this->AddModelToComboBox()调用此函数。文件内容及运行效果如下所示:

     1 void CcomBoxTestDlg::AddModelToComboBox() {
     2     ((CComboBox*)this->GetDlgItem(IDC_COMBO_Model))->ResetContent();
     3     CStdioFile file;
     4     int tmpFinder = 0;
     5     if (file.Open(_T(".\ModelInfo"), CFile::modeRead | CFile::shareDenyNone)) {
     6         CString tmp;
     7         while (file.ReadString(tmp)) {
     8             if (tmp.Find(_T("ModelName")) != -1) {
     9                 tmpFinder = 1;
    10             }
    11             if (tmpFinder == 1) {
    12                 if (tmp.Find(_T("=")) != -1) {
    13                     int postion = tmp.Find(_T("="));
    14                     CString model_tmp = tmp;
    15                     CString modelstr;
    16                     modelstr = model_tmp.Left(postion);
    17                     ((CComboBox*)this->GetDlgItem(IDC_COMBO_Model))->AddString(modelstr);
    18                 }
    19             }
    20         }
    21         file.Close();
    22     }
    23 }
    View Code
  • 相关阅读:
    iOS系列译文:自定义Collection View布局
    iOS系列译文:整洁的表视图代码
    各种类型的电影排行榜-movie路线
    学习正则
    sublime text 配置golang开发环境
    百度地图 Javascript API 笔记
    Sublime Text 3 若干问题解决办法
    无法修改系统Host的解决办法
    守望先锋overwatch美服外服设置方法
    Steam游戏黑屏与游戏直接安装方法
  • 原文地址:https://www.cnblogs.com/geziyu/p/14086722.html
Copyright © 2011-2022 走看看