你为什么要将wndTree的父窗口指定为m_Button呢?指定成Dlg不就得了?

解决方案 »

  1.   

    从CButton派生一个类吧,用来声明m_button
      

  2.   

    1、把 m_Botton.Create("ksjdk",WS_CHILD¦WS_VISIBLE,CRect5,5,215,215),this,VIEW_BOTTON);
    中的第一个参数 "ksjdk" 去掉试试,可能你的BUTTON没有创建成功2、你的消息映射可能你问题
        
      

  3.   

    是好奇怪的思路象楼上那样吧
    让button 的OnSelchangedTree传递给dlg
      

  4.   

    to Julienjut(秋水)  这句话没错,"ksjdk"是m_Botton的标题。
    to beni() 我怎样才能传递呢?
      

  5.   

    To: wintenko
    不错,是我看错了,我以前是类名呢!在自定义的BUTTON类中收到消息后,转发给父窗口就行了!
      

  6.   

    你的这个例子没有试过,不过这种问题用消息反射会很好的解决
    从CTreeXXX派生新类,在新类中添加消息,这时你会看到在VC的消息映射对话框中多了几个前面有“=”号的消息,其中有TVN_SELCHANGED,点中这个反射消息进行处理就行了
      

  7.   

    如wintenko(目毕龙) 所说,
    在你的button类的OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 里用SendMessage什么的发给dlg
      

  8.   

    说错了
    是Julienjut(秋水) 所说
    >_<
      

  9.   

    MSVCer(家宝)的方法我试了,好像可以呀
      

  10.   

    MSVCer(家宝)的方法是行得通,我以前也是这样做的,
    但在此我要解决的是如何让CDlg收到wndTree发出的消息。
      

  11.   

    wndTree.Create(...);    
    wndTree.SetOwner(this);
      

  12.   

    comet(二子乘舟)的方法怎么没有效果?
      

  13.   

    哦,对
    SetOwner()的确不行。
    那就试试SetParent()吧。
      

  14.   

    哈哈
    我试了一下,SetParent()也是不行的。不好意思!
      

  15.   

    帖个代码吧
    // MyButton.cpp : implementation file
    //#include "stdafx.h"
    #include "testagain.h"
    #include "MyButton.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CMyButtonCMyButton::CMyButton()
    {
    }CMyButton::~CMyButton()
    {
    }
    BEGIN_MESSAGE_MAP(CMyButton, CButton)
    //{{AFX_MSG_MAP(CMyButton)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyButton message handlersBOOL CMyButton::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
    {
    // TODO: Add your specialized code here and/or call the base class
    int idCtrl = (int) wParam; 
    if (WM_USER+101==idCtrl)
    {
    LPNMHDR pnmh = (LPNMHDR) lParam; 
    if (TVN_SELCHANGED==pnmh->code)
    {
    int nRet=::SendMessage(GetParent()->m_hWnd,WM_USER+102,(WPARAM)wParam,(LPARAM)lParam);
    }
    }
    return CButton::OnNotify(wParam, lParam, pResult);
    }///////////////////////////////////////////////////
    //下面是父窗口里的处理
    BEGIN_MESSAGE_MAP(CTestagainDlg, CDialog)
    //{{AFX_MSG_MAP(CTestagainDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_MESSAGE(WM_USER+102,OnNotifyMsg)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()void CTestagainDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    CMyButton *pButton=new CMyButton;
    CRect rect(40,40,180,180);
    CRect rect2(0,0,140,140);
    pButton->Create("New",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect,this,WM_USER+100);
    CTreeCtrl *pTree=new CTreeCtrl;
    pTree->Create(WS_CHILD|WS_VISIBLE,rect2,pButton,WM_USER+101);
    pTree->InsertItem("Second");
    pTree->InsertItem("first");
    m_nTreeCtrl.InsertItem("First");
    m_nTreeCtrl.InsertItem("Second");
    pButton->ShowWindow(SW_SHOWNORMAL);
    pTree->ShowWindow(SW_SHOWNORMAL);
    }
    void CTestagainDlg::OnNotifyMsg(WPARAM wParam,LPARAM lParam)
    {
        AfxMessageBox("asdlfkjas;df");
    }
      

  16.   

    CMyButton 是从CButton继承过来的一个新类
    在其中处理WM_NOFIFY()消息,如果是新建的TREECTRL的消息则转发给其父窗口。
    (其中有一个问题,在消息映射中没有ON_MESSAGE(WM_NOTIFY)之类的语句,不知为什么)
    在爷爷窗口的OnButton1()中创建一个BUTTON,一个TreeCtrl,并加入了一些数据
    在OnNotifyMsg()进行想要的操作就可以了
      

  17.   

    //Resource.h
    #define IDC_TREE 1000
    //之所以在Resource.h写,是使IDC_TREE在MyButton.cpp和Dlg.cpp中都起作用.//MyButton.cpp
    #include "Resource.h"BEGIN_MESSAGE_MAP(CMyButton, CButton)
    //{{AFX_MSG_MAP(CMyButton)
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()void CGfxOutBarCtrl::OnSelchangedTree()
    {
    GetOwner()->SendMessage(TVN_SELCHANG,0,0);
    }//MyButton.h
    #define TVN_SELCHANG    WM_USER + 10
    protected:
    //{{AFX_MSG(CGfxOutBarCtrl)
    //}}AFX_MSG
    afx_msg void OnSelchangedTree();
    DECLARE_MESSAGE_MAP()// Dlg.cpp
    #include "Resource.h"
    BEGIN_MESSAGE_MAP(CDlg, CDialog)
    ON_MESSAGE(TVN_SELCHANG, OnSelchangedTree)
    END_MESSAGE_MAP()int CDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
        if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;
        // TODO: Add your specialized creation code here
        m_Botton.Create("ksjdk",WS_CHILD|WS_VISIBLE,CRect(5,5,215,215),this,VIEW_BOTTON);
        wndTree.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,200), &m_botton, IDC_TREE);
        return 0;
    }
    void CDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
    {
        NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
        AfxMessageBox("asdlfkjas;df");
    }// Dlg.h
    #define  VIEW_BOTTON 3016
    class CDlg: public CDialog
    {
    public:
    CTreeCtrl wndTree;
    CButton m_botton;
    protected:
    //{{AFX_MSG(CDlg)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };
      

  18.   

    不好意思写错了一点.再发一遍.
    //Resource.h
    #define IDC_TREE 1000
    //之所以在Resource.h写,是使IDC_TREE在MyButton.cpp和Dlg.cpp中都起作用.
    //MyButton.cpp
    #include "Resource.h"BEGIN_MESSAGE_MAP(CMyButton, CButton)
    //{{AFX_MSG_MAP(CMyButton)
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()void CMyButton::OnSelchangedTree()
    {
    GetOwner()->SendMessage(TVN_SELCHANG,0,0);
    }//MyButton.h
    #define TVN_SELCHANG    WM_USER + 10
    protected:
    //{{AFX_MSG(CMyButton)
    //}}AFX_MSG
    afx_msg void OnSelchangedTree();
    DECLARE_MESSAGE_MAP()// Dlg.cpp
    #include "Resource.h"
    BEGIN_MESSAGE_MAP(CDlg, CDialog)
    ON_MESSAGE(TVN_SELCHANG, OnSelchangedTree)
    END_MESSAGE_MAP()int CDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
        if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;
        // TODO: Add your specialized creation code here
        m_Botton.Create("ksjdk",WS_CHILD|WS_VISIBLE,CRect(5,5,215,215),this,VIEW_BOTTON);
        wndTree.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,200), &m_botton, IDC_TREE);
        return 0;
    }
    void CDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
    {
        NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
        AfxMessageBox("asdlfkjas;df");
    }// Dlg.h
    #define  VIEW_BOTTON 3016
    class CDlg: public CDialog
    {
    public:
    CTreeCtrl wndTree;
    CButton m_botton;
    protected:
    //{{AFX_MSG(CDlg)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };