Code: Select all
OnLeftDown(wxMouseEvent& event);
wxmenuBar* menuBar = new wxMenuBar();
menuBar->Bind(wxEVT_LEFT_DOWN,&MyFrame::OnLeftDown,this);
void MyFrame::OnLeftDown(wxMouseEvent& event)
{
// do something here
}
Code: Select all
OnLeftDown(wxMouseEvent& event);
wxmenuBar* menuBar = new wxMenuBar();
menuBar->Bind(wxEVT_LEFT_DOWN,&MyFrame::OnLeftDown,this);
void MyFrame::OnLeftDown(wxMouseEvent& event)
{
// do something here
}
Code: Select all
#include <iostream>
#include <wx/wx.h>
using namespace std;
class MyFrame : public wxFrame {
public:
MyFrame();
private:
void OnLeftDown(wxMouseEvent& event);
} *frame;
MyFrame::MyFrame() :
wxFrame(NULL, wxID_ANY, _T("Testing Mouse Event")) {
wxMenuBar* menuBar = new wxMenuBar();
menuBar->Append(new wxMenu(), _T("File"));
SetMenuBar(menuBar);
// bind to wxMenuBar
menuBar->Bind(wxEVT_LEFT_DOWN, &MyFrame::OnLeftDown, this);
// bind to wxFrame
Bind(wxEVT_LEFT_DOWN, &MyFrame::OnLeftDown, this);
// test OnLeftDown method at initialization
wxPostEvent(menuBar, wxMouseEvent(wxEVT_LEFT_DOWN));
}
void MyFrame::OnLeftDown(wxMouseEvent& event) {
cout << "Left mouse down event\n";
}
class MyApp : public wxApp {
public:
virtual bool OnInit();
} *app;
bool MyApp::OnInit() {
frame = new MyFrame();
SetTopWindow(frame);
frame->Show(true);
return true;
}
int main(int argc, char** argv) {
cout << "Starting test ...\n";
app = new MyApp();
wxApp::SetInstance(app);
wxEntry(argc, argv);
return 0;
}
There is not much you can do wrong here, and i tested it myself. Apparently the answer is no, at least under Windows. I didn't test on other platforms.Is it possible the menuBar react to the EVT_LEFT_DOWN mouse event?
Code: Select all
#include <wx/wx.h>
class MyFrame : public wxFrame
{
public:
MyFrame() : wxFrame(NULL, wxID_ANY, _("Test"), wxDefaultPosition, wxSize(800, 600))
{
wxMenu* fileMenu = new wxMenu;
fileMenu->Append(wxID_OPEN);
fileMenu->Append(wxID_SAVE);
wxMenu* editMenu = new wxMenu;
editMenu->Append(wxID_CUT);
editMenu->Append(wxID_COPY);
wxMenuBar* menuBar = new wxMenuBar();
menuBar->Append(fileMenu, _("&File"));
menuBar->Append(editMenu, _("&Edit"));
SetMenuBar(menuBar);
wxTextCtrl* logCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
wxLog::SetActiveTarget(new wxLogTextCtrl(logCtrl));
}
#ifdef __WXMSW__
WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) override
{
WXLRESULT result = wxFrame::MSWWindowProc(nMsg, wParam, lParam);
if ( nMsg == WM_NCHITTEST && result == HTMENU && GetMenuBar() )
{
size_t menuCount = GetMenuBar()->GetMenuCount();
if ( menuCount )
{
static const LONG gap = 30; // between last menu item and draggable area
RECT menuItemRect{0};
POINT mousePos{0};
::GetMenuItemRect(GetHandle(), GetMenuBar()->GetHMenu(), menuCount - 1, &menuItemRect);
::MapWindowPoints(nullptr, GetHandle(), (LPPOINT)&menuItemRect, 2);
mousePos.x = LOWORD(lParam);
mousePos.y = HIWORD(lParam);
::ScreenToClient(GetHandle(), &mousePos);
if ( mousePos.x > menuItemRect.right + gap )
{
wxLogMessage("Pretending this part of menubar is caption.");
result = HTCAPTION;
}
}
}
return result;
}
#endif // #ifdef __WXMSW__
};
class MyApp : public wxApp
{
public:
bool OnInit() override
{
(new MyFrame)->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);