How to make widget movable by mouse the best way

May 17, 2013 at 2:03 PM
Hi Jinhao!
I tried to make widget movable by mouse.
To do it I added to this widget event handlers for mouse_down,mouse_move and mouse_up events.But it was found that if the mouse moved quickly, the cursor would get out of the widget.
To fix it I added handlers also to widget's parent.But I think it's a bit ugly.
So,maybe there is a better way?

Thank you.
Coordinator
May 17, 2013 at 3:06 PM
Grab the cursor for the button by calling API::capture_window(button, true) in mouse_down, and then release the grab window by calling API::capture_window(button, false) in mouse_up. But the best way to move a widget is to employ the class dragger, E.g
#include <nana/gui/wvl.hpp>
#include <nana/gui/button.hpp>
#include <nana/gui/dragger.hpp>

int main()
{
    using namespace nana::gui;

    form fm;
    button btn(fm, nana::rectangle(10, 10, 100, 20));
    btn.caption(STR("Drag Me"));
    
    dragger dg;
    dg.trigger(btn);  //When you drag the btn, then
    dg.target(btn);   //move the btn

    fm.show();
    exec();
}
You can find the class dragger in the documentation at "3.3.8 Components - dragger", but the doc is a bit out of date, the method target() in doc is drag_target().
May 17, 2013 at 3:12 PM
Thank you very much.