2
votes

We use in several place a CTreeCtrl (TreeView) and accept a double click to open dialogs related to the double clicked node.

The opened dialog looses the focus after being opened, since the tree view seems to force to be focussed at the end of the double click handling.

Our scenario:

  1. the user double clicks onto a node
  2. the tree view gets focused and selects an item in its tree
  3. the tree view containing window receives the NM_DBLCLK notification for the tree view and reacts on the double click by opening a dialog or a MDI child window in our MDI environment
  4. the opened dialog/MDI child window gets focused after being opened
  5. the tree view gets focused again

Even if we use in (3) (the notification handler) the result field returning a non zero value to prevent the rest of the default handling, (5) happens and the tree view gets focused again, the item selected again.

I'd really appreciate any hint about a way to resolve this issue, since it is really annoying, that a just opened dialog or window looses its focus right after opening.

Thanks in advance!

1
How are you 'intercepting' the double-click: override of OnNotify or override of OnLButtonDblClk? I use the latter for a similar operation (opening a node-dependent dialog) and it works OK.Adrian Mole

1 Answers

2
votes

This behavior won't occur if you create a modal dialog box, because parent window is immediately disabled and dialog gains focus. But with mode-less dialog, flicker may occur, and dialog loses focus.

For mode-less dialog, use PostMessage or SetTimer so that the mode-less dialog is opened after TreeView message is processed. Example:

#define WM_USER_MSG1 WM_USER + 1

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
    ON_MESSAGE(WM_USER_MSG1, create_dialog)
    ...
END_MESSAGE_MAP()

void CMyWnd::OnDblClick(NMHDR*, LRESULT* pResult)
{
    PostMessage(WM_USER_MSG1, 0, 0);
    *pResult = 0;
}

LRESULT CMyWnd::create_dialog(WPARAM, LPARAM)
{
    if(!m_dlg.GetSafeHwnd())
        m_dlg.Create(IDD_DIALOG_X, this);
    m_dlg.ShowWindow(SW_SHOW);
    return 0;
}