Chinaunix首页 | 论坛 | 博客
  • 博客访问: 588333
  • 博文数量: 64
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-09 20:38
文章存档

2012年(1)

2011年(8)

2010年(16)

2009年(18)

2008年(21)

我的朋友

分类:

2010-04-15 13:22:33

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 wndList = new List();

//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();
}
}
阅读(4365) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~