Hiren Dabhi
Hiren Dabhi

Reputation: 3713

how to drag graphics rectangle through mouse left key in c or c++

How to move graphics rectangle using mouse in c or c++. This is some what similar to movable message box in windows. how i do like this in c/c++ graphics application? THANKS in Advance...

Upvotes: 1

Views: 1805

Answers (2)

Moo-Juice
Moo-Juice

Reputation: 38820

To follow on from Alexander's answer to move it (this is pseudo code as I am not sure what platform you are on)

Point m_ptOld;
bool  m_bLDown;

void handle_onLeftMouseDown(const Point& pt)
{
    m_ptOld = pt;
    m_bLDown = true;
}

void handle_onLeftMouseUp(const Point& pt)
{
    m_bLDown = false;
}

void handle_onMouseMove(const Point& pt)
{
    if(m_bLDown)
    {
        Point ptNew = pt;
        Size delta(ptNew - ptOld);

        // Move your rectangle by Size.cx, Size.cy
    }
}

EDIT: Realised I hadn't showed code which interacted with the mouse button. Once again, no idea of platform so take with a grain of salt. I am aware that in Win32 mouse events you can find out if the left/right/middle button is down as part of the event handler. This is purely theoretical.

Upvotes: 2

Alexander Rafferty
Alexander Rafferty

Reputation: 6233

Pick your platform... Windows, Mac, Linux or the others.
Pick an API... GDI, DirectX, OpenGL

On windows, mouse coords are found using GetCurPos() in the winAPI.

In openGl, drawing a rectangle involves glBegin(). GlEnd() and the calls that go in the middle.

In GDI, Rectangle() should do the trick.

Upvotes: 2

Related Questions