在MFC中,生成一个对话框de程序。定义 CButton *mybutton;和 struct str_dir *p_str_dir; 两个指针为全局。在程序 ::OnInitDialog()中动态生成N个CButton *mybutton= new CButton[N];个按钮。并通过struct str_dir *p_str_dir结构对 mybutton[xyz].SetWindowText(p_str_dir[xyz].m_dir_name);赋值。以上都还能正常运行。但是,当通过自定义de消息响应函数来改变动态生成de按钮上de文本“mybutton[m_xs].SetWindowText(p_str_dir[m_xs+1].m_dir_name);”时,出错!编译提示为“error C2039: 'SetWindowTextA' : is not a member of 'HWND__'
        d:\visual studio\vc98\include\windef.h(195) : see declaration of 'HWND__'”请问大家,这是怎么回事??是不是指针是全局de,可是指向de数据在::OnInitDialog()后就没有了?谢谢!

解决方案 »

  1.   

    是不是指针是全局de,可是指向de数据在::OnInitDialog()后就没有了?我想法应该是不是这个原因,因为你new的数据是在堆上的,你要调用delete 才会把这些对象释放掉
      

  2.   

    new的数据是在堆上,那还会有作用域de问题吗??
      

  3.   

    但是,当通过自定义de消息响应函数来改变动态生成de按钮上de文本“mybutton[m_xs].SetWindowText(p_str_dir[m_xs+1].m_dir_name);”时,出错!
    ========================为啥是m_xs+1 ??
      

  4.   

    而前面是:
    mybutton[xyz].SetWindowText(p_str_dir[xyz].m_dir_name);
      

  5.   

    xyz 和 m_xs 只是变量。
      

  6.   

    把p_str_dir[xyz-1]数组里de数据给mybutton[xyz]数组
      

  7.   

    CButton *mybutton= new CButton[N];【重复定义 CButton *】->   mybutton= new CButton[N];
      

  8.   

    xyz 和 m_xs 只是变量。
    ======================知道只是变量,我是说你为啥用m_xs+1,而不是m_xs 。
      

  9.   

    在MFC中,生成一个对话框de程序。定义 CButton *mybutton;和 struct str_dir *p_str_dir; 两个指针为全局。在程序 ::OnInitDialog()中动态生成N个CButton *mybutton= new CButton[N];个按钮。并通过struct str_dir *p_str_dir结构对 mybutton[xyz].SetWindowText(p_str_dir[xyz].m_dir_name);赋值。以上都还能正常运行。
    =================
    你能保证吗?我试了可是不行的。
    下面的代码一定能满足你的要求。调试通过void CMySeDlg::OnButton1() 
    {
    CButton *mybutton;
    struct str_dir *p_str_dir; //两个指针为全局。
    int N = 10;
    mybutton= new CButton[N];
    p_str_dir = new str_dir[N];

    RECT rect;
    rect.left = 10;
    rect.top = 10;
    rect.right = 50;
    rect.bottom = 50; for(int xyz=0; xyz<N; xyz++)
    {
    char str[10];
    itoa(xyz, str, 10);
    p_str_dir[xyz].m_dir_name = "DirName";
    strcat(p_str_dir[xyz].m_dir_name.GetBuffer(0), str);
    mybutton[xyz].Create(NULL, WS_VISIBLE, rect, this, 1000+xyz);
    mybutton[xyz].SetWindowText(p_str_dir[xyz].m_dir_name);
    }
    }
      

  10.   

    1) 为啥用m_xs+1,而不是m_xs  那是应为程序需要。
    2)【重复定义 CButton *】嗯~  有可能的确是重复定义。要试一试。3)在 ::OnButton1() 里当然都是可以di,但是假如你在 ::OnButton2() 里在次通过CButton *mybutton; struct str_dir *p_str_dir; //两个指针为全局   
    来读取数据,能行吗?? 会不会有作用域de问题??   有时间我还要来试试。
      

  11.   

    总算有点明白楼主的意思了。在 ::OnButton1()已经Crecte 了,在 ::OnButton2() 直接SetWindowText 就行了。
    一开始: CButton *mybutton = NULL;
    在OnButton_N中判断
    if(mybutton == NULL)
    {
       Crecte
       SetWindowText 
    }
    else
    {
       SetWindowText 
    }
      

  12.   

    我在把::OnInitDialog()里重复定义de “CButton *mybutton= new CButton[N];”和“struct str_dir *p_str_dir= new struct str_dir[m_flie];”改为“mybutton= new CButton[N];”“p_str_dir= new struct str_dir[m_flie];”后,编译时,错误更多了,提示为“: error C2039: 'Create' : is not a member of 'HWND__'
            c:\program files\microsoft visual studio\vc98\include\windef.h(195) : see declaration of 'HWND__'”“: error C2039: 'ShowWindow' : is not a member of 'HWND__'
            c:\program files\microsoft visual studio\vc98\include\windef.h(195) : see declaration of 'HWND__'”“: error C2039: 'SetWindowTextA' : is not a member of 'HWND__'
            c:\program files\microsoft visual studio\vc98\include\windef.h(195) : see declaration of 'HWND__'”
    PS: 我在::OnInitDialog()里就创建并初始化完成“mybutton[xyz].SetWindowText(p_str_dir[xyz].m_dir_name);”然后我在我自定义de消息响应函数里写上“mybutton[xyz].SetWindowText(p_str_dir[xyz-1].m_dir_name);”也会编译出错,提示跟上面de差不多。
      

  13.   

    用new创建Button只是声明一个对象,而没有实际创建控件,当调用Create函数时创建Button句柄,这时候才能调用SetWindowText函数
    CButton* ptr = new CButton;
    ptr->Create(...);
    if(ptr->m_hWnd)
    ptr->SetWindowText(...);
      

  14.   

    我在声明 new Button 后,就已经Create了,并且已经调用SetWondowText来对按钮添加文本。但是,当我在另外一个函数里,想用SetWindowText来更改文本时,出错。
      

  15.   

    哈哈~~  昨天抓了一天de头皮,终于把问题搞清楚了。就是因为“::”因为是全局de指针,所以要在指针前加“::”,所以为什么会又有重复定义de问题。在这里十分感谢大家de热心帮助,特别是 xlzxlich(阳光) 兄   和 '70ers 兄。