"mo A" <ma******@hotmail.com> wrote in message news:eJ**************@TK2MSFTNGP12.phx.gbl...
i looked into using EnumWindows() to get a handle to all active windows,
but how can i implement that ? (especially the call back function)?
EnumWindows can be accessed through P/Invoke using a signature
like this,
[DllImport( "USER32.DLL")]
public static extern int EnumChildWindows(
IntPtr hwndParent,
[MarshalAs( UnmanagedType.FunctionPtr)]
EnumFunc lpfnEnumFunc,
int lParam
);
The "callback" function is just a .NET delegate marshaled as a function
pointer in the above signature. Define it like this,
public delegate int EnumFunc( IntPtr hwnd, int lParam);
Elsewhere within your application, you define a method that gets called
back (in this trivial example, I'll just emit the HWND of the windows, al-
though in more sophisticated implementations you may have other Win32
P/Invoke methods like GetWindowText and can pass the HWND to them
to display more relevant information as required),
private int EnumChildren( IntPtr hwnd, int lParam)
{
Console.WriteLine( "HWND {0:X8}", (int)hwnd);
}
With these three pieces of code in place, the final line of code, the
actual calling to EnumWindows( ), becomes elementary,
MyClassName.EnumChildWindows(
IntPtr.Zero,
new EnumFunc( this.EnumChildren),
0
);
Derek Harmon