發過一篇文章是.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 下調試通過!