欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

C# 遍历桌面窗口实例源码(windows API)

介绍 评论 失效链接反馈

C# 遍历桌面窗口实例源码(windows API) C#语言基础-第1张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("请打开开票软件界面"); } } }}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复