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
BOOL CTreeCtrlX::PreTranslateMessage(MSG* pMsg)
if( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE
m_bLDragging = 0;
return TRUE; // DO NOT process further
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.