sendunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:1k
- unit sendunit;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Buttons;
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Label1: TLabel;
- BitBtn1: TBitBtn;
- procedure BitBtn1Click(Sender: TObject);
- private
- { Private declarations }
- public
- procedure SendData;
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.SendData; //发送消息和数据过程
- var
- Copydatastruct: TCopyDataStruct; //定义一个Tcopydatastruct类型的变量
- hwnd: THandle; //句柄类型,用于存储目标对象的句柄
- begin
- Copydatastruct.cbData := Length (Edit1.Text) + 1;
- //为传递的数据区分配内存并把要传送的字符串放入数据区
- GetMem (Copydatastruct.lpData, Copydatastruct.cbData );
- StrCopy (Copydatastruct.lpData, PChar (Edit1.Text));
- // 获得接收窗口的句柄
- Hwnd := FindWindow (nil, '利用WM_COPYDATA进行数据交换演示Receive ');
- // 如果目标对象存在,则向其发送WM_COPYDATA消息
- if Hwnd <> 0 then
- SendMessage (Hwnd, WM_COPYDATA, Handle, Cardinal(@Copydatastruct))
- else
- ShowMessage ('目标对象窗口没找到!');
- //释放资源
- FreeMem (Copydatastruct.lpData);
- end;
- procedure TForm1.BitBtn1Click(Sender: TObject);
- begin
- if Edit1.Text<>'' then
- SendData;
- end;
- end.