zoukankan      html  css  js  c++  java
  • MFC-一个很简单的动态创建控件例子

    前两天开发工具用到了一个动态创建控件的需求,

    写个简单的例子,记录一下。本身例子也比较简单,就不做详细说明了,看不懂百度就好了。

    VS2012
    
    int count1 = 0;
    void CMFCApplication1Dlg::OnCbnSelchangeCombo1()
    {
        // TODO: 在此添加控件通知处理程序代码
    
        //删除动态控件
        int bun1id1 = 19960;//控件的id
        for (int i=0;i<count1;i++)
        {
            CComboBox* pCombox = (CComboBox*)GetDlgItem(bun1id1);
            delete pCombox;
            bun1id1++;
        }
    
        int bun1id1_static = 18960;
        for (int i=0;i<count1;i++)
        {
            CStatic* pSta = (CStatic*)GetDlgItem(bun1id1_static);
            delete pSta;
            bun1id1_static++;
        }
    
        //获得当前选择的数量
        CString enumNameCstr;
        m_combox.GetWindowText(enumNameCstr);
        int num = atoi(enumNameCstr.GetBuffer());
        //动态创建控件
        int bun1id = 19960;//控件的id
        for (int i=0;i<num;i++)
        {
            CComboBox* combox = new CComboBox();
            combox->Create(WS_VISIBLE|WS_CHILD|CBS_DROPDOWNLIST, CRect(100, 80+i*30,300,500+i*300), this, bun1id);
            combox->AddString("1");
            combox->AddString("2");
            combox->AddString("3");
            bun1id++;
            count1++;
        }
    
        //动态创建控件(模板类型名字)
        int bun1id_static = 18960;
        for (int i=0;i<num;i++)
        {    
            char str[256];
            sprintf_s(str, "这是%d", i+1);
    
            CStatic* sta = new CStatic();
            sta->Create(str,WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(10,80+i*30,80,200+i*30), this, bun1id_static);
    
            bun1id_static++;
        }
    }
    
    
    
    void CMFCApplication1Dlg::OnBnClickedButton1()
    {
        // TODO: 在此添加控件通知处理程序代码
    
        CString enumNameCstr;
        m_combox.GetWindowText(enumNameCstr);
        int num = atoi(enumNameCstr.GetBuffer());
        //读取动态控件
        int bun1id1 = 19960;
        for (int i=0;i<num;i++)
        {
            TCHAR ch1[256];
            GetDlgItem(bun1id1)->GetWindowText(ch1, 256);
            AfxMessageBox(ch1);
            bun1id1++;
        }
    
    }
    
    
    Caesar卢尚宇
    2021年3月8日

    作者: 阿飞

    出处: https://www.cnblogs.com/nxopen2018/>

    关于作者:......

    如有问题, 可在底部(留言)咨询.

  • 相关阅读:
    ubuntu 14.04 安装python包psycopg2
    vmare 往 virtualbox迁移
    docker-compose & docker 镜像/加速
    nodejs & npm & gulp 安装和配置
    airflow 优化
    airflow 部署
    windows 上vmare超卡的问题解决方案
    HDU 6781 Solo (贪心 + 优先队列)
    HDU 6779 Drink (最小费用流)
    HDU 6778 Car (状压DP)
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/14502065.html
Copyright © 2011-2022 走看看