C#调用API函数EnumWindows枚举窗口的方法

简介: 原文 http://blog.csdn.net/dengta_snowwhite/article/details/6067928 与C++不同,C#调用API函数需要引入.dll文件,步骤如下:   1.

原文 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;
        }

目录
相关文章
|
10天前
|
JSON 安全 API
Microsoft邮箱API发送邮件的方法和步骤
Aoksend详解如何使用Microsoft邮箱API发送邮件:1. 在Azure创建应用并获取访问权限;2. 设置API请求头,含Authorization和Content-Type;3. 构建JSON格式的邮件内容;4. 使用POST方法发送至API端点;5. 检查响应处理发送结果。遵循最佳实践,安全集成邮件功能。
|
12天前
|
搜索推荐 JavaScript 前端开发
Gmail邮箱API发送邮件的方法有什么
使用Gmail API发送邮件,需先获取API访问权限,包括在Google Cloud Platform上创建项目,启用Gmail API,生成API密钥或OAuth 2.0凭据。然后,用Python等编程语言设置API请求,指定邮件详情。发送简单邮件涉及创建Base64编码的消息体,而带附件的邮件需编码为multipart格式。可添加邮件头信息,并处理发送结果以确保成功。Gmail API使应用能集成自动化、个性化的邮件发送功能,提升效率和体验。
|
28天前
|
人工智能 Java API
Google Gemini API 接口调用方法
Google 最近发布的 Gemini 1.0 AI 模型通过其升级版,Gemini,标志着公司迄今为止最为强大和多功能的人工智能技术的突破。
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
1月前
|
缓存 JavaScript 算法
活用 Composition API 核心函数,打造卓越应用(下)
活用 Composition API 核心函数,打造卓越应用(下)
|
1月前
|
存储 JavaScript API
活用 Composition API 核心函数,打造卓越应用(上)
活用 Composition API 核心函数,打造卓越应用(上)
|
1月前
|
存储 C# 数据库
C# 生成唯一ID,有哪些方法?
【2月更文挑战第12天】
152 0
|
27天前
|
人工智能 关系型数据库 Serverless
Serverless 应用引擎常见问题之API生成的函数镜像改为自定义的镜像如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
37 3
|
2天前
|
安全 API 开发者
Zoho Mail邮箱API发送邮件的方法
Zoho Mail提供了强大的API,使开发者可以通过编程方式轻松地使用Zoho Mail发送邮件。aoksend将介绍如何使用Zoho Mail邮箱API发送邮件,以及一些常见的用法示例。