发过一篇文章是.net cf 1.x实现EnumWindows,因为.net cf
1.x不支持Callback方式,所以实现起来比较繁琐,而且效率不高。.net cf
2.0中就不同了,已经加入了的对Callback的支持了,所以我们就可以调用EnumWindows这个API函数来遍历所有的窗口了,下面是我写的
一个Demo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace EnumWindows
{
public partial class Form1 : Form
{
public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
EnumWindowsProc callbackDelegate;
IntPtr callbackDelegatePointer;
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool EnumWindows(IntPtr lpEnumFunc, uint lParam);
[DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("coredll.dll")]
public static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
public Form1()
{
InitializeComponent();
callbackDelegate = new EnumWindowsProc(EnumWindowsCallbackProc);
callbackDelegatePointer = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
EnumWindows(callbackDelegatePointer, 0);
}
public int EnumWindowsCallbackProc(IntPtr hwnd, IntPtr lParam)
{
int length = GetWindowTextLength(hwnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hwnd, sb, sb.Capacity);
System.Diagnostics.Debug.WriteLine("Window: " + hwnd.ToString() + "," + sb.ToString());
return 1;
}
}
}
vs.net 2005 下调试通过!