C#调用API函数EnumWindows枚举窗口的方法
2013-08-03 10:47
281 查看
原文 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++不同,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枚举窗口的方法
- C#调用API函数EnumWindows枚举窗口的方法
- C#窗口间的公共方法调用!!
- 在C#中调用Win32函数EnumWindows枚举所有窗口
- C# 在子窗口中调用父窗口的方法
- 在C#中调用Win32函数EnumWindows枚举所有窗口。
- 在C#中调用Win32函数EnumWindows枚举所有窗口
- C#窗口传值及方法调用1--通过静态方法实现
- 不用AJAX实现前台JS调用后台C#方法(小技巧)
- 在C#中调用python方法
- C#调用Java C++方法
- C#中通过调用webService获取上网IP地址的区域的方法
- C#调用C++ DLL类方法
- C#线程调用带参数的方法
- C#异步调用的好处和方法
- Unity C# 使用反射,利用字符串作为泛型参数调用泛型方法。
- C#获取当前堆栈的各调用方法列表
- NET 2.0(C#)调用ffmpeg处理视频的方法
- 父窗口调用iframe子窗口方法
- C# 调用dll获取dll物理路径的方法