一种方法是让窗口为其每个子窗口的鼠标按下事件注册一个事件处理程序。这样,如果满足特定条件(例如单击时按住 Alt 键),窗口就可以控制鼠标。
wxwidgets\samples\shaped\shaped.cpp 示例中说明了其中一些内容,但基本上你可以这样做:
在您的窗口中添加一个方法,在所有子窗口都被添加之后:
void MyFrame::BindChildEvents()
{
visit_recursively(this,
[] (wxWindow *window, MyFrame *thiz) {
// Bind all but the main window's event
if(window != thiz)
{
window->Bind(wxEVT_LEFT_DOWN, &MyFrame::OnChildLeftDown, thiz);
}
},
this
);
}
您可以滚动自己的窗口树遍历,但我在这里使用了这个小辅助函数:
template<typename F, typename... Args>
void
visit_recursively(wxWindow *window, F func, Args&&... args)
{
for(auto&& child : window->GetChildren())
{
visit_recursively(child, func, std::forward<Args>(args)...);
}
func(window, std::forward<Args>(args)...);
}
然后你设置你的鼠标按下事件拦截处理程序:
void MyFrame::OnChildLeftDown(wxMouseEvent& event)
{
// If Alt is pressed while clicking the child window start dragging the window
if(event.GetModifiers() == wxMOD_ALT)
{
// Capture the mouse, i.e. redirect mouse events to the MyFrame instead of the
// child that was clicked.
CaptureMouse();
const auto eventSource = static_cast<wxWindow *>(event.GetEventObject());
const auto screenPosClicked = eventSource->ClientToScreen(event.GetPosition());
const auto origin = GetPosition();
mouseDownPos_ = screenPosClicked - origin;
}
else
{
// Do nothing, i.e. pass the event on to the child window
event.Skip();
}
}
您可以通过与鼠标一起移动窗口来处理鼠标运动:
void MyFrame::OnMouseMove(wxMouseEvent& event)
{
if(event.Dragging() && event.LeftIsDown())
{
const auto screenPosCurrent = ClientToScreen(event.GetPosition());
Move(screenPosCurrent - mouseDownPos_);
}
}
请务必在wxEVT_LEFT_UP 和wxEVT_MOUSE_CAPTURE_LOST 事件中调用ReleaseMouse()。