WM_CHAR 不适用于 notepad.exe [英] WM_CHAR doesn't work with notepad.exe

查看:34
本文介绍了WM_CHAR 不适用于 notepad.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Windows 7 和 Microsoft Visual Studio 2010.我使用此代码将数字字符插入到 calc.exe 的窗口中:

I use Windows 7 and Microsoft Visual Studio 2010. I use this code to insert digit chars to the window of calc.exe:

STARTUPINFO         si  = { 0 };
PROCESS_INFORMATION pi  = { 0 };

memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));

si.cb = sizeof(si);

BOOL bResult = CreateProcess("c:\\windows\\syswow64\\calc.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForInputIdle(pi.hProcess, INFINITE);
HWND hWnd = FindWindow("CalcFrame", NULL);

PostMessage(hWnd, WM_CHAR, (WPARAM)'1', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'2', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'3', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'4', 0);

这段代码完美无缺.当我用 "c:\\windows\\syswow64\\notepad.exe" 替换 "c:\\windows\\syswow64\\calc.exe""CalcFrame" 使用 "Notepad" 它不会将字符插入记事本窗口.

This code works perfect. And when I replace "c:\\windows\\syswow64\\calc.exe" with "c:\\windows\\syswow64\\notepad.exe" and "CalcFrame" with "Notepad" it doesn't insert chars into the Notepad window.

推荐答案

在记事本主窗口的客户区中有一个 EDIT 类的子窗口.对于您正在执行的操作,您需要定位并发送消息到那个窗口,而不是记事本的主窗口.

There is a child window of class EDIT inside the client area of Notepad's main window. For what you are doing, you need to locate and send messages to that window rather than Notepad's main window.

BOOL bResult = CreateProcess("c:\\windows\\syswow64\\notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForInputIdle(pi.hProcess, INFINITE);
HWND hWnd = FindWindow("Notepad", NULL);
hWnd = FindWindowEx(hWnd, NULL, "EDIT", NULL); // <-- add this

PostMessage(hWnd, WM_CHAR, (WPARAM)'1', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'2', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'3', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'4', 0);

使用 Spy++ 探索窗口层次结构并找到这些东西.

Use Spy++ to explore window hierarchy and find these things.

这篇关于WM_CHAR 不适用于 notepad.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆