using System; using System.Runtime.InteropServices; using System.Text; using System.Collections.Generic; class CSharpAPIsDemo { 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; } public WindowInfo[] GetAllDesktopWindows() { 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); wnd.szWindowName = sb.ToString(); //get window class GetClassNameW(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); //add it into list wndList.Add(wnd); return true; }, 0); return wndList.ToArray(); } } 本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/stxyc/archive/2010/03/24/5411365.aspx ------------------------------------截取窗口屏幕 using System.Runtime.InteropServices; …… internal class NativeCalls { [DllImport("user32.dll")] internal extern static IntPtr GetDesktopWindow(); [DllImport("user32.dll")] internal extern static IntPtr GetDC( IntPtr windowHandle ); [DllImport("gdi32.dll")] internal extern static IntPtr GetCurrentObject( IntPtr hdc, ushort objectType ); [DllImport("user32.dll")] internal extern static void ReleaseDC( IntPtr hdc ); } public class DesktopImage { public static Image Capture() { IntPtr desktopWindow = NativeCalls.GetDesktopWindow(); IntPtr desktopDC = NativeCalls.GetDC( desktopWindow ); IntPtr desktopBitmap = NativeCalls.GetCurrentObject( desktopDC, 7 ); Image desktopImage = Image.FromHbitmap( desktopBitmap ); NativeCalls.ReleaseDC( desktopDC ); return desktopImage; } } …… private void button1_Click(object sender, System.EventArgs e) { pictureBox1.Image = DesktopImage.Capture(); } 本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/stxyc/archive/2010/03/24/5411359.aspx 文章来源:玉成ID:stxyc