From pliu3 at unity.ncsu.edu  Fri Jul 16 22:05:57 2004
From: pliu3 at unity.ncsu.edu (Peng Liu)
Date: Fri Jul 16 22:06:01 2004
Subject: [Rd] getWindowsHandle()
In-Reply-To: <g5qdf0tqb7dvvrdorqqlbg6ool5309p28c@4ax.com>
References: <Pine.GSO.4.58.0407151559590.14948@uni01du.unity.ncsu.edu>
	<g5qdf0tqb7dvvrdorqqlbg6ool5309p28c@4ax.com>
Message-ID: <Pine.GSO.4.58.0407161551420.12812@uni01du.unity.ncsu.edu>

On Thu, 15 Jul 2004, Duncan Murdoch wrote:

> You'll need to read Windows documentation.
> Currently R has no way to get the list of MDI document windows.  I
> think the function you want is EnumChildWindows, but you might need to
> use EnumWindows.  I can't remember whether MDI documents are child
> windows or top level windows.

Hi Dr. Murdoch,
Thanks for you advice. I checked WIN32API documents and write the
following codes. So far, the call just gives all the hwnd's of child
windows. Many window widgets are treated as child windows, such as
tool bar, shortcut buttons, workspace, and so on. Therefore, the output
can be a long list. Probably, I need to redefine the maximum number of
handles. Moreover, it does not make sense to give a list of hwnd's only. I
will add more information to the output later.

Cheers!
Peng

/*************************C codes begin*************************/

#define MAXNCHILDWINS 100
struct hwndlst
{
	HWND childhwndlst[MAXNCHILDWINS];
	int p;
};
BOOL CALLBACK EnumChildProc(HWND hwndChild, struct hwndlst * lParam)
{
	struct hwndlst * plist=lParam;
	plist->childhwndlst[(plist->p)++] = hwndChild;
	return TRUE;
}

struct hwndlst getMDIChildHandle(void)
{
	HWND hwnd;
	struct hwndlst hwndlst;
	hwndlst.p = 0;
	hwnd = GetForegroundWindow();
	EnumChildWindows(hwnd, EnumChildProc, &hwndlst);
	return(hwndlst);
}

SEXP getMDIChildhandles()
{
    SEXP result;
	int i;
	struct hwndlst lst;

    result = R_NilValue; /* to avoid warnings */

    lst = getMDIChildHandle();
    PROTECT(result = allocVector(INTSXP, MAXNCHILDWINS+1));
	INTEGER(result)[0]=(int)(lst.p);
    for (i=1; i<MAXNCHILDWINS+1; i++)
    {
	    INTEGER(result)[i] = ((int)(lst.childhwndlst)[i-1]);
    }
    UNPROTECT(1);

    return result;
}

/**********************C codes end.************************/

####################R codes begin###########################
getMDIChildhandles <- function() {
    a <- .Call("getMDIChildhandles")
    a <- a[2:(a[1])]
    return(a)
}
##################R codes end.#############################
-------------------------------------
Peng Liu
Department of Statistics
NC State University
Raleigh, NC 27695, USA
E-mail: pliu3@ncsu.edu
URL: http://www4.ncsu.edu/~pliu3


[Rd] getWindowsHandle()
Peng Liu