Menu

EditView lost focus when click on current tab

Ivan.Zhang
2014-07-13
2014-07-14
  • Ivan.Zhang

    Ivan.Zhang - 2014-07-13

    Hi David,
    I found that the edit view will lose focus after clicked on current tab.
    The issue can be reproduced with DockTabbedMDI sample.
    When I switch to "TextView" by clicking "TextView" tab, the editview will get focus.
    And if I click on the "Textview" again, the textview will lose focus;
    I didn't find the which control grabbed the focus after the click.
    Could you help on this?
    Thank you!

     
  • David

    David - 2014-07-14

    Hi,

    CTabbedMDI uses a CTab control to select and display the various tab pages. This tab control is a child window of the TabbedMDI and it is this control which gets focus when a tab is pressed.

    The GetTab member function of CTabbedMDI is virtual. This makes it possible to override CTab used by our CTabbedMDI if we need more control over CTab's messages. Fortunately there is an easier way.

    The Tab's parent window receives a WM_PARENTNOTIFY when the tab control receives a WM_LBUTTONDOWN message. We can use that to redirect keyboard focus back to the tab's active view with the following code:

    LRESULT CMyTabbedMDI::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        UINT_PTR nIDTimer = 101;
    
        switch(uMsg)
        {
        case WM_PARENTNOTIFY:
            {
                if (LOWORD(wParam) == WM_LBUTTONDOWN)   
                    SetTimer(nIDTimer, 100, NULL);  
            }
            break;
        case WM_TIMER:
            if (wParam == nIDTimer)
            {
                KillTimer(nIDTimer);
    
                // return focus to active child when tab is pressed
                GetActiveMDIChild()->SetFocus();
            }
            break;
        }
    
        return WndProcDefault(uMsg, wParam, lParam);
    }
    

    I've submitted an update with these changes. You can use Tortoise SVN to download the updated code if you wish.

    Spyxx is a utility which ships with full versions of Visual studio. It is a very handy tool for identifying all the windows used in an application, and the messages these windows receive. Microsoft makes trial copies of visual studio available for download if you don't already have this tool.

    Best regards,
    David

     
  • Ivan.Zhang

    Ivan.Zhang - 2014-07-14

    Hi David,
    Thank you for your patient help.
    It really works.

     

Log in to post a comment.