参见:
http://blog.opennetcf.com/ctacke/CategoryView,category,OpenNETCF.aspx
这个不是什么困难的事情,也有很多人都写了,主要是练练手。
BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
lpEnumFunc :EnumWindowsProc类型的回调函数
lParam :传递给回调函数的应用程序指定值
返回值: 非零表示成功;零表示失败
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam );
hwnd:顶层窗口句柄
lParam:指定要传递给EnumWindows or EnumDesktopWindows的值
返回值: 非零表示成功;零表示失败
在程序中如下:
代码:
public partial class EnumWindowProc_Form : Form
{
EnumWindowsProc callbackDelegate;
IntPtr callbackDelegatePointer;
StringBuilder windowName;
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool EnumWindows(IntPtr lpEnumFunc, uint lParam);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
public EnumWindowProc_Form()
{
InitializeComponent();
}
// 在Load中初始化
private void Form1_Load(object sender, EventArgs e)
{
callbackDelegate = new EnumWindowsProc(EnumWindowsCallbackProc);
callbackDelegatePointer = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
// 需指定大小
// 默认容量是 16, 默认的最大容量是 Int32.MaxValue
windowName = new StringBuilder(80);
}
int index = 0;
public int EnumWindowsCallbackProc(IntPtr hwnd, IntPtr lParam)
{
++index;
// 获取窗体名称,并返回实际的字符串长度(不包含空终结符)
int realWindowLenght = GetWindowText(hwnd, windowName, this.windowName.Capacity);
this.listBox1.Items.Add(index.ToString() + " - " + windowName.ToString());
return 1;
}
private void Btn_EnumWindowsProc_Click(object sender, EventArgs e)
{
if (this.listBox1.Items.Count > 0)
{
this.listBox1.Items.Clear();
}
bool res = EnumWindows(callbackDelegatePointer, 0);
if (res)
{
System.Diagnostics.Debug.WriteLine("Error code: " + Marshal.GetLastWin32Error().ToString());
}
}
--------------------------------------------------
|
李森 – listen |
|
声明: Announce: |