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

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds