在C#中调用Win32函数EnumWindows枚举所有窗口。
原文 http://www.cnblogs.com/mfm11111/archive/2009/06/30/1514322.html
开发旺旺群发软件,难点及重要技术点分析(一)
一. 在C#中调用Win32函数EnumWindows枚举所有窗口。
EnumWindows 函数通过借助于应用程序定义的回调函数传递每个窗口句柄枚举所有顶层的屏幕窗口。直到最后一个顶层窗口被枚举或者回调函数返回false ,EnumWindows 函数才会退出停止枚举过程。
下面例子说明如何在 C# 中调用 Win32 API - EnumWindows 枚举所有窗口:
1.首先需要声明一个委托函数用于 Win32 API - EnumWindows 的回调函数:
public delegate bool CallBack(int hwnd, int lParam);
2.然后利用 C# 中的平台调用声明从 USER32.DLL 库中调用 API - EnumWindows,具体参数请参考 MSDN - Win32 API。
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
3.最后实例化委托,调用 EnumWindows。
CallBack myCallBack = new CallBack(EnumWindowsApp.Report);
4.代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowPosDemo
{
public delegate bool CallBack(int hwnd, int lParam); class Program
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y); static void Main(string[] args)
{
CallBack myCallBack = new CallBack(Program.Report);
EnumWindows(myCallBack, ); Console.ReadKey();
}
public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is :");
Console.WriteLine(hwnd);
Console.Read();
return true;
}
}
}
二. 现在我们用一个winform来演示查找旺旺窗口的句柄
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace WindowPosDemo
{
public struct WindowInfo
{
public IntPtr hWnd;
public string szWindowName;
public string szClassName;
}
class Program
{
[DllImport("shell32.dll")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lparam);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//查找窗口
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
static IntPtr Game; static void Main(string[] args)
{
WindowInfo[] a = GetAllDesktopWindows();
int i = ;
int index = ;
for (i = ; i < a.Length; i++)
{
// MessageBox.Show(a[i].szWindowName.ToString());
if (a[i].szWindowName.ToString().Contains("mafangmin888"))
{
MessageBox.Show(a[i].szClassName.ToString());
index = i;
}
}
Game = a[index].hWnd;
Console.ReadKey();
} //寻找系统的全部窗口
static WindowInfo[] GetAllDesktopWindows()
{
List<WindowInfo> wndList = new List<WindowInfo>();
EnumWindows(delegate(IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder();
//get hwnd
wnd.hWnd = hWnd;
//get window name
GetWindowTextW(hWnd, sb, sb.Capacity);
wnd.szWindowName = sb.ToString();
//get window class
GetClassNameW(hWnd, sb, sb.Capacity);
wnd.szClassName = sb.ToString();
Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight() + " szClassName=" + wnd.szClassName.PadRight() + " szWindowName=" + wnd.szWindowName);
//add it into list
wndList.Add(wnd);
return true;
}, );
return wndList.ToArray();
}
}
}
在C#中调用Win32函数EnumWindows枚举所有窗口。的更多相关文章
- [转]用多线程方法实现在MFC/WIN32中调用OpenGL函数并创建OpenGL窗口
原文链接: 1.用多线程方法实现在MFC/WIN32中调用OpenGL函数并创建OpenGL窗口 2.Windows MFC 两个OpenGL窗口显示与线程RC问题
- C#调用API函数EnumWindows枚举窗口的方法
原文 http://blog.csdn.net/dengta_snowwhite/article/details/6067928 与C++不同,C#调用API函数需要引入.dll文件,步骤如下: 1. ...
- 使用API函数EnumWindows()枚举顶层窗口
http://simpleease.blog.163.com/blog/static/1596085820052770290/ 要枚举Windows当前所有打开的顶层窗口,可使用Windows A ...
- C# 互操作性入门系列(二):使用平台调用调用Win32 函数
好文章搬用工模式启动ing ..... { 文章中已经包含了原文链接 就不再次粘贴了 言明 改文章是一个系列,但只收录了2篇,原因是 够用了 } --------------------------- ...
- [转]C# 互操作性入门系列(二):使用平台调用调用Win32 函数
传送门 C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列(二):使用平台调用调用Win32 函数 C# 互操作性入门系列(三):平台调用中的数据封送处理 ...
- EC笔记,第二部分:9.不在构造、析构函数中调用虚函数
9.不在构造.析构函数中调用虚函数 1.在构造函数和析构函数中调用虚函数会产生什么结果呢? #; } 上述程序会产生什么样的输出呢? 你一定会以为会输出: cls2 make cls2 delete ...
- 关于在C#中构造函数中调用虚函数的问题
在C#中如果存在类的继承关系,应避免在构造函数中调用虚函数.这是由于C#的运行机制造成的,原因如下: 新建一个类实例时,C#会先初始化该类(对类变量赋值,并将函数记在函数表中),然后再初始化父类.构造 ...
- 如何在C语言中调用Swift函数
在Apple官方的<Using Swift with Cocoa and Objectgive-C>一书中详细地介绍了如何在Objective-C中使用Swift的类以及如何在Swift中 ...
- C中调用Lua函数
我们先来看一个简单的例子: lua_State* L = NULL; // 内部调用lua函数 double f(double x, double y) { double z; lua_getglob ...
随机推荐
- display:table-cell的惊天作用,直接惊呆你!
一 display:table-cell介绍 ... 二 用法 (1)高度不固定元素,垂直居中 ... (2)高度不固定列表元素,登高排列 ... (3)宽度不固定元素,平均分配 ...
- NASM mode for Emacs
NASM mode for Emacs Quick post for those Emacs users out there. The common assembler used on GNU ...
- 移动端h5页面写法
1. 页面宽度320, 所有元素尺寸设一半 缺点:不自能适应全屏 2.页面宽度640,元素尺寸正常 <meta charset="utf-8" /> <meta ...
- BZOJ 1065 奥运物流
http://www.lydsy.com/JudgeOnline/problem.php?id=1065 思路:由于n个点,有n条边,因此由根就会引出一个环,我们枚举环的长度,在那个长度断开,我们假设 ...
- 苹果开发证书相关BLOG与Delphi IOS环境安装(超详细)
注:有好的资源,请添加了上传,上传后,通知管理员,删除旧文件,累积相关的学习资源,方便新手学习 一.相关论坛http://www.2ccc.com/ delphi 合子 www.2pascal.com ...
- LeeCode-Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...
- iphone 6s pp助手 越狱
https://www.apple.com/iphone-6/合适初高中学习英语http://www.travelchinaguide.comhttp://jailbreak.25pp.com/ ip ...
- Pattern | CLiPS
Pattern | CLiPS Pattern Pattern is a web mining module for the Python programming language. It has t ...
- 本人对于JavaScript的一些总结
类型.值和变量 1.原始类型 数字.字符串和布尔 null空 undefined未定义 2.对象类型 3.类 Array Function Date RegExp Error 4.js ...