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;
		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.


  • 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

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

Top White Papers and Webcasts

  • Agencies that integrate back-office processed with cloud-based services, advanced analytics and talent management solutions can reduce costs, maximize efficiencies and transform employee recruitment and retention. Learn more by reading this interview brief by GovLoop with Infor industry thought leaders.

  • Cloud has the potential to offer many benefits that can enable great success within your business. However, there are still many myths floating around about backing up to the cloud. In this eBook, you'll discover the truth about five of the most common cloud myths, including myths about security, maintaining regulatory compliance and more. Get to the truth, so you can backup to the cloud with confidence.

Most Popular Programming Stories

More for Developers

RSS Feeds

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