?
Solved

WM_COPY Not working in other process?

Posted on 2003-03-26
10
Medium Priority
?
1,217 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
  • 2
  • 2
  • 2
  • +2
8 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

621 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