vc动态生成控件源代码

1. CListCtrl 控件;

 

 

[c-sharp:showcolumns]  view plain copy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. void CDynCreateCtrlDlg::OnOK()   
  2. {  
  3.       
  4.     CWnd *wnd;  
  5.     CRect rect;  
  6.       
  7.     rect.left = 20;  
  8.     rect.right = rect.left + 50;  
  9.     rect.top = 20;  
  10.     rect.bottom = rect.top + 90;  
  11.       
  12.     CListCtrl *pListCtrl;  
  13.       
  14.       
  15.       
  16.     if (!(wnd =GetDlgItem(ID_MYCTRULLIST)))  
  17.     {  
  18.           
  19.           
  20.         if(m_list.CreateEx(WS_EX_CLIENTEDGE, // Make a 3D-border  
  21.             _T("EDIT"),   
  22.             NULL,  
  23.             ES_AUTOHSCROLL|  
  24.             ES_LEFT|ES_NOHIDESEL|  
  25.             WS_CHILD,  
  26.             rect, this,  
  27.             ID_MYCTRULLIST))//生成edit控件  
  28.         {  
  29.             //MessageBox("CreateEx ok!",NULL,MB_ICONSTOP);  
  30.             m_list.ShowWindow(SW_SHOW);  
  31.             m_list.SetFocus();  
  32.         }  
  33.         //MessageBox("GetDlgItem ok!",NULL,MB_ICONSTOP);  
  34.     }  
  35.       
  36.     //添加一些项  
  37.     CString str;  
  38.       
  39.       
  40.     for(int i=0;i<10;i++)  
  41.     {  
  42.           
  43.         str.Format("item%d",i);  
  44.         m_list.InsertItem(i,str);  
  45.           
  46.           
  47.     }  
  48.       
  49. }  
  50.   
  51. void CDynCreateCtrlDlg::OnCancel()   
  52. {  
  53.     // TODO: Add extra cleanup here  
  54.       
  55.     CDialog::OnCancel();  
  56. }  
  57.   
  58. void CDynCreateCtrlDlg::OnChangeEdit()  
  59. {  
  60.       
  61.     GetDlgItem(ID_MYEDIT)->GetWindowText(m_StaticCtrl);  
  62.       
  63.     UpdateData(FALSE);  
  64. }  
  65.   
  66. void CDynCreateCtrlDlg::OnButton1()   
  67. {  
  68.     CWnd *wnd;  
  69.       
  70.     if (wnd =GetDlgItem(ID_MYEDIT))  
  71.         wnd->DestroyWindow() ;  
  72.       
  73.     if (wnd =GetDlgItem(ID_MYCTRULLIST))  
  74.         wnd->DestroyWindow() ;  
  75. }  

 

2.CEdit 控件

 

[c-sharp]  view plain copy
  1. void CDynCreateCtrlDlg::OnOK()   
  2. {  
  3.     CWnd *wnd;  
  4.     CRect rect;  
  5.   
  6.     rect.left = 20;  
  7.     rect.right = rect.left + 110;  
  8.     rect.top = 20;  
  9.     rect.bottom = rect.top + 30;  
  10.   
  11.     if (!(wnd =GetDlgItem(ID_MYEDIT)))  
  12.     {  
  13.         m_MyEdit.CreateEx(WS_EX_CLIENTEDGE, // Make a 3D-border  
  14.                         _T("EDIT"),   
  15.                         NULL,  
  16.                         ES_AUTOHSCROLL|  
  17.                         ES_LEFT|ES_NOHIDESEL|  
  18.                         WS_CHILD,  
  19.                         rect, this,  
  20.                         ID_MYEDIT);//生成edit控件  
  21.         m_MyEdit.ShowWindow(SW_SHOW);  
  22.         m_MyEdit.SetFocus();  
  23.     }  
  24.       
  25. }  
  26.   
  27. void CDynCreateCtrlDlg::OnCancel()   
  28. {  
  29.     // TODO: Add extra cleanup here  
  30.       
  31.     CDialog::OnCancel();  
  32. }  
  33.   
  34. void CDynCreateCtrlDlg::OnChangeEdit()  
  35. {  
  36.   
  37.   GetDlgItem(ID_MYEDIT)->GetWindowText(m_StaticCtrl);  
  38.     
  39.   UpdateData(FALSE);  
  40. }  
  41.   
  42. void CDynCreateCtrlDlg::OnButton1()   
  43. {  
  44.     CWnd *wnd;  
  45.       
  46.     if (wnd =GetDlgItem(ID_MYEDIT))  
  47.         wnd->DestroyWindow() ;  
  48.       
  49. }  

 

 

 3.通过PList=new CListCtrl(); PList 指针生成CList 控件; PList是CDynCreateCtrlDlg 的成员函数

 

 CWnd *wnd;
 CRect rect;
 
 rect.left = 20;
 rect.right = rect.left + 100;
 rect.top = 20;
 rect.bottom = rect.top + 150;

  

PList=new CListCtrl();
 if (!(wnd =GetDlgItem(ID_MYCTRULLIST)))
 {
  
  if(PList->Create(WS_EX_CLIENTEDGE, rect, this,ID_MYCTRULLIST))
  {
   PList->ShowWindow(SW_SHOW);
   PList->SetFocus();
   
  }
 }

 

ps:

 

定义CDynCreateCtrlDlg类成员:

               CEdit   m_MyEdit;
               CListCtrl m_list;

               CListCtrl * PList;

 

#define ID_MYEDIT   1820
#define ID_MYCTRULLIST   1821

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值