CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    Sorry, now I can't show you the entire of my code.
    But I'll prepare test code as possible.

    An exception that occurs when I close documents arbitrary after activate one view. Exception messages are like below

    'A deactivating object is not a recently activated object'

  2. #17
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    Quote Originally Posted by lightshield View Post
    Sorry, now I can't show you the entire of my code.
    But I'll prepare test code as possible.

    An exception that occurs when I close documents arbitrary after activate one view. Exception messages are like below

    'A deactivating object is not a recently activated object'
    Did the call work otherwise?

  3. #18
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    An exception message is like below

    'A deactivating object is not a recently activated object.'

    GCDEF, please show the tail of my first post. thanks

  4. #19
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    Sorry for my duplicated post.

    What about 'the call' and what means 'work' ?

  5. #20
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    I've made the VC solution to repro this problem.
    I'll upload files to some server soon.

    And I fix my words that the exception occurs when I close 'the application', not a document.

  6. #21
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    https://skydrive.live.com/?cid=578e0...8337%21103

    An above link is for the repro program and download 'mditest.zip'.
    1. build the solution
    2. run the mditest.exe and create at least 2 documents.
    3. close the application from 'X' button -> exception will be invoked.

    Please try it.

  7. #22
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    At a quick glance you're switching the view in the child frame instead of activating the correct frame. I'm not sure why you're even doing that. If you comment out the call to ForceActivate the program behaves the same way. Use the MDIActivate function and pass in a pointer to a child frame as I said instead of switching the view in the active frame.

  8. #23
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    I'd already tried ways that you said but I couldn't emulate 'click on a tab' behavior.
    I've known that above code is incorrect. I just want to know ways for 'click on a tab' behavior.
    MDIActivate method does not work this purpose by calling this method simply.

    In other words, make the procedure of OnActivateView same as tab-click.
    But I found a strange thing that existing documents will be deactivated when the new document created. This may be same behavior as tab-click.
    Therefor, I couldn't get same result. fmmm

    I got the another way to avoid this problem now. But It's difficult problem to emulate the tab-click behavior.
    Last edited by lightshield; August 15th, 2011 at 11:08 PM.

  9. #24
    Join Date
    Aug 2011
    Posts
    38

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    I've solved this problem by enumerating documents from document templates and proceed desired behavior.
    It's a safe way to synchronize between each view.



    GCDEF, thank you for this log conversation.
    Last edited by lightshield; August 16th, 2011 at 04:21 PM.

  10. #25
    Join Date
    Jan 2015
    Posts
    2

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    try this code:

    POSITION pos = GetMDITabGroups().GetHeadPosition();

    while(pos){
    const CObject* obj = GetMDITabGroups().GetNext(pos);
    CMFCTabCtrl* tab = (CMFCTabCtrl*)obj;
    int count = tab->GetWindowedChildCount();
    for(int i=0; i<count; i++){
    tab->ActivateMDITab(i);
    if(openedScenario==GetActiveView()){
    break;
    }

    }

    }

  11. #26
    Join Date
    Jan 2015
    Posts
    2

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    Quote Originally Posted by wootra View Post
    try this code:

    POSITION pos = GetMDITabGroups().GetHeadPosition();

    while(pos){
    const CObject* obj = GetMDITabGroups().GetNext(pos);
    CMFCTabCtrl* tab = (CMFCTabCtrl*)obj;
    int count = tab->GetWindowedChildCount();
    for(int i=0; i<count; i++){
    tab->ActivateMDITab(i);
    if(openedScenario==GetActiveView()){
    break;
    }

    }

    }
    the openedScenario is the View you wanted to activate.

    you can know what view you want to active in the other way.

    I had same problem, but I couldn't find the way to solve this problem. So I leave my solution here.

  12. #27
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: [MFC MDI] How to activate a view programmatically and safely ?

    An MDI application with "tabbed views" can have zero, one or more tab groups.
    Here is an example of one having two tab groups.

    Tabed-view MDI application having two tab groups.jpg

    If you want to programmatically activate a tab item (and its associated view) from a given tab group you have to do the following:
    1. call CMDIFrameWndEx::GetMDITabGroups to get a list of tab groups (this is a list of CMFCTabCtrl pointers, one for each tab group);
    2. get the desired tab group from the list;
    3. call CMFCTabCtrl::ActivateMDITab to activate the desired tab item (and the associated view).

    And here is a sample code:
    Code:
    BOOL CMainFrame::_ActivateTab(INT_PTR nTabGroupIndex, int nTabItemIndex /*= -1*/)
    {
        BOOL bRet = FALSE;
        const CObList& list = GetMDITabGroups();
        POSITION pos = list.FindIndex(nTabGroupIndex);
        if(NULL != pos)
        {
            const CObject* pTabGroup = list.GetAt(pos);
            VERIFY(pTabGroup->IsKindOf(RUNTIME_CLASS(CMFCTabCtrl)));
            if(NULL != pTabGroup)
            {
                CMFCTabCtrl* pTabCtrl = (CMFCTabCtrl*)pTabGroup;
                if(pTabCtrl->GetTabsNum() > nTabItemIndex)
                {
                    pTabCtrl->ActivateMDITab(nTabItemIndex);
                    bRet = TRUE;
                }
            }
        }
        return bRet;
    }
    Code:
    void CMainFrame::OnSomeCommand()
    {
        // just for demo purpose
        // activate the third tabbed view from the first tab group
        VERIFY(_ActivateTab(0, 2));
    }
    Last edited by ovidiucucu; January 15th, 2015 at 08:57 AM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured