日期:2014-05-17  浏览次数:20843 次

C#调用API问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace 遍历所有窗体_获取句柄和窗体名称
{
    class Program
    {
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
    
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
        //[DllImport("user32.dll")]
        //private static extern IntPtr FindWindowW(string lpClassName, string lpWindowName);
     
        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
     
        [DllImport("user32.dll")]
        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
    
        public struct WindowInfo
        {
            public IntPtr hWnd;
            public string szWindowName;
            public string szClassName;
        }
        static void Main(string[] args)
        {
            List<WindowInfo> wndList = new List<WindowInfo>();
            //enum all desktop windows
            EnumWindows(delegate(IntPtr hWnd, int lParam)//这里不明白啊
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);//动态的字符串
                //get hwnd
                wnd.hWnd = hWnd;
                //get window name
                GetWindowTextW(hWnd, sb, sb.Capacity);
          [code=csharp]      wnd.szWindowName = sb.ToString();