1.动态创建条形码,需要在FastReport中写脚本,我写的脚本附在后面。
2.需要定义条形码点击事件,以便用户点出设置条形码的属性
3.定义进行条形码设置的对话窗,把需要设置的属性定义在对话窗中。
具体步骤:FastReport中右击Page1(页面1)->New Dialog,在其中加入需要设置属性的编辑框,工具栏里基本都有。将DialogPage1的Visible属性设置为False,否则预览即显示对话。
在Page1的OnBeforePrint事件中,添加代码->动态创建BarCode
procedure Page1OnBeforePrint(Sender: TfrxComponent);
var barCode:TFrxBarCodeView;
begin
tt:=false;
barCode:= TFrxBarCodeView.Create(Page1);
barCode.width:=200;
BarCode.Height:=60;
BarCode.Parent:=Page1;
barCOde.Left:=18;
BarCode.Top:=12;
barCode.OnPreviewCLick:= 'BarCodeClick';
end;
procedure BarCodeClick(Sender: TfrxView; Button: TMouseButton;
Shift: Integer; var Modified: Boolean);
begin
DialogPage1.ShowModal;
with dialogpage1 do
begin
if ModalResult = mrOK then
begin
//showmessage(edit1.Text+#13+edit2.Text+#13+Edit3.Text);
//此处加入条形码属性修改代码
end;
end;
end;
方法二已经验证通过 在panel上绘制二维码
unit panelCanvas;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
pnl5: TPanel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function getRandomString(strlength:Integer=6):string; //生成随机字符串
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Math,frxBarcod, frxBarcode , frxBarcode2D ,frxClass; //frxBarcodeQR,
function getRandomString(strlength:Integer=6):string;
var
SourceStr,str:string;
i:integer;
begin
SourceStr:='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
randomize;
for i:=1 to strlength do
str:=str+sourcestr[Random(62)+1];
result := str;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
cvs: TCanvas;
x : Tfrxbarcodeview;
EWM : TfrxBarcode2DView ;
begin
pnl5.Repaint; //重绘
cvs := TCanvas.Create;
cvs.Handle := GetDC(pnl5.Handle);
// cvs.Pen.Width := 2;
// cvs.Pen.Color := clRed;
// cvs.Brush.Color := clYellow;
// cvs.Rectangle(5, 5, 50, 30);
EWM := TfrxBarcode2DView.Create(self);
EWM.BarType := bcCodeQR;
EWM.Text := 'wode祖国181231250';
// EWM.DataField := 'vou_no';
// EWM.DataSet := frxDBDataset1;
// ewm.ShowText := false; 不显示Text内容
EWM.Height := 50;
EWM.Width := 50;
ewm.AutoSize := False;
EWM.StretchMode := smActualHeight;
EWM.Draw(cvs,3,3,300,5);
x := Tfrxbarcodeview.Create(self);
x.BarType := bcCode128 ;
x.Expression := '12345678';
// x.DataField := 'vou_no';
// x.DataSet := frxDBDataset1;
// x.ShowText := false; 不显示Text内容
x.Height := 30;
x.Width := 50;
x.Draw(cvs,3,6,15,5);
x.Free;
EWM.Free;
cvs.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
cvs: TCanvas;
x : Tfrxbarcodeview;
EWM : TfrxBarcode2DView ;
str1 ,str2 :string;
begin
pnl5.Repaint; //重绘
cvs := TCanvas.Create;
cvs.Handle := GetDC(pnl5.Handle);
EWM := TfrxBarcode2DView.Create(self);
EWM.BarType := bcCodeQR;
str1 := getRandomString(6);
EWM.Text := getRandomString(6);
// EWM.DataField := 'vou_no';
// EWM.DataSet := frxDBDataset1;
// ewm.ShowText := false; 不显示Text内容
EWM.Height := 50;
EWM.Width := 50;
ewm.AutoSize := False;
EWM.StretchMode := smActualHeight;
EWM.Draw(cvs,3,3,500,5);
x := Tfrxbarcodeview.Create(self);
x.BarType := bcCode128 ;
str2 := getRandomString(8);
x.Text := str2;
// x.DataField := 'vou_no';
// x.DataSet := frxDBDataset1;
// x.ShowText := false; 不显示Text内容
x.Height := 30;
x.Width := 50;
x.Draw(cvs,3,6,20,40);
x.Free;
EWM.Free;
cvs.Free;
end;
end.
end.