C#调用API函数EnumWindows枚举窗口的方法
2011-08-29 14:24
218 查看
与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;
}
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#中调用Win32函数EnumWindows枚举所有窗口。
- C#窗口传值及方法调用1--通过静态方法实现
- C#窗口间的公共方法调用!!
- 在C#中调用Win32函数EnumWindows枚举所有窗口
- C#实现简易ajax调用后台方法
- ASP.NET,C#后台调用前台javascript的五种方法
- C#之用Base方法调用基类构造函数学习案例
- C#将PPT转化为XPS时遇到的“对COM组件的调用返回了错误HRESULT E_FAIL”错误的解决方法
- C#异步调用的实现机制及调用方法
- 详解VB调用C#类库的方法步骤(转载)
- C#窗体程序,当窗口关闭后,进程无法关闭解决方法
- C#调用URL接口方法
- C#中同一个类的多个构造方法之间的调用
- C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法
- C#后台调用前台javascript的五种方法小结
- PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)