. Net (C #) platform call: Use enumwindows and getwindowtext to list windows

Source: Internet
Author: User

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 );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.