Delphi DLL 中的窗体能接收到消息吗
1个回答
展开全部
可以,只要dll窗口已经创建,刚测试过简单的例子,看用的上吗?
{Dll:}
library Project1;
uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
exports
showform;
begin
end.
{Dll中的窗体unit1;caption命名为:我是dll创建的窗体:}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
private
{ Private declarations }
public
procedure MyMessage(var msg:TWmCopyData);message WM_CopyData;
{ Public declarations }
end;
var
Form1: TForm1;
procedure showform;stdcall;
implementation
{$R *.dfm}
procedure showform;stdcall;
begin
Form1:=Tform1.create(application);
Form1.show;
end;
procedure TForm1.MyMessage(var msg: TWmCopyData);
begin
Label1.Caption:=StrPas(msg.CopyDataStruct^.lpData);
end;
end.
{调用的窗体:}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1= class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
Procedure showform;stdcall;external 'Project1.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
H:HWND;
ds:TCopyDatastruct;
msg:string;
begin
ShowForm; {调用dll,创建窗体}
msg:='hello jack!';
ds.cbData := Length(msg)+1;
GetMem(ds.lpData,ds.cbData);
StrCopy(ds.lpData,PChar(msg));
H :=FindWindow(nil,'我是dll创建的窗体'); {dll中窗体的caption不要和已打开的窗体重名}
if H<>0 then
SendMessage(H,WM_COPYDATA,0,Cardinal(@ds)); {发送msg,dll中窗体的label1成功看到hello jack!}
FreeMem(ds.lpData);
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询