一种方法是让窗口为其每个子窗口的鼠标按下事件注册一个事件处理程序。这样,如果满足某个条件(例如单击时按住 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_);
}
}
一定要调用和ReleaseMouse()
事件。wxEVT_LEFT_UP
wxEVT_MOUSE_CAPTURE_LOST