Jam*_*s C 2 c++ events wxwidgets button event-handling
我正在尝试创建一个简单的基本应用程序,以wxWidgets开始。我已经可以设置框架并在其上放置菜单,菜单项和按钮,并且它们都可以正确显示。
但是我在使用Bind()将事件处理程序附加到按钮时遇到了麻烦。剥离一下代码,我有这个:
class MainFrameFunctions
{
public:
void buttonOneClicked(wxCommandEvent & event);
};
void MainFrameFunctions::buttonOneClicked(wxCommandEvent & event)
{
// do something
}
MainFrame::MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize& , long style, const wxString& ) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size1)
{
wxButton * buttonOne = new wxButton( panelOne, buttonOneID, wxT("Show Box"), wxPoint(70,50), wxSize(200,40) );
}
Run Code Online (Sandbox Code Playgroud)
因此,我想知道如何使用Bind()创建事件处理程序,以将处理程序功能“ buttonOneClicked”连接到buttonOne?还有,我在代码中的什么位置放置Bind()行?
编辑
经过VZ的推荐。我对该程序进行了一些编辑,因此主要组件现在看起来像这样:
class MainFrame: public wxFrame
{
public:
MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint
&pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE, const wxString &name=wxFrameNameStr);
void OnExit(wxCommandEvent& event);
bool ButtonOneClicked(wxCommandEvent & event);
wxDECLARE_EVENT_TABLE();
};
class CreateNewFrame: public wxFrame
{
public:
CreateNewFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize&, long style);
void OnExit(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
MainFrame::MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize& , long style, const wxString& )
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size1)
{
wxButton * buttonOne = new wxButton( panelOne, buttonOneID, wxT("Button One"), wxPoint(70,50), wxSize(200,40), 0, wxDefaultValidator, "PP" );
// while we are at it, what is the " wxDefaultValidator, "PP" " part about ? I just copied it from an example
buttonOne->Bind(wxEVT_BUTTON, &MainFrame::ButtonOneClicked, this);
}
bool MainFrame::ButtonOneClicked(wxCommandEvent & event)
{
CreateNewFrame* NewFrame = new CreateNewFrame(this, NewFrameID, "Button One Clicked", position2, size2);
NewFrame->Show( true );
return true;
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,我从CodeBlocks 13.12收到此错误消息:
no matching function for call to 'wxButton::Bind(const wxEventTypeTag<wxCommandEvent>&, bool (MainFrame::*)(wxCommandEvent&), MainFrame* const)'|
Run Code Online (Sandbox Code Playgroud)
小智 5
您Bind()
希望应用程序开始处理预期的事件时就可以使用事件处理程序。很多时候,这是在构建UI之后完成的,因此在您的情况下,MainFrame()
构造函数的末尾应该没问题。
Bind()
与许多其他wxWidgets功能一样,您可以在示例中找到使用示例。参见events
示例。对于你的情况,你可以做
buttonOne->Bind(wxEVT_BUTTON, &MainFrameFunctions::buttonOneClicked, aMainFrameFunctionsPointer);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4146 次 |
最近记录: |