wxWidgets - 创建可移动的无边框

时间:2011-02-22 18:51:53

标签: c++ wxwidgets panel movable

我正在尝试将我用C#编写的一个应用程序--Windows Forms转换为C ++ - wxWidgets。

我的应用程序是无边框的,并且在表单顶部有一个薄的透明面板,可用于移动表单。 (我使用了这个问题的技巧:Make a borderless form movable?

现在,我基本上想在wxWidgets中做同样的事情,我在互联网上搜索了如何通过wxPanel处理鼠标按下事件,并找到了几个例子但是在他们的文章/问题中都使用了wxPython而我根本不了解Python。

那么如何在C ++中做同样的事情 - wxWidgets?

2 个答案:

答案 0 :(得分:1)

一种方法是,窗口为其子窗口的每个鼠标按下事件注册一个事件处理程序。这样,如果满足特定条件(例如,单击时按住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

答案 1 :(得分:0)

“如何触发鼠标按下事件?”。您无需担心“触发”事件 - 操作系统会这样做。您需要处理EVT_LEFT_DOWN事件。您对如何处理wxWidgets事件有疑问吗?你看过示例程序了吗? http://docs.wxwidgets.org/2.6/wx_samples.html他们都是C ++。

此处有关于如何处理事件的说明:http://docs.wxwidgets.org/2.6/wx_eventhandlingoverview.html#eventhandlingoverview

如果您对处理EVT_LEFT_DOWN事件的详细信息有疑问,请发布您的代码,描述您希望它做什么以及它做什么。

相关问题