Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-04-15
2
Medium Priority
?
223 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

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.
Suggested Courses

609 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