-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.

Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
Harsh
  • 83
  • 3
  • 10

2 Answers2

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

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
-1

[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

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 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