A: Dragging an image is a multistage process where one has to first erase the image previously drawn then move the coordinates of the image to a new position and finally redraw the image at the new position. Apart from that, one has to handle the mouse messages 'WM_LBUTTONDOWN', 'WM_MOUSEMOVE' and 'WM_LBUTTONUP'. Here are the basic steps to have the effect of dragging an image using mouse.
Data members are to be defined, which are to be used in view class, like the image as a 'CBitmap' object point of insertion of the image and pointer to the cursor point.
Now we have to identify a click inside the image and make movements when cursor is dragged while pressed.
In 'OnLButtonDown()' function (of 'WM_LBUTTONDOWN' message handler) check whether the mouse click is within the image and note the cursor position if it is.
Code:
BITMAP objBM;
bmImage.GetBitmap(&objBM);
CRect rectImage(ptImage, CSize(objBM.bmWidth, objBM.bmHeight));
if (rectImage.PtInRect(point))
{
pCursor = new CPoint;
*pCursor = point;
}
Check in 'OnMouseMove()' function (of 'WM_MOUSEMOVE' message) whether the image is being dragged (pointer to cursor not 'NULL') and update its position by calling a forced repaint.
Both the 'InvalidateRect()' calls can be combined to a single 'Invalidate()' without bothering to evaluate the rectangles to be invalidated. But this will give a flickering effect each time you move the image since the whole client area will be invalidated.
And finally, clean up the pointer to cursor position in 'OnLButtonUp()' function (of 'WM_LBUTTONUP' message).
Code:
if (pCursor)
{
delete pCursor;
pCursor = NULL;
}
This will give a smooth effect of dragging when the bitmap image is small.
One can also store the background image before a bitmap is displayed and reinsert the background image when the image is moved. Though result is smoother in this case, it needs more involved coding.
Last edited by Andreas Masur; November 4th, 2006 at 12:46 PM.
Bookmarks