wxWidgets面板未接收鼠标事件

发布于 2025-01-17 12:56:29 字数 1414 浏览 1 评论 0原文

我在捕获和处理面板中的鼠标事件时遇到了麻烦。 将鼠标事件绑定到我的主窗框,按预期工作。但是,当我将事件绑定到儿童面板时,他们成功地不会走我的框架,而是由我的面板正确处理。任何帮助将不胜感激。

我正在使用WXWIDGETS v3.1.5

以下是我最简单的示例:父框架内的一个面板。

单击面板应自身变黄。单击周围的框架区域应转动面板绿色。

// wxWidgets in full of strcpy
#pragma warning(disable : 4996)
#include <wx/wx.h>

class cPanel : public wxPanel {
 public:
  cPanel(wxWindow* parent, wxSize size)
      : wxPanel(parent, wxID_ANY, wxDefaultPosition, size) {
    this->Bind(wxEVT_LEFT_DOWN, &cPanel::OnLeftClick, this);
  }
  void OnLeftClick(wxMouseEvent& event) {
    SetBackgroundColour(wxColour("yellow"));
    Refresh();
  };
};

class cFrame : public wxFrame {
 public:
  wxPanel* child;
  cFrame()
      : wxFrame(nullptr, wxID_ANY, "Example Title", wxPoint(200, 200),
                wxSize(800, 500)) {
    child = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(300, 300));
    auto top = new wxBoxSizer(wxHORIZONTAL);
    top->Add(child, 0, wxALL, 20);
    SetSizer(top);

    this->Bind(wxEVT_LEFT_DOWN, &cFrame::OnLeftClick, this);
  };
  void OnLeftClick(wxMouseEvent& event) {
    child->SetBackgroundColour(wxColour("green"));
    child->Refresh();
  }
};

class cApp : public wxApp {
 public:
  cFrame* frame = nullptr;
  cApp(){};
  ~cApp(){};

  virtual bool OnInit() {
    frame = new cFrame();
    frame->Show();
    return true;
  }
};

wxIMPLEMENT_APP(cApp);

I am having trouble capturing and handling mouse events within a panel.
Binding mouse events to my main window frame works as expected. However, when I bind events to a child panel, they successfully don't go my frame, but are not correctly handled by my panel. Any help would be appreciated.

I am using wxWidgets v3.1.5

Below is my simplest example: a single panel inside a parent frame.

Clicking the panel should turn itself yellow. Clicking the surrounding frame area should turn the panel green.

// wxWidgets in full of strcpy
#pragma warning(disable : 4996)
#include <wx/wx.h>

class cPanel : public wxPanel {
 public:
  cPanel(wxWindow* parent, wxSize size)
      : wxPanel(parent, wxID_ANY, wxDefaultPosition, size) {
    this->Bind(wxEVT_LEFT_DOWN, &cPanel::OnLeftClick, this);
  }
  void OnLeftClick(wxMouseEvent& event) {
    SetBackgroundColour(wxColour("yellow"));
    Refresh();
  };
};

class cFrame : public wxFrame {
 public:
  wxPanel* child;
  cFrame()
      : wxFrame(nullptr, wxID_ANY, "Example Title", wxPoint(200, 200),
                wxSize(800, 500)) {
    child = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(300, 300));
    auto top = new wxBoxSizer(wxHORIZONTAL);
    top->Add(child, 0, wxALL, 20);
    SetSizer(top);

    this->Bind(wxEVT_LEFT_DOWN, &cFrame::OnLeftClick, this);
  };
  void OnLeftClick(wxMouseEvent& event) {
    child->SetBackgroundColour(wxColour("green"));
    child->Refresh();
  }
};

class cApp : public wxApp {
 public:
  cFrame* frame = nullptr;
  cApp(){};
  ~cApp(){};

  virtual bool OnInit() {
    frame = new cFrame();
    frame->Show();
    return true;
  }
};

wxIMPLEMENT_APP(cApp);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2025-01-24 12:56:29

wxcommandevents (以及从中派生的类)将过滤到父母窗口。 wxmouseeevent> wxmouseevent 不是源自wxcommandevent,因此在面板上的事件,因此面板不会过滤到框架。

因此,您需要将鼠标事件处理程序绑定到面板而不是帧。这是如何更改Cframe类的最后几行进行此操作的示例。

    this->Bind(wxEVT_LEFT_DOWN, &cFrame::OnLeftClick, this);
    child->Bind(wxEVT_LEFT_DOWN, &cFrame::OnChildLeftClick, this);
  };
  void OnLeftClick(wxMouseEvent& event) {
    child->SetBackgroundColour(wxColour("green"));
    child->Refresh();
  }

  void OnChildLeftClick(wxMouseEvent& event) {
    child->SetBackgroundColour(wxColour("yellow"));
    child->Refresh();
  }
};

有很多方法可以完成同一件事,但是我认为类似的事情可能是最简单的。

Only wxCommandEvents (and classes derived from it) will filter up to parent windows. wxMouseEvent does not derive from wxCommandEvent, so a mouse event on the panel that is not handled by the panel will not filter up to the frame.

Consequently, you'll need to Bind the mouse event handler to the panel instead of the frame. Here's an example of how to change the last few lines of your cFrame class to do that.

    this->Bind(wxEVT_LEFT_DOWN, &cFrame::OnLeftClick, this);
    child->Bind(wxEVT_LEFT_DOWN, &cFrame::OnChildLeftClick, this);
  };
  void OnLeftClick(wxMouseEvent& event) {
    child->SetBackgroundColour(wxColour("green"));
    child->Refresh();
  }

  void OnChildLeftClick(wxMouseEvent& event) {
    child->SetBackgroundColour(wxColour("yellow"));
    child->Refresh();
  }
};

There are many ways of accomplishing the same thing, but I think something like this is probably the simplest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文