For API enumwindows, the first parameter wndenumproc is a function pointer type (enumwindowsproc), and hwnd represents the form handle. The second lparam represents the second parameter of enumwindows, that is, the User-Defined Object (parameter) that is passed in the callback function ).
Enumwindows can only obtain the handle of one form. Therefore, getwindowtext is used to return the title of the window.
Code:
// Function pointer type
Delegate Bool Enumwindowsproc(IntptrHwnd,IntptrLparam );
[Dllimport("User32.dll")]
Static Extern IntEnumwindows (EnumwindowsprocHwnd,IntptrLparam );
[Dllimport("User32.dll", Charset= Charset.Auto)]
Static Extern IntGetwindowtext (IntptrHwnd,StringbuilderLptext,IntNcount );
// Callback function
Static BoolPrintwindow (IntptrHwnd,IntptrLparam)
{
// Allocate space
VaRSb= New Stringbuilder(50);
Getwindowtext (hwnd, Sb, sb.Capacity );
// Note that some windows do not have a title
If(Sb.Tostring ()! = String.Empty)
Console.Writeline (sb.Tostring ());
// The callback function returns a value.
Return True;
}
Static VoidMain ()
{
Enumwindows (printwindow,Intptr.Zero );
}