Solved

Selected Text From an Active Window

Posted on 2004-03-30
6
341 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

13 Experts available now in Live!

Get 1:1 Help Now