以下过程和示例演示托管应用程序如何使用平台调用来输出本地计算机上每个窗口的句柄值。尤其是,该过程和示例将使用 EnumWindows 函数来逐步浏览窗口列表,并使用一个托管回调函数(名为 CallBack)来输出窗口句柄的值。
using System;
using System.Runtime.InteropServices;
public delegate bool CallBack(int hwnd, int lParam);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}
public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}