Head Geek的解决方案对于我使用的wxWidgets 3.0.2无效,但我找到了另一种方法:使用Bind
在被点击控件所在的wxFrame
上临时注册一个wxEVT_MENU_HIGHLIGHT
事件处理程序。完整示例:
class FunctionMenuWindow : public wxWindow
{
public:
int option;
FunctionMenuWindow(wxWindow *parent) : wxWindow(parent, -1), option(0), mPushed(false)
{
this ->Bind(wxEVT_MENU , &FunctionMenuWindow::OnMenu , this, wxID_ANY);
theMainWin->Bind(wxEVT_MENU_OPEN , &FunctionMenuWindow::OnOpen , this, wxID_ANY);
theMainWin->Bind(wxEVT_MENU_CLOSE , &FunctionMenuWindow::OnClose , this, wxID_ANY);
theMainWin->Bind(wxEVT_MENU_HIGHLIGHT, &FunctionMenuWindow::OnHighlight, this, wxID_ANY);
}
~FunctionMenuWindow()
{
theMainWin->Unbind(wxEVT_MENU_OPEN , &FunctionMenuWindow::OnOpen , this, wxID_ANY);
theMainWin->Unbind(wxEVT_MENU_CLOSE , &FunctionMenuWindow::OnClose , this, wxID_ANY);
theMainWin->Unbind(wxEVT_MENU_HIGHLIGHT, &FunctionMenuWindow::OnHighlight, this, wxID_ANY);
}
private:
void OnMenu(wxCommandEvent& event)
{
option = event.GetId();
}
void OnOpen(wxMenuEvent &evt)
{
this->mMenu = evt.GetMenu();
if (!mPushed)
{
theMainWin->GetStatusBar()->PushStatusText(wxString());
mPushed = true;
}
}
void OnClose(wxMenuEvent &evt)
{
if (mPushed)
{
theMainWin->GetStatusBar()->PopStatusText();
mPushed = false;
}
}
const wxString GetHelpString(wxMenuEvent &evt)
{
if (evt.GetMenuId() < 0)
return wxString();
else
return mMenu->GetHelpString(evt.GetMenuId());
}
void OnHighlight(wxMenuEvent &evt)
{
if (mPushed)
theMainWin->GetStatusBar()->SetStatusText(GetHelpString(evt));
else
{
theMainWin->GetStatusBar()->PushStatusText(GetHelpString(evt));
mPushed = true;
}
}
bool mPushed;
wxMenu* mMenu;
};
void ShowMenu()
{
FunctionMenuWindow funcWindow(theMainWin);
wxMenu *menu = new wxMenu;
funcWindow.PopupMenu(menu);
switch(funcWindow.option)
{
}
}