?
Solved

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

Posted on 1998-04-15
2
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

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