我现在自己创建了CStatic控件,想在CStatic中再创建一个CButton的控件,并且还要响应CButton事件,能做到吗,怎么做??

解决方案 »

  1.   

    你新建一个class,例如CStaticEX,继承CStatic
    然后定义一个CButton变量
    这样创建button和响应button事件都发生在这个类里
    try~~~good luck,success
      

  2.   

    可以的
    1、你定义从CStatic派生的新类 CStaticEx
    2、在一个窗口中创建这个CStaticEx对象
        2.1 创建CStaticEx的对象
            CStaticEx m_cMyStatic;
       2.2 在窗口类的OnCreate函数中创建。
      m_cMyStatic.Create("test",WS_CHILD |WS_VISIBLE,CRect(0,0,100,200),this);
    3、在CStaticEx类中创建CButton对象
        3.1 创建CButton对象
             CButton m_cMyBtn;
        3.2 在CStaticEx::OnCreate函数中创建
    m_cMyBtn.Create("test",WS_CHILDWINDOW|WS_VISIBLE,CRect(20,20,80,180),this,0x400);
      

  3.   

    注意:
    1、定义的时候,一定要将子控件定义成类成员变量,不然他会在函数执行结束的时候析构。
    2、设置属性为WS_VISIBLE 不然他会隐藏。
      

  4.   

    动态创建一个CStatic控件,然后在其中动态创建按钮,注意矩形框的值
    在BEGIN_MESSAGE_MAP后添加ON_BN_CLICKED事件
    然后在DECLARE_MESSAGE_MAP上面添加消息响应函数原型
      

  5.   

    晕,我做了一遍
    可以的啊?
    一个新类~~~CStaticEX
    //Head file
    #pragma once
    class CStaticEX : public CStatic
    {
    DECLARE_MESSAGE_MAP()
    public:
    afx_msg void OnBnClicked();
    CButton *button;
    void AddButton();
    };
    //CPP file
    #include "stdafx.h"
    #include "StaticEX.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    BEGIN_MESSAGE_MAP(CStaticEX, CStatic)
    ON_BN_CLICKED(123, OnBnClicked)
    END_MESSAGE_MAP()void CStaticEX::AddButton()
    {
    button = new CButton();
    button->Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 
    CRect(10,10,100,30), this, 123);
    }void CStaticEX::OnBnClicked()
    {
    AfxMessageBox(_T("Button in Static"));
    }
    //调用
    CStaticEX *a = new CStaticEX();
    a->Create(_T("CStatic"), WS_CHILD|WS_VISIBLE|SS_CENTER,CRect(0,100,200,300), this);
    a->ShowWindow(SW_SHOW);
    a->AddButton();