随笔 - 74, 文章 - 0, 评论 - 26, 引用 - 0
数据加载中……

.NET Compact Framework 2.0下调用EnumWindows(Callback方式) 转

发过一篇文章是.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 下调试通过!

posted on 2008-10-24 16:40 井泉 阅读(571) 评论(1)  编辑 收藏 引用 所属分类:

评论

# re: .NET Compact Framework 2.0下调用EnumWindows(Callback方式) 转  回复  更多评论   

错误 1 “WindowsApplication2.Form1.Dispose(bool)”: 没有找到适合的方法来重写 C:\Users\05568\Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.Designer.cs 14 33 WindowsApplication2
2011-05-23 08:38 | 钻某人

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理