参见:
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的值
返回值: 非零表示成功;零表示失败
在程序中如下:
P/Invoke
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool EnumWindows(IntPtr lpEnumFunc, uint lParam);
// 枚举窗口回调
public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
代码:
Code
public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
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
E-mail: lisencool@gmail.com
|
声明:
这里集中了在WinCE和Windows Mobile开发中的一些基本常识。我很乐意和大家分享,也希望大家提出意见,并给我投稿,我会第一时间替您发表并署上您的大名!
Announce:
Here collects general knowledge on WinCE and Windows mobile. I 'm very glad to share them with all friends, and also hope you can share your problems and opinions and contribute articles to me to share with others. I'll publish your articles and sign your name at the first time.
|