Solved

Selected Text From an Active Window

Posted on 2004-03-30
6
346 Views
Last Modified: 2012-05-04
Greeting...
How can I read selected text from an Active Window [using hot key] without the need to know title name or program name, modify the text in my program, and send it back to the same window I toke it from ?

Selected text from an active window => modify it => replace the selected text with the modify text.

please i need a clear code, i don't know much about API
thank you.
0
Comment
Question by:TheLeader
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 10712474
Hi,

uses Clipbrd;

var s: string;
begin
  // Ctrl-C
  keybd_event(VK_CONTROL, 0, 0, 0);
  keybd_event(ORD('c'), 0, 0, 0);
  keybd_event(ORD('c'), 0, KEYEVENTF_KEYUP, 0);
  keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
  if Clipboard.HasFormat(CF_TEXT) then begin // there is text in the clipboard
    s := Clipboard.AsText; // get the text
    if Length(s) > 0 then begin
      s:= 'Hello'; // modigy it here. You must write your own code instead of this line
      Clipboard.SetTextBuf(PChar(s)); // write to the clipboard
      // Ctrl-V
      keybd_event(VK_CONTROL, 0, 0, 0);
      keybd_event(ORD('V'), 0, 0, 0);
      keybd_event(ORD('V'), 0, KEYEVENTF_KEYUP, 0);
      keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
    end;
  end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 10712488
Replace the lines above

  keybd_event(ORD('c'), 0, 0, 0);
  keybd_event(ORD('c'), 0, KEYEVENTF_KEYUP, 0);

with

  keybd_event(ORD('C'), 0, 0, 0);
  keybd_event(ORD('C'), 0, KEYEVENTF_KEYUP, 0);

Sorry.
0
 
LVL 2

Author Comment

by:TheLeader
ID: 10712555
geobul, it seems (did not tested yet) your code will work perfect, but it uses clipboard
can you do it in another way, I don't want the user loses clipboard data.
thank you
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Accepted Solution

by:
geobul earned 200 total points
ID: 10722011
OK. Here it is:

function GetActiveChildWindow: HWND;
var
  h, h1: HWND;
begin
  h := GetForegroundWindow();
  if IsWindow(h) then begin
    repeat
      h1 := GetWindow(h, GW_CHILD);
      if IsWindow(h1) then begin
        if h1 <> h then h := h1;
      end else break;
    until h1 <> 0;
  end;
  result := h;
end;

function GetWindowText(h:HWND) : string;
var
 buf : string;
 len : integer;
begin
 try
   len := SendMessage(h, WM_GETTEXTLENGTH,0,0);
   SetLength(buf,len);
   SendMessage(h, WM_GETTEXT, len+1, Integer(buf));
   result := buf;
 except
   result := '';
 end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  s: string;
  h: HWND;
  StartPos, EndPos: integer;
  res: integer;
begin
  h := GetActiveChildWindow; // get active control
  if IsWindow(h) then begin
    s := GetWindowText(h); // get the whole text
    if Length(s) > 0 then begin
      res := SendMessage(h, EM_GETSEL, 0, 0); // get selected text coordinates
      StartPos := LOWORD(res);
      EndPos := HIWORD(res);
      s := Copy(s, StartPos, EndPos - StartPos); // get the selected text
      if Length(s) > 0 then begin // there is selected text
        s:= 'Hello'; // modify it
        SendMessage(h, EM_REPLACESEL, 0, Integer(PChar(s))); // replace the selection
      end;
    end;
  end;
end;

Regards, Geo
0
 
LVL 2

Author Comment

by:TheLeader
ID: 10740229
Perfect :)
do you suggest any book, website, or article to help as you wrote this code ?
thank you.
0
 
LVL 17

Expert Comment

by:geobul
ID: 10742324
You are welcome :-)

Except GetWindowText function which I already had I wrote the rest using Delphi help (Win32 Programmer's Reference mainly) and the MSDN site http://msdn.microsoft.com 

Regards, Geo
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 154
Print Graphic and Text to Epson TM-T88v 12 367
find a node in VST 2 70
scroll down TListBox component in Delphi 1 19
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question