打算在主窗口中创建一个按钮,在CMyDlg类当中定义CButton对象/指针,出现如下问题:
1,在OnInitDialog()函数中采用如下代码:CButton btn;btn.Create()无法创建一个按钮,但是采用CButton* btn=new CButton;btn->Create()则可以创建。
2,但如果在CMyDlg类声明当中定义public:CButton btn;在OnInitDialog()中btn.Create()则可以成功创建按钮。
以上3中方法有什么区别,谢谢!
1,在OnInitDialog()函数中采用如下代码:CButton btn;btn.Create()无法创建一个按钮,但是采用CButton* btn=new CButton;btn->Create()则可以创建。
2,但如果在CMyDlg类声明当中定义public:CButton btn;在OnInitDialog()中btn.Create()则可以成功创建按钮。
以上3中方法有什么区别,谢谢!
解决方案 »
- TCP网络:检查当前网络buffer中是否已经全部接收完成
- opengl 为什么不显示图啊!!求解释!
- 如何使鼠标双击时,前一次单击操作不处理?
- L"程式師"是什么意思,char c[]="程式師";如何轉換為L"程式師"所表示的類型?
- 链表问题:如何把一个链表声明为静态的?急!!
- dll程序怎么调试
- PRINTER对象的PAGE属性设置成自定义,如何设置?
- daiwoo领分
- usb插拔接收消息参数均为7,没有8000或其它的,请问为何?急
- 怎样在程序中执行另一个可执行文件?
- 为什么在VITSA和IE7的环境下,ActiveX控件中getsockname()函数得到的端口和netstat看到的端口不一致?
- win32版的snort1.7如何使用啊
这个是因为 你第一种方式 CButton btn 是创建在栈上 ,函数结束的时候 btn会销毁 ,btn销毁的时候 会调用destroywindow()
2个同上 作为类成员变量的时候 ,CMyDlg对象在销毁的时候 public CButton btn 才会销毁,所以能创建成功
CButton btn;btn.Create();
btn.Detach();试试
In fact,In the first case, You really create a button successfully,but it is destroyed when OnInitialDlg() function return ,as you have created a temporary button object on the stack;In the second case , you create it successfully and it works well.That's because you new the object on the heap,it remains there until you make a free operation;In the last case,as the button object is a class member, it lasts in the whole life cycle of the CMydlg class.