在自定义的wxPanel中访问子项

时间:2012-07-29 19:29:34

标签: c++ wxwidgets codeblocks

所以我有一个继承自wxPanel的类MyPanel:

 class MyPanel : public wxPanel
 {
       public:
          MyPanel(wxWindow* parent) : wxPanel(parent){}
          void OnMouseMove(wxMouseEvent& event);
       private:
          DECLAER_EVENT_TABLE()
  };

和下面定义的另一个主要wxframe:

 class mainFrame : public wxFrame
 {
       ...
       private:
          ...
          MyPanel* myPanel;
          ...
          wxStaticText* StaticText1;
          ...
 };

最终将StaticText1指定为myPanel的子节点。 所以我想在方法OnMouseMove()

中更新鼠标光标的坐标

我想知道如何访问StaticText1并更新内容。

3 个答案:

答案 0 :(得分:1)

使用朋友声明,如下所示:

class mainFrame : public wxFrame
{
  friend class MyPanel;
  ...
};

答案 1 :(得分:0)

您可以在MyPanel中定义成员,并在需要时为其分配StaticText1。 在OnMouseMove中,检查StaticText1是否为NULL。

 class MyPanel : public wxPanel
  {
        public:
           MyPanel(wxWindow* parent) : wxPanel(parent){}
           void OnMouseMove(wxMouseEvent& event) { if (StaticText1) { /* do something */ } ;
           void SetStaticText1(wxStaticText* txt) { StaticText1 = txt;}
        private:
           DECLAER_EVENT_TABLE()
           wxStaticText* StaticText1;

   };

答案 2 :(得分:0)

从编程的角度来看,您可以使用友元解决方案。但从逻辑上讲,如果wxStaticText * StaticText1将成为MyPanel的父级,并且主要从MyPanel的一个成员函数中引用/更新。理想情况下,wxStaticText应该在MyPanel中声明和定义,而不是在mainFrame中。