Solved

Selected Text From an Active Window

Posted on 2004-03-30
6
342 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now