🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.borland-delphi
3 messages
3 total messages Started by aSp Wed, 15 Oct 2003 09:07
Get Selected Text from another application
#1930
Author: aSp
Date: Wed, 15 Oct 2003 09:07
85 lines
4489 bytes
Hi all,

First up let me apologise for the cross-posting, I am not quite sure which
newsgroup this would fit into (there are quite a few)..
I am a relative "n00b" to Delphi, and indeed programming and mainly learning
out of curiosity.

What I want to do is this. My application will be minimized. The user
selects a word in another application (word, outlook, IE, etc) then they
press a hot key my application picks up that word and does something with
it.

Thanks to searching the news groups I have found a few posts (mainly Peter
Below's) and have got some semi working code. The application picks up the
word from most applications. The problem I am running into is with Internet
explorer and Outlook/express. I believe it has something to do with the fact
that IE and outlook/express treat the text as HTML (even if it is just text)
or something like that, as if I turn the email to "Plain Text" my program
works.. If not.. It doesn't pick up the word..

Here is the block of code I am having trouble with.. Any
suggestions/improvements would be greatly appreciated.

procedure tform1.wmhotkey( var msg: TWMHotkey );
var
  buf: array [0..256] of char;
  hOtherWin, hFocusWin: HWND;
  OtherThreadID: Integer;
  aDwordVar: DWORD;
  lookup_word: string;
  lookup_url: pchar;
  begin
  clipboard.clear;
  hOtherWin := GetForegroundWindow;
  OtherThreadID := GetWindowThreadProcessID( hOtherWin, @aDwordvar);
  If AttachThreadInput( GetCurrentThreadID, OtherThreadID, True ) Then
  Begin
    hFocusWin := GetFocus;
    If hFocusWin <> 0 Then
      begin
      SendMessage( hFocusWin, WM_COPY, 0, 0 );
      lookup_word:= clipboard.astext;
      edit1.Text := lookup_word;
     if msg.HotKey = 1 then
     begin
        //DO STUFF
      end
     else
       //DO SOMETHING ELSE
     end;

      sleep(200);
      ShellExecute(0,'open',lookup_url,nil,nil,SW_SHOWNORMAL);
    try
    finally
      AttachThreadInput( GetCurrentThreadID, OtherThreadID, False );
    end;

  end;
  End;

It seems to be "doing stuff / Other stuff" fine.. as long as a word is
selected.. Also another problem I seem to be running into is I want to test
to make sure there is text in the clipboard before continuing, and if there
is not display a message. I have tried using   if
clipboard.hasformat(CF_TEXT) then blah blah.. But this doesn't seem to work
exactly as I would like it. It works if MY application has focus, but not if
the OTHER application has focus (Perhaps due to the  hOtherWin :=
GetForegroundWindow;
  OtherThreadID := GetWindowThreadProcessID( hOtherWin, @aDwordvar);
  If AttachThreadInput( GetCurrentThreadID, OtherThreadID, True ) Then
  Begin
    hFocusWin := GetFocus;
    If hFocusWin <> 0 Then
part??)

Once again any help would be GREATLY appreciated, I've been searching and
trying to come up with a solution, but seem to be banging my head against a
wall...

Thanks in advance
David
(ps, sorry about the long post)
a-s-p$di-g-ic-orp-s-e .co(m)      (remove the -'s change the $ to @ and
replace com with net)
Re: Get Selected Text from another application
#1931
Author: Kevin
Date: Tue, 28 Oct 2003 10:56
115 lines
5435 bytes
Hi
    The code seems fine!
Take a look at "AlwaysNetSearch" @ http://www.kabware.com this program does
exactly what you are describing but suffers the same restrictions with HTML
documents. Strangely the same problem occurs with a program called "TextPad"
in this case I think it's due the non-standard way "TextPad" handles the
WM_COPY message.

Haven't got time now to look into it now, but you could trying leeching IE a
window at a time getting the handle of each window obviously when the right
window found ask for the selected text. That might work, if it does let me
know.

p.s. would appreciate any thought you have on "AlwaysNetSearch" :)


Kevin

"aSp" <abuse@aol.com> wrote in message news:3f8d0d24@quokka.wn.com.au...
> Hi all,
>
> First up let me apologise for the cross-posting, I am not quite sure which
> newsgroup this would fit into (there are quite a few)..
> I am a relative "n00b" to Delphi, and indeed programming and mainly
learning
> out of curiosity.
>
> What I want to do is this. My application will be minimized. The user
> selects a word in another application (word, outlook, IE, etc) then they
> press a hot key my application picks up that word and does something with
> it.
>
> Thanks to searching the news groups I have found a few posts (mainly Peter
> Below's) and have got some semi working code. The application picks up the
> word from most applications. The problem I am running into is with
Internet
> explorer and Outlook/express. I believe it has something to do with the
fact
> that IE and outlook/express treat the text as HTML (even if it is just
text)
> or something like that, as if I turn the email to "Plain Text" my program
> works.. If not.. It doesn't pick up the word..
>
> Here is the block of code I am having trouble with.. Any
> suggestions/improvements would be greatly appreciated.
>
> procedure tform1.wmhotkey( var msg: TWMHotkey );
> var
>   buf: array [0..256] of char;
>   hOtherWin, hFocusWin: HWND;
>   OtherThreadID: Integer;
>   aDwordVar: DWORD;
>   lookup_word: string;
>   lookup_url: pchar;
>   begin
>   clipboard.clear;
>   hOtherWin := GetForegroundWindow;
>   OtherThreadID := GetWindowThreadProcessID( hOtherWin, @aDwordvar);
>   If AttachThreadInput( GetCurrentThreadID, OtherThreadID, True ) Then
>   Begin
>     hFocusWin := GetFocus;
>     If hFocusWin <> 0 Then
>       begin
>       SendMessage( hFocusWin, WM_COPY, 0, 0 );
>       lookup_word:= clipboard.astext;
>       edit1.Text := lookup_word;
>      if msg.HotKey = 1 then
>      begin
>         //DO STUFF
>       end
>      else
>        //DO SOMETHING ELSE
>      end;
>
>       sleep(200);
>       ShellExecute(0,'open',lookup_url,nil,nil,SW_SHOWNORMAL);
>     try
>     finally
>       AttachThreadInput( GetCurrentThreadID, OtherThreadID, False );
>     end;
>
>   end;
>   End;
>
> It seems to be "doing stuff / Other stuff" fine.. as long as a word is
> selected.. Also another problem I seem to be running into is I want to
test
> to make sure there is text in the clipboard before continuing, and if
there
> is not display a message. I have tried using   if
> clipboard.hasformat(CF_TEXT) then blah blah.. But this doesn't seem to
work
> exactly as I would like it. It works if MY application has focus, but not
if
> the OTHER application has focus (Perhaps due to the  hOtherWin :=
> GetForegroundWindow;
>   OtherThreadID := GetWindowThreadProcessID( hOtherWin, @aDwordvar);
>   If AttachThreadInput( GetCurrentThreadID, OtherThreadID, True ) Then
>   Begin
>     hFocusWin := GetFocus;
>     If hFocusWin <> 0 Then
> part??)
>
> Once again any help would be GREATLY appreciated, I've been searching and
> trying to come up with a solution, but seem to be banging my head against
a
> wall...
>
> Thanks in advance
> David
> (ps, sorry about the long post)
> a-s-p$di-g-ic-orp-s-e .co(m)      (remove the -'s change the $ to @ and
> replace com with net)
>
>
Re: Get Selected Text from another application
#1932
Author: J French
Date: Wed, 29 Oct 2003 10:40
18 lines
1957 bytes
Instead of sending the WM_COPY message
- why not simulate the [Ctl C]  ?

Keybd_Event

On Wed, 15 Oct 2003 17:07:51 +0800, "aSp" <abuse@aol.com> wrote:

>Hi all,
>
>First up let me apologise for the cross-posting, I am not quite sure which
>newsgroup this would fit into (there are quite a few)..
>I am a relative "n00b" to Delphi, and indeed programming and mainly learning
>out of curiosity.
>
>What I want to do is this. My application will be minimized. The user
>selects a word in another application (word, outlook, IE, etc) then they
>press a hot key my application picks up that word and does something with
>it.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads