简体   繁体   English

动态创建的按钮不会显示在mfc中

[英]dynamically created button is does not show in mfc

So I have this code 所以我有这个代码

CButton details;
details.Create(_T("details"),WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON,CRect(120,100,100,30), this, 15000);

but it doesn't do anything(created button is not visible after creating it). 但它没有做任何事情(创建后创建的按钮不可见)。 What am I missing? 我错过了什么?

EDIT: The code is in a dialog based application's OnInitDialog function. 编辑:代码在基于对话框的应用程序的OnInitDialog函数中。 What it should do is to display the button. 它应该做的是显示按钮。

Your CButton is created with automatic storage duration. 您的CButton是使用自动存储持续时间创建的。 So it is destroyed when OnInitDialog returns. 所以它在OnInitDialog返回时被销毁。 (Which is before the dialog is visible.) Make the CButton a member variable instead. (在对话框可见之前。)使CButton成为成员变量。

您提供的CRect下的值不正确,必须是CRect(120, 100, 220, 130)

this is depending to declaring CButton details; 这取决于声明CButton details; ! you must declare CButton details; 你必须申报CButton details; as general instance(not local instance) 作为一般实例(不是本地实例)

define your CButton details; 定义你的CButton details; instance as general, so your problem will solved! 实例一般,所以你的问题就解决了! bellow code work 100 percentage : 波纹管代码工作100%:

#define BBB 10000
CButton c;
void CThreadsDlg::OnBnClickedButton1()
{
    bool a = c.Create(_T("new button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(100, 100, 220, 230), this, BBB);
}

if c button created, a variable will be true . 如果创建a c按钮,则变量为true

My experience: nothing is shown if your dialog is inherited from CDHtmlDialog, but works OK with normal CDialog. 我的经验:如果你的对话框是从CDHtmlDialog继承而没有显示,但是对普通的CDialog工作正常。 So change the first line in OnInitDialog() 所以改变OnInitDialog()中的第一行

//CDHtmlDialog::OnInitDialog();
CDialog::OnInitDialog();

and of course, the button variable should be global or class member, not local. 当然,button变量应该是全局或类成员,而不是本地成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM