在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();
        }
    }
}

 

 

时间: 2024-08-01 12:58:30

在C#中调用Win32函数EnumWindows枚举所有窗口。的相关文章

C#调用API函数EnumWindows枚举窗口的方法

原文 http://blog.csdn.net/dengta_snowwhite/article/details/6067928 与C++不同,C#调用API函数需要引入.dll文件,步骤如下:   1. 添加命名空间 using System.Runtime.InteropServices;   2. DllImport调入EnumWindows等函数         [DllImport("user32.dll")]         //EnumWindows函数,EnumWind

《Effective C++》读书笔记09:绝不在构造和析构过程中调用virtual函数

首先明确一下,对于一个继承体系,构造函数是从基类开始调用了,而析构函数则正 好相反,从最外层的类开始. 对于在构造函数中调用virtual函数,先举个例子: 1 class Transaction //所有交易的基类 2 { 3 public: 4 Transaction(); 5 virtual void logTransaction() const = 0;//日志记 录,因交易类型的不同而有不同的记录 6 } 7 8 Transaction::Transaction()//构造函数实现 9

为什么要在cs文件中调用js函数

问题描述 我有个问题想问下大家,为什么有的时候要在cs文件中调用js函数,还有有时候要在js里面调用cs函数,一直比较迷惑,不是太清楚,能举个例子详细的介绍下吗谢谢了 解决方案 解决方案二:js调用cs的函数是ajax,cs调用js的函数,有时是为了实现某个特定的效果,才会这么做解决方案三:谢谢你的回答,不过能说得再详细点吗,我刚开始学习,好多地方不明白谢谢解决方案四:当然是实现需求了.服务端输出JS最典型的是response.write("<script>alert('notice

link中调用自定义函数为什么不能是C#的函数?

问题描述 link中调用自定义函数为什么不能是C#的函数? link中调用自定义函数为什么不能是C#的函数? 解决方案 因为linq是放在数据库端查询的,而C#代码是本地执行的,没法翻译成sql

有一个类中的函数是protected的,我需要在另外一个类中调用这个函数如何实现

问题描述 有一个类中的函数是protected的,我需要在另外一个类中调用这个函数如何实现 一个类中的函数的修饰符为protected protected: BOOL RegisterWindowClass(); LRESULT SendMessageToParent(int nRow, int nCol, int nMessage); BOOL InvalidateCellRect(const CCellID& cell); 我需要在另外一个对话框类中访问这个类,请问如何实现谢谢了.

c++-我想创建可以在任何一个OnClick事件中调用的函数,怎么写?

问题描述 我想创建可以在任何一个OnClick事件中调用的函数,怎么写? 现在有一个单文档MFC程序,我想创建一个全局的函数或者类,可以在任何一个OnClick事件中调用,请问应该怎么写? 我是这样做的,定义了一个类A,然后A中的函数比如void b()是public的,在其他onclick事件中创建一个A的对象a,a.b();编译没问题,可是一连接就报错,为什么?我应该怎么写? 解决方案 如果单纯的想将OnClick中的功能分离成一个函数的话,你可以在OnClick这个函数所在的类比如说,CE

C++构造函数中调用虚函数

谈谈关于构造函数中调用虚函数的情况,仅讨论单继承,不考虑虚拟继承和多重继承. 测试平台:VS2013 + Win7X64 一个例子: #include <stdlib.h> #include <stdio.h> class Base { private: int __data; public: Base() { this->Func(); } public: virtual void Func() { printf("Base::Func"); } };

Lua中调用C++函数实例_Lua

到这为止,大家对Lua和C++之间的通信应该有些熟悉了,今天我们来介绍最后一个操作. (旁白:什么?最后一个?要结束了么?太好了~!) 上一章传送门:http://www.jb51.net/article/55097.htm 1. Lua调用C++的函数 Lua要调用C++的函数还是蛮方便的,首先,我们来创建一个c++函数先: 复制代码 代码如下: public: static int getNumber(int num); int HelloLua::getNumber( int num )

c++ 构造函数中调用虚函数的实现方法_C 语言

我们知道:C++中的多态使得可以根据对象的真实类型(动态类型)调用不同的虚函数.这种调用都是对象已经构建完成的情况.那如果在构造函数中调用虚函数,会怎么样呢? 有这么一段代码: class A { public: A ():m_iVal(0){test();} virtual void func() { std::cout<<m_iVal<<' ';} void test(){func();} public: int m_iVal; }; class B : public A {