基本信息
源码名称:C# 遍历桌面窗口实例源码(windows API)
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2017-02-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace FindWindow
{
    public partial class Form1 : Form
    {
        IntPtr hwnd = new IntPtr(0);
        public string czct = "开具增值税专用发票";
        public string fpkjpp = "开具增值税普通发票";
        // const int WM_COPYDATA = 0x004A;//必须是这个数值,不能更改
        const int WM_COPYDATA = 0x13;
        const int WM_SETTEXT = 0x000C;
        List<string> list = new List<string>();

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

      
        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
        public delegate bool CallBack(IntPtr hwnd, int lParam);

        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);

        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);

        public struct COPYDATASTRUCT
        {
            public IntPtr dwData; //可以是任意值
            public int cbData;    //指定lpData内存区域的字节数
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData; //发送给目录窗口所在进程的数据
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnum_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = string.Empty;
            StringBuilder lpString = new StringBuilder(200);
            // 遍历所有windows
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                lpString = new StringBuilder(200);
                GetWindowTextW(hWnd, lpString, lpString.Capacity);
                if (!string.IsNullOrEmpty(lpString.ToString()))
                {
                    this.textBox1.AppendText(lpString.ToString() "\r\n");
                }
                return true;
            }, 0);
        }

        private void btnFimd_Click(object sender, EventArgs e)
        {
            hwnd = FindWindow(null, czct);
            //如果找到窗口句柄
            if (hwnd != IntPtr.Zero)
            {
                this.textBox1.Clear();
                this.textBox1.AppendText("找到:" czct);

            }
            else
            {
                this.textBox1.Clear();
                this.textBox1.AppendText("未找到:" czct);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
           
        }
        private void button2_Click(object sender, EventArgs e)
        {
            IntPtr hwnd = new IntPtr(0);
            IntPtr[] Ptr = new IntPtr[100];
            hwnd = FindWindow(null, czct);
            if (hwnd != IntPtr.Zero)
            {
                int i = 0;
                int j = 0;
                EnumChildWindows(hwnd,
                               delegate (IntPtr hWnd, int lParam)
                               {
                                   StringBuilder lpString = new StringBuilder(200);
                                   GetClassName(hWnd, lpString, lpString.Capacity);
                                   String[] PtrClassName = new String[100];
                                   if (!string.IsNullOrEmpty(lpString.ToString()))
                                   {

                                       this.textBox1.AppendText(lpString.ToString() "\r\n" hWnd.ToString() "\r\n" j.ToString() "\r\n");
                                       j ;
                                       Ptr[i] = hWnd;                                       
                                       i ;
                                   }
                                   return true;
                               }, 0);
                //SendMessage(Ptr[33], WM_SETTEXT, 0, "测试发送数据1");
                //SendMessage(Ptr[31], WM_SETTEXT, 0, "测试发送数据2");
                //SendMessage(Ptr[36], WM_SETTEXT, 0, "测试发送数据3");
                //SendMessage(Ptr[38], WM_SETTEXT, 0, "测试发送数据4");
                //MessageBox.Show(Ptr[44] "已找到句柄1" Ptr[38] "已找到句柄2" Ptr[39] "已找到句柄3" Ptr[40] "已找到句柄4" Ptr[41] "已找到句柄1");
            }
            else
            {
                MessageBox.Show("请打开开票软件界面");
            }
        }
    }
}