FindWindowEx (or EnumWindows/EnumChildWindows) in .NET

  • Thread starter Thread starter Mallikarjun Tuppad
  • Start date Start date
M

Mallikarjun Tuppad

Whats the equivalent in .NET for the following.

FindWindowEx (or EnumWindows/EnumChildWindows)

Mallik
 
Whats the equivalent in .NET for the following.

FindWindowEx (or EnumWindows/EnumChildWindows)

Those same APIs. There's no framework wrappers for them, so you have
to call them through P/Invoke.



Mattias
 
You could also just allow the .NET interop services do most/all of the work
and declare it like the following in a class (mine's a static utility
class), then just use it.
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]

public static extern IntPtr FindWindowEx([MarshalAs(UnmanagedType.I4)]int
hWndParent, [MarshalAs(UnmanagedType.I4)]int hWndChild,
[MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);

This seems to work good for my purposes.

John
 
You could also just allow the .NET interop services do most/all of the work
and declare it like the following in a class (mine's a static utility
class), then just use it.
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]

public static extern IntPtr FindWindowEx([MarshalAs(UnmanagedType.I4)]int
hWndParent, [MarshalAs(UnmanagedType.I4)]int hWndChild,
[MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);

This seems to work good for my purposes.

John

John

I have been trying to find the 'Desktop' window so that I can hook
into it and pick up mouse clicks.

Spy shows its class name as 'SysListView32' and its Window text as
'FolderView' but even with this information FindWindowEx returns null.

Is this a 'special' case of some sort or is XP/.NET hiding this
particular window?
 
Jeff said:
You could also just allow the .NET interop services do most/all of the work
and declare it like the following in a class (mine's a static utility
class), then just use it.
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]

public static extern IntPtr FindWindowEx([MarshalAs(UnmanagedType.I4)]int
hWndParent, [MarshalAs(UnmanagedType.I4)]int hWndChild,
[MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);

This seems to work good for my purposes.

John


John

I have been trying to find the 'Desktop' window so that I can hook
into it and pick up mouse clicks.

Spy shows its class name as 'SysListView32' and its Window text as
'FolderView' but even with this information FindWindowEx returns null.

Is this a 'special' case of some sort or is XP/.NET hiding this
particular window?


I think you can Call GetDesktopWindow() API for that.. No?
 
Jeff,

I'd try to use GetDesktopWindow(), but you'd have to adorn it w/ proper
attribute info analogously to the FindWindowEx approach to get the .NET
interop services to access it correctly. I havent't tried it, but something
like this should be pretty close:

[DllImport("user32.dll", SetLastError=false, CharSet=CharSet.Auto)]
public static extern IntPtr GetDesktopWindow();


John

Jeff Gaines said:
You could also just allow the .NET interop services do most/all of the work
and declare it like the following in a class (mine's a static utility
class), then just use it.
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]

public static extern IntPtr FindWindowEx([MarshalAs(UnmanagedType.I4)]int
hWndParent, [MarshalAs(UnmanagedType.I4)]int hWndChild,
[MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);

This seems to work good for my purposes.

John

John

I have been trying to find the 'Desktop' window so that I can hook
into it and pick up mouse clicks.

Spy shows its class name as 'SysListView32' and its Window text as
'FolderView' but even with this information FindWindowEx returns null.

Is this a 'special' case of some sort or is XP/.NET hiding this
particular window?
 
Jeff,

I'd try to use GetDesktopWindow(), but you'd have to adorn it w/ proper
attribute info analogously to the FindWindowEx approach to get the .NET
interop services to access it correctly. I havent't tried it, but something
like this should be pretty close:

[DllImport("user32.dll", SetLastError=false, CharSet=CharSet.Auto)]
public static extern IntPtr GetDesktopWindow();


John

Girish / John

Well at least that returns a handle :-)) It doesn't seem to be the
'Desktop' though, i.e. the area that contains the icons and has the
TaskBar sitting on it. I have had to use:

private void JGetDeskTop()
{
bool blnFound = false;
string strText;

IntPtr ipTemp = JLib.JGAPI.GetShellWindow();

do
{
ipTemp = JLib.JGAPI.GetWindow(ipTemp, JLib.JGAPI.GW_CHILD);
strText = JLib.JGAPI.JGetWindowText(ipTemp);
if(strText == "FolderView")
{
blnFound = true;
}
}
while(!blnFound);

txtSearchHandle.Text = ipTemp.ToString();
}

I am really struggling to discover why this is so difficult, I would
have thought that getting a handle on the Desktop was a fairly common
requirement?

I have just re-installed VB6 and I think I will have a play with that
and see how the results compare to C#.

I would certainly appreciate any more comments though, perhaps what I
am calling the 'Desktop' is actually something else nowadays?
 
Hi,
The util class looks great.

But can it help in my requirement, (posted as another question.

=====================REPOST==================
Hi Guys,


I have this requirement,


I am using a 3rd party program to do some tasks for me.
When I invoke some api on them - it launches a dialog box.
Catch is the API doesn't return the handle to me.

My requirement here is -
- I want to hide this dialog box. (the user shouldn't notice this)
- Gain Control of this dialog using some windows APIs (I know the dlg
display name)
- Once I gain control of that dialog - want to simulate a user click - I.e.
select an item in the List box on dialog (while its hidden)
- Simulate button click on the dialog (while hidden)
- then close the dlg programmatically



How can I do this.

What Are the APIs available for me. ?

Mallikarjun.
=====================REPOST - END==================

Mallikarjun
 
Jeff,
I have been trying to find the 'Desktop' window so that I can hook
into it and pick up mouse clicks.

Spy shows its class name as 'SysListView32' and its Window text as
'FolderView' but even with this information FindWindowEx returns null.

The FolderView window isn't a top-level window, so you have to walk
the window hierarchy to find it. It's

The desktop window (GetDesktopWindow)
- The shell window (GetShellWindow)
- A window with class name SHELLDLL_DefView
- The FolderView window with class name SysListView32

So I guess you could do

IntPtr shell = GetShellWindow();
IntPtr defView = FindWindowEx( shell, IntPtr.Zero, "SHELLDLL_DefView",
null );
IntPtr folderView = FindWindowEx( defView, IntPtr.Zero,
"SysListView32", "FolderView" );



Mattias
 
Jeff,


The FolderView window isn't a top-level window, so you have to walk
the window hierarchy to find it. It's

The desktop window (GetDesktopWindow)
- The shell window (GetShellWindow)
- A window with class name SHELLDLL_DefView
- The FolderView window with class name SysListView32

So I guess you could do

IntPtr shell = GetShellWindow();
IntPtr defView = FindWindowEx( shell, IntPtr.Zero, "SHELLDLL_DefView",
null );
IntPtr folderView = FindWindowEx( defView, IntPtr.Zero,
"SysListView32", "FolderView" );



Mattias

Mattias

Many thanks, I just couldn't understand the hierarchy properly.

I'll have to add you to my Christmas card list (you are already in my
spell checker) :-))
 
Mattias,

This explains a lot. Thanks!

John

Mattias Sjögren said:
Jeff,


The FolderView window isn't a top-level window, so you have to walk
the window hierarchy to find it. It's

The desktop window (GetDesktopWindow)
- The shell window (GetShellWindow)
- A window with class name SHELLDLL_DefView
- The FolderView window with class name SysListView32

So I guess you could do

IntPtr shell = GetShellWindow();
IntPtr defView = FindWindowEx( shell, IntPtr.Zero, "SHELLDLL_DefView",
null );
IntPtr folderView = FindWindowEx( defView, IntPtr.Zero,
"SysListView32", "FolderView" );



Mattias
 
Back
Top