Solved

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

Posted on 1998-04-15
2
208 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
ID: 1184311
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
ID: 1184312
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
Should CArray be used for a list of pointers in C++? 19 118
Gaming Software 1 19
Adding items to a C# list incrementally 5 33
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

837 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