How to find the active child form of MDI parent from User control which is loaded in MDI Parent at runtime in...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-3















IDE: Microsoft Visual Studio



Platform: C#.net



Hi, I am trying to get the instance of active MDI child of MDI parent from user control which is loaded at runtime.










share|improve this question

























  • Try property ActiveMdiChild of class Form

    – okrumnow
    Jan 24 '14 at 10:29


















-3















IDE: Microsoft Visual Studio



Platform: C#.net



Hi, I am trying to get the instance of active MDI child of MDI parent from user control which is loaded at runtime.










share|improve this question

























  • Try property ActiveMdiChild of class Form

    – okrumnow
    Jan 24 '14 at 10:29














-3












-3








-3








IDE: Microsoft Visual Studio



Platform: C#.net



Hi, I am trying to get the instance of active MDI child of MDI parent from user control which is loaded at runtime.










share|improve this question
















IDE: Microsoft Visual Studio



Platform: C#.net



Hi, I am trying to get the instance of active MDI child of MDI parent from user control which is loaded at runtime.







c# .net winforms visual-studio-2010






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 29 '15 at 8:56









Cary Bondoc

1,87112744




1,87112744










asked Jan 24 '14 at 10:24









HarshHarsh

2529




2529













  • Try property ActiveMdiChild of class Form

    – okrumnow
    Jan 24 '14 at 10:29



















  • Try property ActiveMdiChild of class Form

    – okrumnow
    Jan 24 '14 at 10:29

















Try property ActiveMdiChild of class Form

– okrumnow
Jan 24 '14 at 10:29





Try property ActiveMdiChild of class Form

– okrumnow
Jan 24 '14 at 10:29












2 Answers
2






active

oldest

votes


















2














From anywhere within your MDI parent form's code, you can use:



Form activeChild = this.ActiveMdiChild;


That will give you the form instance of the currently focused (activated) child form. It will change each time a new child form is opened, or when the user clicks (focuses) a different child.



The MSDN page






share|improve this answer































    -2














    [edit]



    The answer was based on assumption that source code was not available which was not the case but that was revealed only after. I still think this can be help full to others in such case ..



    That is hard but doable.





    1. list all window handles which are children of your parent



      even non direct descendants !!! because your form can be docked to some subcomponent of window ...




    2. filter out handles which are not windows



      you will get many handles > 100 because every visual component is also handle (like buttons,scrollbars,...)




    now the hard part:




    1. if you know the class name of searched window then just check any handle for it



    2. if not then you have to do this window search time to time



      and search for changes if any new window pop up then it is most probably your new MDI child but you must filter out windows like open/save file/image/printer dialogs, ...




    have been doing similar task in the past and this is what I come up with:



    //---------------------------------------------------------------------------
    //--- Windows ver: 1.1 ------------------------------------------------------
    //---------------------------------------------------------------------------
    HWND getwindow(HWND hnd0,AnsiString nam,AnsiString cls="")
    {
    int i,l,ln=nam.Length(),lc=cls.Length(),e;
    char txt[256];
    HWND hnd1;
    if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
    else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
    for (;;)
    {
    e=1;
    if (hnd1==hnd0) break;
    if (hnd1==NULL) break;
    l=GetWindowText(hnd1,txt,256);
    if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
    l=RealGetWindowClass(hnd1,txt,256);
    if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
    if (e) return hnd1;
    hnd0=hnd1;
    hnd1=GetWindow(hnd0,GW_HWNDNEXT);
    }
    return NULL;
    };
    //---------------------------------------------------------------------------
    HWND getsubwindow(HWND hndp,HWND hnd0,AnsiString nam,AnsiString cls="")
    {
    int i,l,ln=nam.Length(),lc=cls.Length(),e;
    char txt[256];
    HWND hnd1;
    if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
    else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
    for (;;)
    {
    e=1;
    if (hnd1==hnd0) break;
    if (hnd1==NULL) break;
    if (GetParent(hnd1)!=hndp) e=0;
    l=GetWindowText(hnd1,txt,256);
    if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
    l=RealGetWindowClass(hnd1,txt,256);
    if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
    if (e) return hnd1;
    hnd0=hnd1;
    hnd1=GetWindow(hnd0,GW_HWNDNEXT);
    }
    return NULL;
    };
    //---------------------------------------------------------------------------
    bool getwindows(HWND &hnd,AnsiString &nam,AnsiString &cls)
    {
    int i,l;
    char txt[256];
    HWND hnd0=hnd;
    nam=""; cls="";
    if (hnd0==NULL) hnd=GetTopWindow(hnd0);
    else hnd=GetWindow(hnd0,GW_HWNDNEXT);
    if (hnd==hnd0) { hnd=NULL; return false; }
    if (hnd==NULL) { hnd=NULL; return false; }
    l=GetWindowText(hnd,txt,256); for (i=0;i<l;i++) nam+=txt[i];
    l=RealGetWindowClass(hnd,txt,256); for (i=0;i<l;i++) cls+=txt[i];
    return true;
    };
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------


    It is written in BDS2006 Turbo C++ and use VCL so you need to convert it to your Language. Actually it uses only AnsiString from VCL so just change it to any string you have. It is WinAPI based so include "Windows.h"



    Here is some example of ussage:



    HANDLE hnd,hnd0;
    AnsiString nam,cls;
    for (hnd=NULL;;)
    {
    if (!getwindows(hnd,nam,cls)) break; // get hnd,name and class
    hnd0=GetParent(hnd); // get parent hnd
    // here process found window or add it to list or what ever
    }


    Also do not forget to check if handle is valid time to time because meantime windows can be closed ...



    PS. I forget to mention this works even if you accessing not your own windows but another process/exe which is what I needed.



    Hope it helps






    share|improve this answer


























    • Thanks for the code ... but it is beyond my reach :)

      – Harsh
      Jan 24 '14 at 12:30











    • I think you missed the point of the question.

      – DonBoitnott
      Jan 24 '14 at 13:06











    • may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

      – Spektre
      Jan 24 '14 at 13:16














    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21330158%2fhow-to-find-the-active-child-form-of-mdi-parent-from-user-control-which-is-loade%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    From anywhere within your MDI parent form's code, you can use:



    Form activeChild = this.ActiveMdiChild;


    That will give you the form instance of the currently focused (activated) child form. It will change each time a new child form is opened, or when the user clicks (focuses) a different child.



    The MSDN page






    share|improve this answer




























      2














      From anywhere within your MDI parent form's code, you can use:



      Form activeChild = this.ActiveMdiChild;


      That will give you the form instance of the currently focused (activated) child form. It will change each time a new child form is opened, or when the user clicks (focuses) a different child.



      The MSDN page






      share|improve this answer


























        2












        2








        2







        From anywhere within your MDI parent form's code, you can use:



        Form activeChild = this.ActiveMdiChild;


        That will give you the form instance of the currently focused (activated) child form. It will change each time a new child form is opened, or when the user clicks (focuses) a different child.



        The MSDN page






        share|improve this answer













        From anywhere within your MDI parent form's code, you can use:



        Form activeChild = this.ActiveMdiChild;


        That will give you the form instance of the currently focused (activated) child form. It will change each time a new child form is opened, or when the user clicks (focuses) a different child.



        The MSDN page







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 24 '14 at 13:08









        DonBoitnottDonBoitnott

        7,96543658




        7,96543658

























            -2














            [edit]



            The answer was based on assumption that source code was not available which was not the case but that was revealed only after. I still think this can be help full to others in such case ..



            That is hard but doable.





            1. list all window handles which are children of your parent



              even non direct descendants !!! because your form can be docked to some subcomponent of window ...




            2. filter out handles which are not windows



              you will get many handles > 100 because every visual component is also handle (like buttons,scrollbars,...)




            now the hard part:




            1. if you know the class name of searched window then just check any handle for it



            2. if not then you have to do this window search time to time



              and search for changes if any new window pop up then it is most probably your new MDI child but you must filter out windows like open/save file/image/printer dialogs, ...




            have been doing similar task in the past and this is what I come up with:



            //---------------------------------------------------------------------------
            //--- Windows ver: 1.1 ------------------------------------------------------
            //---------------------------------------------------------------------------
            HWND getwindow(HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            HWND getsubwindow(HWND hndp,HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            if (GetParent(hnd1)!=hndp) e=0;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            bool getwindows(HWND &hnd,AnsiString &nam,AnsiString &cls)
            {
            int i,l;
            char txt[256];
            HWND hnd0=hnd;
            nam=""; cls="";
            if (hnd0==NULL) hnd=GetTopWindow(hnd0);
            else hnd=GetWindow(hnd0,GW_HWNDNEXT);
            if (hnd==hnd0) { hnd=NULL; return false; }
            if (hnd==NULL) { hnd=NULL; return false; }
            l=GetWindowText(hnd,txt,256); for (i=0;i<l;i++) nam+=txt[i];
            l=RealGetWindowClass(hnd,txt,256); for (i=0;i<l;i++) cls+=txt[i];
            return true;
            };
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------


            It is written in BDS2006 Turbo C++ and use VCL so you need to convert it to your Language. Actually it uses only AnsiString from VCL so just change it to any string you have. It is WinAPI based so include "Windows.h"



            Here is some example of ussage:



            HANDLE hnd,hnd0;
            AnsiString nam,cls;
            for (hnd=NULL;;)
            {
            if (!getwindows(hnd,nam,cls)) break; // get hnd,name and class
            hnd0=GetParent(hnd); // get parent hnd
            // here process found window or add it to list or what ever
            }


            Also do not forget to check if handle is valid time to time because meantime windows can be closed ...



            PS. I forget to mention this works even if you accessing not your own windows but another process/exe which is what I needed.



            Hope it helps






            share|improve this answer


























            • Thanks for the code ... but it is beyond my reach :)

              – Harsh
              Jan 24 '14 at 12:30











            • I think you missed the point of the question.

              – DonBoitnott
              Jan 24 '14 at 13:06











            • may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

              – Spektre
              Jan 24 '14 at 13:16


















            -2














            [edit]



            The answer was based on assumption that source code was not available which was not the case but that was revealed only after. I still think this can be help full to others in such case ..



            That is hard but doable.





            1. list all window handles which are children of your parent



              even non direct descendants !!! because your form can be docked to some subcomponent of window ...




            2. filter out handles which are not windows



              you will get many handles > 100 because every visual component is also handle (like buttons,scrollbars,...)




            now the hard part:




            1. if you know the class name of searched window then just check any handle for it



            2. if not then you have to do this window search time to time



              and search for changes if any new window pop up then it is most probably your new MDI child but you must filter out windows like open/save file/image/printer dialogs, ...




            have been doing similar task in the past and this is what I come up with:



            //---------------------------------------------------------------------------
            //--- Windows ver: 1.1 ------------------------------------------------------
            //---------------------------------------------------------------------------
            HWND getwindow(HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            HWND getsubwindow(HWND hndp,HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            if (GetParent(hnd1)!=hndp) e=0;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            bool getwindows(HWND &hnd,AnsiString &nam,AnsiString &cls)
            {
            int i,l;
            char txt[256];
            HWND hnd0=hnd;
            nam=""; cls="";
            if (hnd0==NULL) hnd=GetTopWindow(hnd0);
            else hnd=GetWindow(hnd0,GW_HWNDNEXT);
            if (hnd==hnd0) { hnd=NULL; return false; }
            if (hnd==NULL) { hnd=NULL; return false; }
            l=GetWindowText(hnd,txt,256); for (i=0;i<l;i++) nam+=txt[i];
            l=RealGetWindowClass(hnd,txt,256); for (i=0;i<l;i++) cls+=txt[i];
            return true;
            };
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------


            It is written in BDS2006 Turbo C++ and use VCL so you need to convert it to your Language. Actually it uses only AnsiString from VCL so just change it to any string you have. It is WinAPI based so include "Windows.h"



            Here is some example of ussage:



            HANDLE hnd,hnd0;
            AnsiString nam,cls;
            for (hnd=NULL;;)
            {
            if (!getwindows(hnd,nam,cls)) break; // get hnd,name and class
            hnd0=GetParent(hnd); // get parent hnd
            // here process found window or add it to list or what ever
            }


            Also do not forget to check if handle is valid time to time because meantime windows can be closed ...



            PS. I forget to mention this works even if you accessing not your own windows but another process/exe which is what I needed.



            Hope it helps






            share|improve this answer


























            • Thanks for the code ... but it is beyond my reach :)

              – Harsh
              Jan 24 '14 at 12:30











            • I think you missed the point of the question.

              – DonBoitnott
              Jan 24 '14 at 13:06











            • may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

              – Spektre
              Jan 24 '14 at 13:16
















            -2












            -2








            -2







            [edit]



            The answer was based on assumption that source code was not available which was not the case but that was revealed only after. I still think this can be help full to others in such case ..



            That is hard but doable.





            1. list all window handles which are children of your parent



              even non direct descendants !!! because your form can be docked to some subcomponent of window ...




            2. filter out handles which are not windows



              you will get many handles > 100 because every visual component is also handle (like buttons,scrollbars,...)




            now the hard part:




            1. if you know the class name of searched window then just check any handle for it



            2. if not then you have to do this window search time to time



              and search for changes if any new window pop up then it is most probably your new MDI child but you must filter out windows like open/save file/image/printer dialogs, ...




            have been doing similar task in the past and this is what I come up with:



            //---------------------------------------------------------------------------
            //--- Windows ver: 1.1 ------------------------------------------------------
            //---------------------------------------------------------------------------
            HWND getwindow(HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            HWND getsubwindow(HWND hndp,HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            if (GetParent(hnd1)!=hndp) e=0;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            bool getwindows(HWND &hnd,AnsiString &nam,AnsiString &cls)
            {
            int i,l;
            char txt[256];
            HWND hnd0=hnd;
            nam=""; cls="";
            if (hnd0==NULL) hnd=GetTopWindow(hnd0);
            else hnd=GetWindow(hnd0,GW_HWNDNEXT);
            if (hnd==hnd0) { hnd=NULL; return false; }
            if (hnd==NULL) { hnd=NULL; return false; }
            l=GetWindowText(hnd,txt,256); for (i=0;i<l;i++) nam+=txt[i];
            l=RealGetWindowClass(hnd,txt,256); for (i=0;i<l;i++) cls+=txt[i];
            return true;
            };
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------


            It is written in BDS2006 Turbo C++ and use VCL so you need to convert it to your Language. Actually it uses only AnsiString from VCL so just change it to any string you have. It is WinAPI based so include "Windows.h"



            Here is some example of ussage:



            HANDLE hnd,hnd0;
            AnsiString nam,cls;
            for (hnd=NULL;;)
            {
            if (!getwindows(hnd,nam,cls)) break; // get hnd,name and class
            hnd0=GetParent(hnd); // get parent hnd
            // here process found window or add it to list or what ever
            }


            Also do not forget to check if handle is valid time to time because meantime windows can be closed ...



            PS. I forget to mention this works even if you accessing not your own windows but another process/exe which is what I needed.



            Hope it helps






            share|improve this answer















            [edit]



            The answer was based on assumption that source code was not available which was not the case but that was revealed only after. I still think this can be help full to others in such case ..



            That is hard but doable.





            1. list all window handles which are children of your parent



              even non direct descendants !!! because your form can be docked to some subcomponent of window ...




            2. filter out handles which are not windows



              you will get many handles > 100 because every visual component is also handle (like buttons,scrollbars,...)




            now the hard part:




            1. if you know the class name of searched window then just check any handle for it



            2. if not then you have to do this window search time to time



              and search for changes if any new window pop up then it is most probably your new MDI child but you must filter out windows like open/save file/image/printer dialogs, ...




            have been doing similar task in the past and this is what I come up with:



            //---------------------------------------------------------------------------
            //--- Windows ver: 1.1 ------------------------------------------------------
            //---------------------------------------------------------------------------
            HWND getwindow(HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            HWND getsubwindow(HWND hndp,HWND hnd0,AnsiString nam,AnsiString cls="")
            {
            int i,l,ln=nam.Length(),lc=cls.Length(),e;
            char txt[256];
            HWND hnd1;
            if (hnd0==NULL) hnd1=GetTopWindow(hnd0);
            else hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            for (;;)
            {
            e=1;
            if (hnd1==hnd0) break;
            if (hnd1==NULL) break;
            if (GetParent(hnd1)!=hndp) e=0;
            l=GetWindowText(hnd1,txt,256);
            if (e) { if (l>ln) l=ln; if (l<ln) e=0; else for (i=0;i<l;i++) if (txt[i]!=nam[i+1]) { e=0; break; } }
            l=RealGetWindowClass(hnd1,txt,256);
            if (e) { if (l>lc) l=lc; if (l<lc) e=0; else for (i=0;i<l;i++) if (txt[i]!=cls[i+1]) { e=0; break; } }
            if (e) return hnd1;
            hnd0=hnd1;
            hnd1=GetWindow(hnd0,GW_HWNDNEXT);
            }
            return NULL;
            };
            //---------------------------------------------------------------------------
            bool getwindows(HWND &hnd,AnsiString &nam,AnsiString &cls)
            {
            int i,l;
            char txt[256];
            HWND hnd0=hnd;
            nam=""; cls="";
            if (hnd0==NULL) hnd=GetTopWindow(hnd0);
            else hnd=GetWindow(hnd0,GW_HWNDNEXT);
            if (hnd==hnd0) { hnd=NULL; return false; }
            if (hnd==NULL) { hnd=NULL; return false; }
            l=GetWindowText(hnd,txt,256); for (i=0;i<l;i++) nam+=txt[i];
            l=RealGetWindowClass(hnd,txt,256); for (i=0;i<l;i++) cls+=txt[i];
            return true;
            };
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------
            //---------------------------------------------------------------------------


            It is written in BDS2006 Turbo C++ and use VCL so you need to convert it to your Language. Actually it uses only AnsiString from VCL so just change it to any string you have. It is WinAPI based so include "Windows.h"



            Here is some example of ussage:



            HANDLE hnd,hnd0;
            AnsiString nam,cls;
            for (hnd=NULL;;)
            {
            if (!getwindows(hnd,nam,cls)) break; // get hnd,name and class
            hnd0=GetParent(hnd); // get parent hnd
            // here process found window or add it to list or what ever
            }


            Also do not forget to check if handle is valid time to time because meantime windows can be closed ...



            PS. I forget to mention this works even if you accessing not your own windows but another process/exe which is what I needed.



            Hope it helps







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 11:45

























            answered Jan 24 '14 at 10:43









            SpektreSpektre

            30.4k650220




            30.4k650220













            • Thanks for the code ... but it is beyond my reach :)

              – Harsh
              Jan 24 '14 at 12:30











            • I think you missed the point of the question.

              – DonBoitnott
              Jan 24 '14 at 13:06











            • may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

              – Spektre
              Jan 24 '14 at 13:16





















            • Thanks for the code ... but it is beyond my reach :)

              – Harsh
              Jan 24 '14 at 12:30











            • I think you missed the point of the question.

              – DonBoitnott
              Jan 24 '14 at 13:06











            • may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

              – Spektre
              Jan 24 '14 at 13:16



















            Thanks for the code ... but it is beyond my reach :)

            – Harsh
            Jan 24 '14 at 12:30





            Thanks for the code ... but it is beyond my reach :)

            – Harsh
            Jan 24 '14 at 12:30













            I think you missed the point of the question.

            – DonBoitnott
            Jan 24 '14 at 13:06





            I think you missed the point of the question.

            – DonBoitnott
            Jan 24 '14 at 13:06













            may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

            – Spektre
            Jan 24 '14 at 13:16







            may be a little ... its because I needed solution that taps into different applicatoin without changing it anyway. but if you have access to the source of MDI app then your way is better :)

            – Spektre
            Jan 24 '14 at 13:16




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21330158%2fhow-to-find-the-active-child-form-of-mdi-parent-from-user-control-which-is-loade%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma