开发者

SendMessage to Firefox, on cursor position

The code below sometimes works: it inserts text f开发者_StackOverflow社区rom 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?


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 using SendMessage() is definitely not the way to go.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜