?
Solved

WM_COPY Not working in other process?

Posted on 2003-03-26
10
Medium Priority
?
1,186 Views
Last Modified: 2007-12-19
Hi!


PostMessage(getForeGroundWindow, WM_COPY , 0, 0);

and

PostMessage(getForeGroundWindow, WM_CLOSE , 0, 0);



The WM_CLOSE close any active application,
but how come WM_COPY doesnt copy the selected text at all ??
And for sure, WM_PASTE doesnt work to! Help me please!

What I want is to copy the selected text when you hit like Control+1, ( that part is done and is fully working ).. The Copy to Clipboard thing is missing..

I could set the Selected Text into a var, that would be a lot better, but i think its really harder than simply copy the selectedtext to clipboard, and then assign it to a var..

If you have any suggestions, feel free to reply:D

PS.
Getting the current selected text without using the ClipBoard would be very apreciated, thx!


- Morg0thz.
0
Comment
Question by:hamowe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
10 Comments
 

Author Comment

by:hamowe
ID: 8211232
I also tried with

PostMessage(ActiveControl.Handle, WM_COPY , 0, 0);
And it still only works only on my form..
0
 
LVL 6

Assisted Solution

by:DaFox
DaFox earned 664 total points
ID: 8211424
Hi.

Try something like:

procedure TForm1.WMHotKey(var Msg: TWMHotkey);
var
  buf: array [0..256] of char;
  hOtherWin, hFocusWin: hWnd;
  OtherThreadID: Integer;
  aDwordVar: DWord;
begin
  hOtherWin := GetForegroundWindow;
  OtherThreadID := GetWindowThreadProcessID(hOtherWin, @aDwordvar);
  If AttachThreadInput(GetCurrentThreadID, OtherThreadID, True) then
  begin
    hFocusWin := GetFocus;
    If hFocusWin <> 0 then SendMessage( hFocusWin, WM_COPY, 0, 0 );
      edit3.Text := Clipboard.astext;
    try
    finally
      AttachThreadInput(GetCurrentThreadID, OtherThreadID, false);
    end;
  end;
end;


Markus
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8211606
this will work only on windowed controls (for examp. web pages TEXTAREA will not copy..):

function GetFocusedText: string;
var
  dwFocusWnd: dword;
  hc: THandle;
  s: string;
begin
  dwFocusWnd := GetWindowThreadProcessId(GetForegroundWindow, nil);
  AttachThreadInput(MainThreadID, dwFocusWnd, True);
  try
    SendMessage(GetFocus, WM_COPY, 0, 0);
  finally
    AttachThreadInput(MainThreadID, dwFocusWnd, False);
  end;
  if OpenClipboard(0) then
  try
    hc := GetClipboardData(CF_TEXT);
    try
      if hc <> 0 then  Result := PChar(GlobalLock(hc))
        else  Result := '';
    finally
      if hc <> 0 then GlobalUnlock(hc);
    end;
  finally
    CloseClipboard;
  end;
end;

wbr, mo.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:mocarts
mocarts earned 664 total points
ID: 8211629
hmm.. Markus :)
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8211710
@mo ;)
BTW: You're right. It doesn't work for all applications.

Markus
0
 
LVL 1

Expert Comment

by:DavidRissato
ID: 8213821
You can do it by this way:
uses Clipbrd;

procedure TForm1.Panel1Click(Sender: TObject);
var
  iCount : integer;
begin
  for iCount := Application.ComponentCount-1 downto 0 do
    begin
      if Application.Components[iCount].InheritsFrom(TForm) then
        with (Application.Components[iCount] as TForm) do
          if Active then
            begin
              SendMessage(ActiveControl.Handle, WM_COPY , 0, 0);
              ShowMessage(Clipboard.AsText);
              Exit;
            end;
    end;
end;

a) Clipbrd is a VCL unit only. For clx projects use QClipbrd.

b) Do not PostMessage because it sends the message to application queue and therefor it will execute after the completion of your procedure. Use SendMessage instead.

c) This will take data only from your application, doesn't work if you are trying to copy data from a control in another application.


{}'s
David Rissato Cruz
0
 
LVL 1

Accepted Solution

by:
DavidRissato earned 672 total points
ID: 8213846
Oh, i forgot to mention:

If you do this way, you can even select from what kinds of control you want to borrow data, just change the line:

if Active then

by

if Active and
   ActiveControl.inheritsFrom(TEdit) and
   ActiveControl.Name = "Edit1" then

and so on...

{}'s
David Rissato Cruz
0
 

Expert Comment

by:CleanupPing
ID: 9316593
hamowe:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

801 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