在C#中调用Win32函数EnumWindows枚举所有窗口。

简介: 原文 http://www.cnblogs.com/mfm11111/archive/2009/06/30/1514322.html 开发旺旺群发软件,难点及重要技术点分析(一) 一.        在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, 0);

            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 = 0;
            int index = 0;
            for (i = 0; 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(256);
                //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(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName);
                //add it into list
                wndList.Add(wnd);
                return true;
            }, 0);
            return wndList.ToArray();
        }
    }
}

 

 

目录
相关文章
|
3月前
|
程序员 C#
深入理解 C# 编程:枚举、文件处理、异常处理和数字相加
枚举是一个特殊的“类”,表示一组常量(不可更改/只读变量)。 要创建枚举,请使用 enum 关键字(而不是 class 或 interface),并用逗号分隔枚举项:
38 0
|
C# Windows 容器
C#或Winform中的消息通知之系统托盘的气泡提示窗口(系统toast通知)、ToolTip控件和ToolTipText属性
NotifyIcon控件表示系统右下角任务栏上的托盘图标,其ShowBalloonTip方法用于显示气球状提示框(Win10只有为本地Toast通知),ToolTip\oolTipText可以...
1487 0
C#或Winform中的消息通知之系统托盘的气泡提示窗口(系统toast通知)、ToolTip控件和ToolTipText属性
|
消息中间件 安全 API
C#实现操作Windows窗口句柄:SendMessage/PostMessage发送系统消息、事件和数据【窗口句柄总结之二】
SendMessage/PostMessage API 可以实现发送系统消息,这些消息可以定义为常见的鼠标或键盘事件、数据的发送等各种系统操作......
3771 1
C#实现操作Windows窗口句柄:SendMessage/PostMessage发送系统消息、事件和数据【窗口句柄总结之二】
|
1月前
|
设计模式 C#
36.c#:如何设置MDL窗口
36.c#:如何设置MDL窗口
15 1
|
1月前
|
C# 开发者
35.c#:winform窗口
35.c#:winform窗口
13 1
|
5月前
|
开发框架 自然语言处理 文字识别
一款C#开发的窗口文本提取开源软件
一款C#开发的窗口文本提取开源软件
47 2
|
9月前
|
C#
【C#视频】常量、枚举、结构体、数组
【C#视频】常量、枚举、结构体、数组
|
9月前
|
存储 C#
C#基础⑥.1——枚举、结构体
枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。也就是一些固定范围的值。
|
9月前
|
存储 C#
C#基础Ⅵ❶-枚举、结构体
C#基础Ⅵ❶-枚举、结构体