WinApi识别并监听WinForm窗体文本内容

更新时间:2023-07-13 01:04:48 阅读: 评论:0

WinApi识别并监听WinForm窗体⽂本内容⼀、准备⼯作
VS2017创建winform项⽬,下载CSkin.dll库并在项⽬中添加引⽤(主要使⽤MouHook),创建两个窗体页⾯
中国少先队队歌
⼆、功能实现
窗体识别信息实体类
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinApiDemo
{
internal class RecognitionData
{
private int index = -1;
private string szPWindowName;
private string szPClasName;
private string szWindowName;
private string szClassName;
private Size size;
private List<string> windows;
public string SzWindowName
{
get
{
return this.szWindowName;
}
t
{
this.szWindowName = value;
}
}
public string SzClassNameoppor9sk
{
get
{
return this.szClassName;
t
{
this.szClassName = value;
}
}
public string SzPWindowName
{
get
{
return this.szPWindowName;            }
t
{
this.szPWindowName = value;            }
}
public string SzPClasName
{
get
{
return this.szPClasName;
}
t
{
this.szPClasName = value;
}
}
public Size Size
{
get
{
return this.size;
}
t
{
this.size = value;
}
}
public List<string> Windows
{
get
{
return this.windows;
}
t
{
this.windows = value;
}
}
public int Index
广告优化{
get
{
return this.index;
}
t
{
this.index = value;
}
}
}
窗体识别WinApi实现代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinApiDemo.Common
{
/// <summary>
/// 窗体识别
/// </summary>
public class HWNDHelper
{
public struct POINTAPI
{
public int X;
public int Y;
}
public struct Rect
{
public int Width
{
get
{
含鸟的诗句return this.right - this.left;
}
}
public int Height
{
get
{
return this.bottom - p;
}
}
public int left;
public int top;
public int right;
public int bottom;
}
private static List<IntPtr> wndList = new List<IntPtr>();
private static List<IntPtr> list = new List<IntPtr>();
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);        public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
[DllImport("ur32.dll")]
public static extern IntPtr GetCursorPos(ref POINTAPI lpPoint);        [DllImport("ur32.dll")]
public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);        [DllImport("ur32.dll")]
[DllImport("ur32.dll")]
public static extern int UpdateWindow(IntPtr hWnd);
[DllImport("ur32.dll")]
public static extern int RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);
[DllImport("ur32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int lParam, int nMaxCount, StringBuilder lpString);
[DllImport("ur32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
[DllImport("ur32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("ur32.dll")]
public static extern int ReleaDC(IntPtr hwnd, IntPtr hdc);
[DllImport("ur32.dll")]
public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);
[DllImport("ur32.dll")]
public static extern IntPtr GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("ur32.dll")]
public static extern bool SetSystemCursor(IntPtr hcur, uint id);
[DllImport("ur32.dll")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
[DllImport("ur32.dll", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
拉拉队英文[DllImport("ur32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
[DllImport("ur32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);        [DllImport("ur32.dll")]
public static extern IntPtr GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCont);
[DllImport("ur32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
///
/// </summary>
/// <returns></returns>
public static IntPtr WindowFromPoint()
{
POINTAPI pointapi = default(POINTAPI);
GetCursorPos(ref pointapi);
return WindowFromPoint(pointapi.X, pointapi.Y);
}
/// <summary>
///
/// </summary>
/// <param name="hWnd"></param>
public static void Refresh(IntPtr hWnd)
{
InvalidateRect(hWnd, IntPtr.Zero, 1);
UpdateWindow(hWnd);
RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, 1409U);
}
/// <summary>
///
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
public static string GetWindowTextByMessage(IntPtr hwnd)
{
StringBuilder stringBuilder = new StringBuilder(256);微信英文昵称
SendMessage(hwnd, 13, 256, stringBuilder);
return stringBuilder.ToString();
}
/// <summary>
/// </summary>
/// <param name="hWnd"></param>
/// <param name="tips"></param>
public static void Highlight(IntPtr hWnd, string tips)
{
Rect rect = default(Rect);
GetWindowRect(hWnd, ref rect);
IntPtr windowDC = GetWindowDC(hWnd);
if (windowDC != IntPtr.Zero)
{
using (Pen pen = new Pen(Color.Green, 3f))
{
using (Graphics graphics = Graphics.FromHdc(windowDC))
{
Font font = new Font("微软雅⿊", 10f, FontStyle.Bold);
graphics.DrawRectangle(pen, 0, 0, rect.right - rect.left - 3, rect.bottom - p - 3);                        graphics.DrawString(tips, font, Brushes.Red, 10f, 10f);
}
}
}
ReleaDC(hWnd, windowDC);
}
天龙屯堡/// <summary>
///
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
public static string GetWindowText(IntPtr hwnd)
{
StringBuilder stringBuilder = new StringBuilder(256);
GetWindowText(hwnd, stringBuilder, 256);
return stringBuilder.ToString();
}
/// <summary>
///
/// </summary>
public static void SetSystemCursor()
{
SetSystemCursor(Cursors.SizeAll.CopyHandle(), 32512U);
}
/// <summary>
///
/
空心菜的功效// </summary>
public static void SystemParametersInfo()
{
SystemParametersInfo(87U, 0U, IntPtr.Zero, 2U);
}
/// <summary>
///
/// </summary>
/// <param name="hwnd"></param>
/// <param name="isGetDeskAll"></param>
/// <returns></returns>
public static IntPtr GetParent(IntPtr hwnd, bool isGetDeskAll = true)
{
if (isGetDeskAll)
{
GetAllDesktopWindows();
}
IntPtr parent = GetParent(hwnd);
if (parent == IntPtr.Zero || parent == hwnd || wndList.Contains(hwnd))
{
return hwnd;
}

本文发布于:2023-07-13 01:04:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1093450.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:窗体   识别   实现   队歌   广告   诗句   昵称   空心菜
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图