首页 技术 正文
技术 2022年11月24日
0 收藏 698 点赞 4,237 浏览 4527 个字

1、要善用spy++

2、不同的控件主要靠GetDlgCtrlID去区分

3、要获得另一个进程的焦点窗口(GetFocus)需要调用AttachThreadInput

4、尽量少用keybd_event模拟键盘输入,主要是该函数不能保证按键消息一定能被特定进程接收到。取而代之的是SendMessage(hwnd, WM_IME_CHAR, …)。而模拟回车按下要用PostMessage (hFocus, WM_KEYDOWN, VK_RETURN, 0),SendMessage不知为何不起作用

5、通达信在按下第一个数字键后会弹出键盘精灵,此时焦点窗口会转移到键盘精灵上,要重新调用GetFocus一次

6、GetWindow(hMainWnd, GW_ENABLEDPOPUP)可以获得当前的弹出窗口句柄

7、PostMessage(hPopup, WM_COMMAND, MAKEWPARAM(IDOK, BN_CLICKED), NULL),模拟“确定”键被按下

8、EnumChildWindows函数可以枚举一个窗口的所有子窗口

class ThreadHandler
{
#region P/Invoking and constants definition
const uint WM_GETTEXT = 0x000D; [DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd); [DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, uint lpdwProcessId = ); delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,
IntPtr lParam); [DllImport("user32.dll")]
static extern bool AttachThreadInput(int idAttach, int idAttachTo, bool fAttach); [DllImport("kernel32.dll")]
static extern int GetCurrentThreadId(); [DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam); #endregion public readonly string ProcessName, WindowName;
protected readonly int TargetThreadID, CurrentThreadID;
protected readonly IntPtr TargetWindowHandle; public ThreadHandler(string processName, string windowName)
{
CurrentThreadID = GetCurrentThreadId();
ProcessName = processName;
WindowName = windowName; object[] objs = GetWindowThread(processName, windowName);
if (objs == null)
{
throw new ArgumentException("Could not find the specified process/window.");
} TargetThreadID = (int)objs[];
TargetWindowHandle = (IntPtr)objs[];
} public ThreadHandler(string processName)
{
CurrentThreadID = GetCurrentThreadId();
ProcessName = processName; var processes = Process.GetProcessesByName(ProcessName);
if (processes.Length == )
{
throw new ArgumentException("Could not find the specified process.");
}
var appProc = processes[]; WindowName = appProc.MainWindowTitle;
TargetThreadID = GetWindowThreadProcessId(appProc.MainWindowHandle);
TargetWindowHandle = appProc.MainWindowHandle;
} public bool AttachThreadInput()
{
return AttachThreadInput(CurrentThreadID, TargetThreadID, true);
} public bool DetachThreadInput()
{
return AttachThreadInput(CurrentThreadID, TargetThreadID, false);
} public void SetFocus()
{
SetFocus(TargetWindowHandle);
} static object[] GetWindowThread(string processName, string windowName)
{
var processes = Process.GetProcessesByName(processName);
if (processes.Length > )
{
//Fill a list of handles
var handles = new List<IntPtr>();
foreach (ProcessThread thread in processes[].Threads)
EnumThreadWindows(thread.Id,
(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero); //Create a stringbuilder to function as storage unit
StringBuilder nameBuffer = new StringBuilder();
foreach (var hWnd in handles)
{
//And finally compare the caption of the window with the requested name
nameBuffer.Clear();
SendMessage(hWnd, WM_GETTEXT, nameBuffer.Capacity, nameBuffer);
if (nameBuffer.ToString() == windowName)
{
return new object[] { GetWindowThreadProcessId(hWnd), hWnd };
}
}
}
return null;
}
}
static void Main(string[] args)
{
Console.WriteLine("Please input the name of the process to hook: ");
string pName = Console.ReadLine();
Console.WriteLine("Input the name of a specific window, or leave blank: ");
string pWnd = Console.ReadLine();
ThreadHandler threadHandler;
try
{
if(!String.IsNullOrWhiteSpace(pWnd))
threadHandler = new ThreadHandler(pName, pWnd);
else
threadHandler = new ThreadHandler(pName);
}
catch
{
Console.WriteLine("Error: " + pName +" does not seem to be running.");
Console.ReadKey();
return;
} if (!threadHandler.AttachThreadInput())
{
Console.WriteLine("Error: The application tried to attach its Input Processing Mechanism to " + threadHandler.ProcessName + ", but failed.");
Console.ReadKey();
return;
}
Console.WriteLine("Input Processing Mechanism correctly attached to " + threadHandler.ProcessName + ".");
threadHandler.SetFocus();
InputSimulator.SimulateTextEntry("test"); //InputSimulator is a seemingly famous SendInput wrapper. Replacing this line with the code for a keystroke also doesn't work.
Console.ReadLine();
Console.WriteLine("Detaching Input Processing Mechanism.");
threadHandler.DetachThreadInput();
}

以上代码尚不能工作,

You should be able to use SetFocus to give focus to the control you are sending the keystrokes to.

SendMessage and PostMessage can also be used to send keystrokes, but it’s BAD PRACTICE and should be avoided.

Check out System.Windows.Forms.SendKeys for information on sending keystrokes though the Forms class in .NET.

In a lot of cases, if you don’t need the keystrokes themselves, you can just change the text on a window using SendMessage with WM_SETTEXT if this is what you’re looking to do.

http://answer.techwikihow.com/334694/sendinput-working-attaching-thread-input-target-process.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,727
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,331
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,158
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:5,978
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,620
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,639