Solved

Selected Text From an Active Window

Posted on 2004-03-30
6
345 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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