开发者

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

开发者 https://www.devze.com 2023-01-24 07:23 出处:网络
How to move graphics rectangle using mouse in c or c++. This is some what similar to movable message box in windows.

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? THANK开发者_如何学CS in Advance...


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消