在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 ...
随机推荐
- ubuntu 安装openproj-1.4-2.noarch.rpm
一 openproj是rpm包,ubuntu下需要转成deb安装.具体步骤1:下载:http://sourceforge.net/projects/openproj/2:安装alien sudo ap ...
- C 语言---漂亮的宏定义
写好C 语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性等等.下面列举一些成熟软件中常用得宏定义. 1.防止一个头文件被重复包含 #ifndef COMDEF_H #de ...
- 【转】Linux驱动模块编译进内核中
原文网址:http://blog.chinaunix.net/uid-29287950-id-4573481.html BQ27501驱动编译进内核 一. 驱动程序编译进内核的步骤 在 l ...
- windows 基于命令行制作vhd虚拟磁盘
什么是VHD? VHD是Virtual Hard Disk的简称,就是虚拟硬盘,就是能把VHD文件直接虚拟成一个硬盘,在其中能像真实硬盘一样操作,读取.写入.创建分区.格式化.如果你用过虚拟机,就会知 ...
- [置顶] think in java interview番外篇-谈程序员如何修练英语
一.程序员对英语能力的重视度和能力要求应该是在各行各业中排在比较靠前的 这样说吧,英语程度的好坏直接影响着一个程序员的编程.开发.创新能力. 道理很简单: 1. 计算机和软件是用英语创造出来的 2. ...
- Unity 集成联通SDK
我相信Unity程序员都会遇到加入SDK的问题,我相信如果你不会android编程,我相信你的CPU当场计算过快而爆炸! 这里也写笔记希望能帮助大家 如果有讲错的地方,希望大家能回复并且提供答案! ...
- Funny Sheep(思维)
Problem 1606 - Funny Sheep Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 612 Accepted ...
- MVC5 Entity Framework学习之Entity Framework高级功能
在之前的文章中,你已经学习了怎样实现每一个层次结构一个表继承. 本节中你将学习使用Entity Framework Code First来开发ASP.NET web应用程序时能够利用的高级功能. 在本 ...
- js/css 检测移动设备方向的变化 判断横竖屏幕
js/css 检测移动设备方向的变化 判断横竖屏幕 方法一:用触发手机的横屏和竖屏之间的切换的事件 window.addEventListener("orientationchange&qu ...
- JS转换Decimal带千分号的字符串显示
var numberChars = "0123456789"; /* Convert to decimal string */ function toDecimalString(v ...