对话框A的函数OnButton()调用类B的方法f(),如何将f()执行的结果显示在对话框A的ListBox中?
注意该ListBox对应关联成员变量m_display
void CADlg::OnButton(){
   B b;
   b.f();
   ...
}

解决方案 »

  1.   

    不好意思,有点漏洞。不是“将f()执行的结果显示在对话框A的ListBox中”,而是“将f()执行过程中产生的中间结果显示在对话框A的ListBox中”,比如f()执行过程中每隔一段时间会产生一个字符串,我想把生成的字符串显示在对话框A的ListBox中,遮盖怎么弄啊!等待高手指点!
      

  2.   

    先把字符串放进CString变量s中
    然后m_display.AddString(s);
      

  3.   

    你的意思应该是把生成的字符串放到变量CString s中,等b.f();执行完后再执行语句m_display.AddString(s)。
    但是我的程序中f()是通过while循环产生字符串的,我想在字符串生成后马上把它显示在m_display中(显然在f()中是不能调用m_display变量的)而不是把生成的字符串全部放到一个变量里头等f()执行完再调用。请问有没有什么好的方法可以实现啊?m_display.AddString显示。请问有什么
      

  4.   

    可以这样解决:
    设置一个定时器:SetTimer(1,100,NULL);//100毫秒一次
    添加OnTimer消息响应函数:
    if(1==nIDEvent)
    {
        m_display.AddString(s);
    }
    在f()函数中改变CString变量就可以了    
      

  5.   

    I suggest you transfer the pointer of the CListBox control to B.f(), as a parameter of it, then you will be able to add strings to the list box. Also there's another method to implement the function:
    (1) Declare a function pointer in class A:
    typedef static void __stdcall (*ADDSTRING_TOLISTBOX) (LPVOID pA,const CString& strTxt);
    (2) Define a function in class A:
    static void __stdcall AddStringToListBox(LPVOID pA,const CString& strTxt)
    {
    CA* pDlg=(CA*)pA;
    pA->m_clstBox.AddString(strTxt);
    }
    (3) Define b.f() in such a type:
    void func(ADDSTRING_TOLISTBOX pfnAddStringToListBox, LPVOID pParam){}
    (4) Call b.f() in class A:
    b.f(AddStringToList,this);I suggest you program in the second method, for a function pointer is able to deal with more situations.