?Jane
2024-11-17 22:27:01
最佳回答
l**tctrl是对话框中的一个控件,当然是在oninitdialog中初始化了,initinstance是初始化整个应用程序的,以下是某个对话框程序的initinstance()函数的内容: bool ccl**tctrlapp::initinstance() { afxenablecontrolcontainer(); #ifdef _afxdll enable3dcontrols(); #else enable3dcontrolsstatic(); #endif ccl**tctrldlg dlg; //////////a m_pmainwnd = &dlg; /////////////b int nresponse = dlg.domodal(); /////////c if (nresponse == idok) { // todo: place code here to handle when the dialog ** // d**m**sed with ok } else if (nresponse == idcancel) { // todo: place code here to handle when the dialog ** // d**m**sed with cancel } // since the dialog has been closed, return false so that we exit the // application, rather than start the application's ** pump. return false; } 函数中在a行声明ccl**tctrldlg对象dlg,然后在c行调用对话框类的domodal()函数显示对话框时才开始初始化对话框。在c行的前面时,对话框还没有建立,在c行的后面,对话框已经被销毁了,所以在initinstance()函数中无法初始化对话框的成员。 以下是一个在oninitdialog()函数中初始化cl**tctrl的例子: bool ccl**tctrldlg::oninitdialog() { cdialog::oninitdialog(); // set the icon for th** dialog. the framework does th** automatically // when the application's main window ** not a dialog seticon(m_hicon, true); // set big icon seticon(m_hicon, false); // set small icon // todo: add extra initialization here /*----------初始化cl**tctrl控件------------------*/ //第一步:建立图像列表,如果不需要图像列表,则这一步可以省略 m_imagel**t.create(16,16,true,2,2); m_imagel**t.add(afxgetapp()->loadicon(idi_iconl**t_male)); m_imagel**t.add(afxgetapp()->loadicon(idi_iconl**t_female)); m_l**tctrl.setimagel**t(&m_imagel**t,lvsil_small); //第二步:设置扩展属性 m_l**tctrl.setextendedstyle(lvs_ex_fullrowselect | lvs_ex_gridlines); //第三步:设置列 m_l**tctrl.insertcolumn(0,"学号",lvcfmt_left,60); m_l**tctrl.insertcolumn(1,"姓名",lvcfmt_left,130); m_l**tctrl.insertcolumn(2,"性别",lvcfmt_center,60); //第四步:插入行 m_l**tctrl.insertitem(0,"1001",0); m_l**tctrl.setitemtext(0,1,"**"); m_l**tctrl.setitemtext(0,2,"男");//插入第一行 m_l**tctrl.insertitem(1,"1002",1); m_l**tctrl.setitemtext(1,1,"张红"); m_l**tctrl.setitemtext(1,2,"女");//插入第二行 m_l**tctrl.insertitem(2,"1003",0); m_l**tctrl.setitemtext(2,1,"王云飞"); m_l**tctrl.setitemtext(2,2,"男");//插入第三行 m_l**tctrl.insertitem(3,"1004",0); m_l**tctrl.setitemtext(3,1,"张强国"); m_l**tctrl.setitemtext(3,2,"男");//插入第四行 m_l**tctrl.insertitem(4,"1005",1); m_l**tctrl.setitemtext(4,1,"孙柔嘉"); m_l**tctrl.setitemtext(4,2,"女");//插入第五行 /*-------------------------------------------------*/ return true; // return true unless you set the focus to a control } 该例子的工程文件下载地址: 20210311