Solved

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

Posted on 1998-04-15
2
210 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 280
How to gracefully close the c++ 11 thread? 3 122
Printing the elements of a set declared inside a map in C++. 3 58
sorting efficency of sorting algorithm 30 129
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

756 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