原文 http://blog.csdn.net/dengta_snowwhite/article/details/6067928

与C++不同,C#调用API函数需要引入.dll文件,步骤如下:

1. 添加命名空间

using System.Runtime.InteropServices;

2. DllImport调入EnumWindows等函数

[DllImport("user32.dll")]

//EnumWindows函数,EnumWindowsProc 为处理函数

private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);

其他常用函数格式如下:
        [DllImport("user32.dll")]
        private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
        [DllImport("user32.dll")]
        private static extern bool IsWindowVisible(int hWnd);
        [DllImport("user32.dll")]
        private static extern int GetWindowTextLength(int hWnd);
        [DllImport("USER32.DLL")]
        private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
        [DllImport("USER32.DLL")]
        private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

3. 申明委托

public delegate bool EnumWindowsProc(int hWnd, int lParam);

4.定义委托函数,ADA_EnumWindowsProc为执行函数,返回true则EnumWindows继续枚举下一个顶级窗口直到枚举完

EnumWindowsProc ewp = new EnumWindowsProc(ADA_EnumWindowsProc);
        EnumWindows(ewp, 0);

5. 实现委托函数

public bool  ADA_EnumWindowsProc(int hWnd, int lParam)
        {
            int cTxtLen, i;
            String cTitle, strtmp;
            if (IsWindowVisible(hWnd)) 
            {

//..........对每一个枚举窗口的处理
                //Get the task name
                cTxtLen = GetWindowTextLength(hWnd) +1;
                StringBuilder text = new StringBuilder(cTxtLen);
                GetWindowText(hWnd, text, cTxtLen);
                cTitle = text.ToString();
                cTitle = cTitle.ToUpper();

//...............
            }

return true;
        }

C#调用API函数EnumWindows枚举窗口的方法的更多相关文章

  1. 在C#中调用Win32函数EnumWindows枚举所有窗口。

    原文 http://www.cnblogs.com/mfm11111/archive/2009/06/30/1514322.html 开发旺旺群发软件,难点及重要技术点分析(一) 一.        ...

  2. 调用API函数,在窗口非客户区绘图(通过GetWindowDC获得整个窗口的DC,就可以随意作画了)

    http://hi.baidu.com/3582077/item/77d3c1ff60f9fa5ec9f33754 调用API函数,在窗口非客户区绘图 GDI+的Graphics类里有个FromHdc ...

  3. 使用API函数EnumWindows()枚举顶层窗口

      http://simpleease.blog.163.com/blog/static/1596085820052770290/ 要枚举Windows当前所有打开的顶层窗口,可使用Windows A ...

  4. Vb.net/VB 声明API函数实现父窗口功能

    回想第一次敲机房收费.自己调用了api函数实现了父窗口及其子窗口最小化的功能.现在再次遇到,自己就在思考,能不能继续使用API函数呢?答案当然是Of Course! 事实上细致看两者并没有多大的差别. ...

  5. Delphi调用API函数获取Windows目录信息、获取System目录信息、获取Temp临时文件目录信息

    var Str1, Str2: Array[..Max_Path]of Char;//开辟缓冲区 Str3: Array[..]of Char; begin GetWindowsDirectory(@ ...

  6. 调用API函数减少c#内存占用(20+m减至1m以下)

    原文:调用API函数减少c#内存占用(20+m减至1m以下) c#虽然内置垃圾回收机制,但是并不能解决程序占用内存庞大的问题,如果我们仔细观察任务管理器,我们会发现一个程序如果最小化的时候,它所占用的 ...

  7. Delphi - Windows系统下,Delphi调用API函数和7z.dll动态库,自动把文件压缩成.tar.gz格式的文件

    项目背景 应欧美客户需求,需要将文件压缩成.tar.gz格式的文件,并上传给客户端SFTP服务器. 你懂的,7-Zip软件的显著特点是文件越大压缩比越高,在Linux系统上相当于我们Windows系统 ...

  8. 实现:调用API函数ShowWindow()来隐藏窗口

    只需要将相应代码复制即可. 代码如下: #include <iostream> #include <windows.h> int main() { HWND hDos; //声 ...

  9. Windows API 函数列表 附帮助手册

    所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...

随机推荐

  1. HTML5学习摘录

    设计原理 不是规范里都包含什么,而是规范里为什么会包含它们,以及在设计这个规范的时候,设计者们是怎么看待这些东西的. 发展史:HTML2.0——>HTML3.2——>HTML4.0.1—— ...

  2. 健康管理app要注意哪些要点

    健康管理app现在变得越来越热,越来越多的垂直领域的加入,让健康app的市场逐渐扩大,但移动健康管理app仍有许多缺陷需要解决.健康管理并不是治病,而是让健康的人更好的保持身体健康状态,让慢性病高风险 ...

  3. Word Break I II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  4. UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>

    K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Su ...

  5. ZOJ3761(并查集+树的遍历)

    Easy billiards Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Edward think a g ...

  6. 格而知之8:我所理解的Runtime(3)

    关联对象 14.使用Category对类进行拓展的时候,只能添加方法,而不适合添加属性(可以添加属性,也可以正常使用get方法和set方法,只是不会自动生成以下划线开头命名的成员变量). 可以通过关联 ...

  7. tomcat web项目部署方式

    1.利用MyEclipse的部署部工具部署项目,可以直接部署成文件形式,这样当启动tomcat后可以直接访问 2.利用MyEclipse部署工具部署war形式,点击发布选择tomcat时可以选择该项 ...

  8. Android应用程序组件Content Provider应用实例

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...

  9. FlashBack-SCN-TIMESTAMP

    一.基于时间(as of timestamp)的flashback1.创建表create table flash_tab(id,vl) as select rownum,oname from ( se ...

  10. 【TFS】增加组员,以及用户权限分配

    一.创建windows用户. 二.TFS ->组成员资格->双击 项目集合管理员->添加创建的Windows用户(最高权限) 三.设置权限: TFS权限的复杂,其实也不是很复杂,它只 ...