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

  • On-Demand Webinar Your developers are shifting to a DevOps delivery model to increase the speed of delivering new code and features to your customers. But as a security and IT practitioner, you want security-first, enterprise-wide solutions that support your development teams without compromising on security. During this session, we discuss why it is important to: Secure and manage secrets used by machines (e.g. micro-services, applications, scripts, CI/CD tools, hosts, etc.) and privileged users …

  • Contact center infrastructure vendors continue to integrate their multichannel capabilities to develop ''omnichannel'' solutions, while enhancing their cloud delivery capabilities. Gartner Magic Quadrant reports evaluate contact center solution providers based on completeness of vision and ability to execute. As you are looking to modernize your existing on premises contact center solution or considering cloud as an alternative, check out these Gartner Reports for strengths and cautions to consider when …

Most Popular Programming Stories

More for Developers

RSS Feeds

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