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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds