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/>

    关于作者:......

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

  • 相关阅读:
    NYOJ 23 取石子(一)
    XYNUOJ 2026 素数环
    XYNUOJ 1756 魔法工会
    XYNUOJ 1784 胜利大逃亡
    NYOJ 18 The Triangle
    NYOJ 737 合并石子
    XYNUOJ 问题 B: 敌兵布阵
    NYOJ 1063 生活的烦恼
    XYNUOJ 1774 最少拦截系统
    XYNUOJ 1248 排队打水问题
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/14502065.html
Copyright © 2011-2022 走看看