-
1
- #1
Stretchwickster
Programmer
This technique uses wm_CopyData and provides an easy means of sending data between applications. The sample code given is by no means bullet-proof - so adapt it at your leisure!!! The code passes a filename from Form1 of one application to MyForm of another application which loads the specified file into a RichEdit box.
Here are the steps you need to implement:
1) Ensure the Messages unit is in the Uses section of both applications .
2) In the sending application, place the following code in the OnClick event of a button or wherever is appropriate for your application:
3) In the receiving application, declare the following routine:
Then add the following code to its' implementation:
procedure TMyForm.CopyData(var Msg: TWmCopyData);
var
Filename: String;
begin
// restore the window if minimized
if IsIconic(Application.Handle) then
Application.Restore;
// extract the data
Filename := PChar(Msg.CopyDataStruct.lpData);
// open the file and goto error line
try
RichEdit1.Lines.LoadFromFile(Filename);
except
MessageDlg('Invalid file', mtWarning, [mbOK], 0);
end;
end;
Clive
Here are the steps you need to implement:
1) Ensure the Messages unit is in the Uses section of both applications .
2) In the sending application, place the following code in the OnClick event of a button or wherever is appropriate for your application:
Code:
var
hwnd: THandle;
DataStruct: CopyDataStruct;
begin
// get the handle of the window
hwnd := FindWindow('TMyForm', nil);
if hwnd <> 0 then
begin
DataStruct.dwData := 0;
DataStruct.cbData := length(Ed_Filename.Text) + 1;
DataStruct.lpData := PChar(Ed_Filename.Text);
// activate the receiving window
SetForegroundWindow(hwnd);
// send the data
SendMessage(hwnd, wm_CopyData, Form1.Handle, Integer (@DataStruct));
end;
end;
3) In the receiving application, declare the following routine:
Code:
procedure CopyData(var Msg: TWMCopyData); message wm_CopyData;
procedure TMyForm.CopyData(var Msg: TWmCopyData);
var
Filename: String;
begin
// restore the window if minimized
if IsIconic(Application.Handle) then
Application.Restore;
// extract the data
Filename := PChar(Msg.CopyDataStruct.lpData);
// open the file and goto error line
try
RichEdit1.Lines.LoadFromFile(Filename);
except
MessageDlg('Invalid file', mtWarning, [mbOK], 0);
end;
end;
Clive