如何在mfc对话框中动态创建按钮 [英] how to create a button dynamically in mfc dialog form

查看:418
本文介绍了如何在mfc对话框中动态创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用以下代码在mfc对话框中动态创建一个按钮。



CButton myButton1,myButton2,myButton3,myButton4;





myButton1.Create(_T(我的按钮),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

CRect(10,10,100) ,30),pParentWnd,1);



在这段代码中我得到一个错误pParentwnd作为不是成员..我怎么能清除这个错误?

解决方案

你可以传递这个(这是指向 CDialog的指针本身)而不是 pParentWnd





[更新]

请注意 myButton 不应该是一个局部变量(它应该是一个类成员)。

我在MFC SDI应用程序的about对话框中使用了以下代码:

  class  CAboutDlg: public  CDialog 
{
CButton myButton;
// ...







 BOOL CAboutDlg :: OnInitDialog()
{
CDialog :: OnInitDialog();
myButton.Create(_T( 我的按钮),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
CRect( 10 10 100 30 ), 1 );
return TRUE;
}



工作正常(显示按钮)。

[/ Update]


I need to create a button dynamically in mfc dialog form by using following code..

CButton myButton1, myButton2, myButton3, myButton4;


myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(10,10,100,30), pParentWnd, 1);

In this code i got an error pParentwnd as not a member.. how can i clear this error?

解决方案

You may just pass this (that is the pointer to the CDialog itself) instead of pParentWnd.


[Update]
Please note myButton shouldn''t be a local variable (it should be a class member).
I used the following code in the about dialog of a MFC SDI application:

class CAboutDlg : public CDialog
{
  CButton myButton;
//...




BOOL CAboutDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
  CRect(10,10,100,30), this, 1);
  return TRUE;  
}


It is working fine (it shows the button).
[/Update]


这篇关于如何在mfc对话框中动态创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆