Solved

VC++5.0 How to: Drag a picture box in view

Posted on 1998-04-15
2
198 Views
Last Modified: 2008-03-06
I need to know how to drag an object(in this case a picbox) on screen. I don't exactly where to start. I know the buttondown/up and mousemeove functions need to be coded. Can you provide me some general sample code for moving objects around?  
0
Comment
Question by:englm
  • 2
2 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
Comment Utility
you respond to the WM_LBUTTONDOWN by checking if you are over the object you want to drag.  If so, set a pointer member to the object you are dragging, and remember the mouse position in another member variable

you respond to the WM_MOUSEMOVE by first checking if the pointer variable is currently pointing to an object to move, if not then don't do anything.  Otherwise invalidate the rect where the object was; move the object pointed to the same amount in x and y as the difference between the last mouse position and the new one; save the new mouse position; invalidate the rect where the object is now

you respond to the WM_LBUTTONUP by doing the same as for the move, but then NULLing the pointer variable so future mouse move events won't move it again.

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
here is some code...

assume we have...

CMyBox* m_pbox; // pointer to a pic box object being dragged
CPoint m_mouseat; // mouse position
CMyBox* CMyView::FindBoxContaining(CPoint point) const;
// finds the box containing the point (if any)
void CBox::InvalidateBox(CMyView* pview);
// invalidates a rect in the view corresponding
// to the bounding rect of the box

then in your view class...

BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CMyView::InvalidateBox(CBox* pbox) {
  pBox->InvalidateBox(this);
}

void CMyView::OnLButtonDown(UINT nFlags, CPoint point) {
  m_pbox = FindBoxContaining(point);
  m_mouseat = point;
}

void CMyView::OnMouseMove(UINT nFlags, CPoint point) {
  if (! m_pbox) return;
  InvalidateBox(m_pbox);
  CSize move = point - m_mouseat;
  m_pBox->MoveBy(move);
  m_mouseat = point;
}

void CMyView::OnLButtonUp(UINT nFlags, CPoint point) {
  if (! m_pbox) return;
  OnMouseMove(nFlags,point);
  m_pbox = NULL;
}

hope this helps

You may want to set a flag in your CBox object that indicates it is selected for dragging, then you can change the way the box draws (eg difference color) so that you can get some visual feedback

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now