在Delphi中创建调用DLL示例代码
unit frmColor;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs,StdCtrls, Buttons, ColorGrd;
type
TColorFrm = class(TForm)
ColorGrid: TColorGrid;
OkBitBtn: TBitBtn;
CancelBitBtn: TBitBtn;
procedure OkBitBtnClick(Sender: TObject);
procedure CancelBitBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ColorFrm: TColorFrm;
function FBColor(Var FColor, BColor:TColor):WordBool;export;
implementation
{$R *.DFM}
{按 Ok按钮,退出颜色对话框}
procedure TColorFrm.OkBitBtnClick(Sender: TObject);
begin
ModalResult := mrOk;
end;
{按Cancel按钮,退出颜色对话框}
procedure TColorFrm.CancelBitBtnClick(Sender: TObject);
begin
ModalResult := mrCancel;
end;
function FBColor(Var FColor, BColor:TColor):WordBool;
begin
Result := False;
ColorFrm:=TColorfrm.Create(Application);
Try
if ColorFrm.ShowModal = mrOk then
with ColorFrm do
begin
FColor := ColorGrid.ForegroundColor;
BColor := ColorGrid.BackgroundColor;
Result := True;
end;
finally
ColorFrm.Free;
end;
end;
end.
library ColorLib;
uses
SysUtils,
Classes,
frmColor in 'frmColor.pas' {ColorFrm};
exports
FBColor;
{$R *.RES}
begin
end.
unit testdll;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TMainFrm = class(TForm)
Memo1: TMemo;
ColorBtn: TButton;
ExitBtn: TButton;
procedure ColorBtnClick(Sender: TObject);
procedure ExitBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainFrm: TMainFrm;
implementation
{$R *.DFM}
{声明ColorLib.Dll中的函数}
function FBColor(var FColor, BColor:TColor):WordBool;Far;
external 'Colorlib.dll';
{弹出ColorLib.dll中的颜色对话框 }
procedure TMainFrm.ColorBtnClick(Sender: TObject);
var
FColor, BColor: TColor;
begin
FColor := Font.Color; {窗体字体颜色}
BColor := Color; {窗体背景颜色}
if FBColor(FColor,BColor) then
begin
Memo1.Color := BColor; {使用窗体背景色}
Memo1.Font.Color := FColor; {使用字体颜色}
end;
end;
procedure TMainFrm.ExitBtnClick(Sender: TObject);
begin
close;
end;
end.