Using ESC to cancel drag and drop


To escape out of a drag and drop operation we can look for the ESCAPE key in the PreTranslateMessage() and terminate the operation without moving any item..
 

BOOL CTreeCtrlX::PreTranslateMessage(MSG* pMsg)
{
	if( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE 
					&& m_bLDragging)
	{
		m_bLDragging = 0;
		CImageList::DragLeave(NULL);
		CImageList::EndDrag();
		ReleaseCapture();
		SelectDropTarget(NULL);
		delete m_pDragImage;
		return TRUE;		// DO NOT process further
	}

	return CTreeCtrl::PreTranslateMessage(pMsg);
}

There is one more thing you need to for this to work properly - call SetFocus() in your WM_LBUTTONDOWN handler. If the control doesn't already have focus when the user begins the drag and drop operation, then the control doesn't receive focus and if the control doesn't have the focus, it doesn't receive the WM_KEYDOWN message.



Comments

  • ignore other keys

    Posted by Legacy on 04/22/2002 12:00am

    Originally posted by: real name

    can very useful to ignore all other keys (or maybe left drag with them too) to not get unwanted situations like ctrl-a/del handling during operation
    t!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Featuring Art Schoeller, VP and Principal Analyst, Forrester Research Wednesday, August 31, 2016 10:00 AM PT | 1 PM ET If Agility Is Essential to Your Business Survival--Now's the Time to Start the Move to Cloud! The maturity of cloud platforms has given organizations new confidence in moving mission-critical systems to the cloud, to gain agility, scale and realize cost benefits in the process. How can your company achieve these benefits, and what steps are necessary to begin your contact center's …

  • The future of cloud platforms is at hand. Even if your cloud applications are basic now, your next set of apps will require strong analytics services and tools, as well as features that ease enterprise integration. To fill these needs, consider using not only your current cloud provider but also specialists. Take advantage of not only the big cloud platforms, but also specialized providers in vertical industries; countries and regions; and functional domains including omnichannel, analytics, integration, and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date