不说了,楼上的兄弟们都说的差不多了,我就来贴代码吧。
//主程序代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
WM_MYMSG = WM_USER + 2008;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FDLLHandle: THandle;
procedure ShellMsgFunc(var AMsg: TMessage); message WM_MYMSG;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function ShowDLLForm(const AppHandle: THandle): THandle; stdcall;
external 'Project2.dll';
procedure TForm1.ShellMsgFunc(var AMsg: TMessage);
begin
case AMsg.WParam of
2001: {代码};
2002: {代码};
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FDLLHandle := ShowDLLForm(Handle);
SendMessage(FDLLHandle,WM_MYMSG,2001,0);
end;
end.
//DLL 代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
WM_MYMSG = WM_USER + 2008;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FDLLHandle: THandle;
procedure ShellMsgFunc(var AMsg: TMessage); message WM_MYMSG;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function ShowDLLForm(const AppHandle: THandle): THandle; stdcall;
external 'Project2.dll';
procedure TForm1.ShellMsgFunc(var AMsg: TMessage);
begin
case AMsg.WParam of
2001: {代码};
2002: {代码};
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FDLLHandle := ShowDLLForm(Handle);
SendMessage(FDLLHandle,WM_MYMSG,2001,0);
end;
end.
//DLL 中窗体代码
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
const
WM_MYMSG = WM_USER + 2008;
type
TForm2 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FAppHWND: THandle;
procedure SetAppHWND(const Value: THandle);
procedure ShellMsgFunc(var AMsg: TMessage); message WM_MYMSG;
{ Private declarations }
public
property AppHWND: THandle read FAppHWND write SetAppHWND;
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TForm2.SetAppHWND(const Value: THandle);
begin
FAppHWND := Value;
end;
procedure TForm2.ShellMsgFunc(var AMsg: TMessage);
begin
case AMsg.WParam of
2001: Caption := IntToStr(2001);
2002: {代码};
end;
end;
end.