WXWidgets 事件参数 [英] WXWidgets Event Arguments

查看:28
本文介绍了WXWidgets 事件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WXWidgets 中设置一个带有按钮网格的窗口.这些按钮中的每一个都将打开另一个窗口.现在我知道我可以通过根据按下的按钮在单独的函数中调用每个窗口来做到这一点.但这似乎有点低效.

I'm trying to set up a window in WXWidgets with a grid of buttons. Each of these buttons will open another window. Now I know I can do this by having the call to each window in a separate function depending on the button that was pressed. But that seems a bit inefficient.

我想要做的是让每个按钮在点击时调用相同的函数,但传递一个 ID,用于确定打开哪个窗口.

What I want to do is have each of these buttons call the same function when clicked, but pass an ID which will be used to determine which window is opened up.

我使用的基本代码如下(删除了位,我有一个关闭应用程序的按钮,只是为了测试按钮)

The basic code I'm using is as follow (bits are stripped out, I have a button that closes the application, just to test buttons out)

Simple::Simple(const wxString& title, int x, int y)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(x,y))
{

    wxPanel  *testPanel  = new wxPanel(this,wxID_ANY, wxDefaultPosition,wxSize(270, 150));
    wxButton *testButton = new wxButton(testPanel, wxID_EXIT, wxT("Actors"), wxPoint(20,20));
    Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(Simple::eventWindowCall));
    testButton->SetFocus();

    Centre();
}

void Simple::eventWindowCall(wxCommandEvent & WXUNUSED(event))
{
    Close(true); //just a line to make sure this function is being called
}

我已经尝试在使用连接时使用数字代替事件 ID,但是如果我这样做,则不会调用按钮的功能.理想情况下,我可以做一些类似 wxCommandEventHandler(Simple::eventWindowCall(26)) 的事情,并在 eventWindowCall 函数中放置一个 case 语句,它会根据传递的数字显示正确的窗口.但到目前为止,这种方法也是无效的.

I've already tried using a number in place of an event ID when using connect, but the button's function doesn't get called if I do that. Ideally, I could just do something like wxCommandEventHandler(Simple::eventWindowCall(26)) and put a case statement in the eventWindowCall function that would show the correct window based on the number pased. But so far, that aproach has also been inefective.

您能提供的任何建议都会很棒.感谢您阅读本文.几个小时以来,我一直在努力解决这个问题.

Any advice you can offer would be great. Thanks for reading this. I've been working on figuring this out for hours.

推荐答案

int wxEvent::GetId() const

返回与此事件关联的标识符,例如按钮命令 ID.

Returns the identifier associated with this event, such as a button command id.

您可以从传递给函数的wxCommandEvent中获取与事件相关的控件ID:

You can get the control ID related to the event from the wxCommandEvent that is passed to the function:

void eventWindowCall(wxCommandEvent& event) {
    event.GetId(); // <-
}

这将为您提供被按下按钮的 ID,在您的情况下为 wxID_EXIT(因为这是您分配给按钮的 ID):

This will give you the ID of the button being pressed, in your case wxID_EXIT (since that is the ID you assigned to the button):

new wxButton(testPanel, wxID_EXIT
//                      ^^^^^^^^^ this will be passed as event id

参考wxCommandEvent 及其基类wxEvent 了解更多信息.

Refer to the documentation of wxCommandEvent and its base class, wxEvent for more information.

这篇关于WXWidgets 事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆