C#调用API函数EnumWindows枚举窗口的方法
2010-12-10 15:49
295 查看
与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#窗口间的公共方法调用!!
- 在C#中调用Win32函数EnumWindows枚举所有窗口
- C#窗口传值及方法调用1--通过静态方法实现
- iframe 父窗口和子窗口的调用方法
- C#使用DllImport调用非托管的代码的方法
- easyui dialog 子窗口jsp(被弹出窗口)调用父jsp页面方法操作父jsp
- C#调用windows的API函数实现将不活动窗口置为活动窗口,并置前显示
- C#中一种可调用的异常处理方法
- 用C#调用Windows API向指定窗口发送按键消息
- C#调用C++编的dll文件里的方法报错
- jquery、js调用iframe父窗口(父类)与子窗口元素的方法
- C#调用C++DLL的方法
- C#调用DLL函数方法(上)(zz)
- C#陷阱之构造函数调用虚方法
- C#调用C++的dll文件方法