在光标位置向 Firefox 发送消息
下面的代码有时可以工作:它从 tEdit
插入文本,但仅限于“Notepad”、“Word”、“ICQ”。 Firefox 或 Google Chrome 等软件无法使用它。 我应该怎么办?
var
Pos: TPoint;
Target: HWND;
...
if not GetCursorPos(Pos) then
RaiseLastOSError;
Target := WindowFromPoint(Pos);
if Target<>0 then
SendMessage(Target, EM_REPLACESEL, ord(True), LPARAM(PChar(Edit1.Text)));
就是这样!我找到了我需要的代码
procedure SendText(ds:string);
var
TI: TInput;
KI: TKeybdInput;
i: integer;
begin
TI.Itype := INPUT_KEYBOARD;
for i := 1 to Length(ds) do
begin
KI.wVk := Ord(UpCase(ds[i]));
KI.dwFlags := 0;
TI.ki := KI;
SendInput(1, TI, SizeOf(TI));
KI.dwFlags := KEYEVENTF_KEYUP;
TI.ki := KI;
SendInput(1, TI, SizeOf(TI));
end;
end;
但问题是 - 我无法使用 SendInpit(Edit1.Text) 复制俄语(西里尔文)符号;有什么建议吗?
The code below sometimes works: it inserts text from tEdit
, but only in "Notepad", "Word", "ICQ". Such software like Firefox or Google Chrome doesn't work with it.
What should I do?
var
Pos: TPoint;
Target: HWND;
...
if not GetCursorPos(Pos) then
RaiseLastOSError;
Target := WindowFromPoint(Pos);
if Target<>0 then
SendMessage(Target, EM_REPLACESEL, ord(True), LPARAM(PChar(Edit1.Text)));
That's it! I have found the code i needed
procedure SendText(ds:string);
var
TI: TInput;
KI: TKeybdInput;
i: integer;
begin
TI.Itype := INPUT_KEYBOARD;
for i := 1 to Length(ds) do
begin
KI.wVk := Ord(UpCase(ds[i]));
KI.dwFlags := 0;
TI.ki := KI;
SendInput(1, TI, SizeOf(TI));
KI.dwFlags := KEYEVENTF_KEYUP;
TI.ki := KI;
SendInput(1, TI, SizeOf(TI));
end;
end;
But the problem is - i can't copy russian(cyrilic) symbols using SendInpit(Edit1.Text); Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它在 Firefox 和 Chrome 中不起作用,因为您在其中看到的编辑框是由浏览器中的 HTML 引擎呈现的,而不是由操作系统呈现的。它们被称为“无窗口控件”,并且因此没有与它们关联的窗口句柄。
就操作系统而言,网页是一个大的
HWND
,其中绘制了一个网页,并且由于 HTML 引擎,一些绘制的元素恰好看起来和行为像控件。您不能使用
SendMessage()
来定位此类控件。根据您的具体计划,可能还有另一种更直接的方法来自动化浏览器。但使用SendMessage()
绝对不是正确的方法。It doesn't work in Firefox and Chrome because the edit boxes you see in them are rendered by the HTML engines in the browser and not by the operating system. They are called "windowless controls", and thus do not have a window handle associated with them.
As far as the operating system is concerned, the webpage is one big
HWND
with a webpage painted inside it, and some of the painted elements just happen to look and act like controls thanks to the HTML engine.You cannot target such controls with
SendMessage()
. Depending on exactly what you plan to do, there may be another, more direct way to automate the browser. But usingSendMessage()
is definitely not the way to go.AFAIR,Firefox 编辑框并不是真正的 Windows 原生编辑框,而是有所不同。
我可能是错的,但你不能将它们视为普通的编辑框。你需要得到他们的窗口
句柄(好吧,如果他们有一个窗口句柄)并向其发送消息。
我说的是 Firefox 的编辑框(地址栏和搜索栏)本身,而不是那些
由 HTML 呈现。
Windows Platform SDK(从 Microsoft 下载)上有一些实用程序可以帮助您识别
SendMessage 调用的正确目标。
AFAIR, Firefox editboxes aren't really Windows native editboxes but something different.
I can be wrong, but you cannot treat those as normal editboxes. You need to get their window
handle (well, if they have an window handle) and send the message to that.
And I'm talking about editboxes of Firefox (address bar and search bar) itself not the ones
rendered out of HTML.
There are utilities on Windows Platform SDK (download from Microsoft) that can help you identify
the correct target for your SendMessage calls.
您可以使用 MSAA 来完成此操作。这是一个示例: http://www.transl-gunsmoker.ru/ 2009/08/blog-post.html 在 WinSDK 中,有一个用于 MSAA 的 WinSpy 的类似版本,称为 AccExplorer32。
You can do this with MSAA. Here is an example: http://www.transl-gunsmoker.ru/2009/08/blog-post.html And in WinSDK there is an analog of WinSpy for MSAA which is called AccExplorer32.