向一个DLL中通过函数参数传递一个窗体form,怎么窗体都传过去了,窗体上的数据集却没过去? ( 积分: 10 )

  • 主题发起人 主题发起人 5411
  • 开始时间 开始时间
弄个传递窗体的小例子啊.最好窗体上再带个数据集.最好再在Dll中,操作操作数据集.
 
这种程序我在做http://www.delphibbs.com/delphibbs/dispq.asp?lid=3164214<br>不过里面巨复杂,做到这样已经比一套正式软件还复杂,而且我正在大修改,有空就做一个简单源代码,公开给研究好了
 
我的邮箱密码忘了寄不了信,想把源代码贴进论坛的笔记里面,就是上传不成功,在这里贴代码算了<br>exe部分<br><br>/////////////////Project1.dpr<br>program Project1;<br><br>uses<br> &nbsp;Forms,<br> &nbsp;Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.res}<br><br>begin<br> &nbsp;Application.Initialize;<br> &nbsp;Application.CreateForm(TForm1, Form1);<br> &nbsp;Application.Run;<br>end.<br><br><br>////////////unit1.pas<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls, DBCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;DataSource1:TDataSource;{四个控件}<br> &nbsp; &nbsp;ADOConnection1:TADOConnection;<br> &nbsp; &nbsp;ADOTable1:TADOTable;<br> &nbsp; &nbsp;Button1:TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender:TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br> &nbsp;TshowDllForm = procedure(fm:TForm1); stdcall;<br><br>var<br> &nbsp;Form1:TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender:TObject);<br>var<br> &nbsp;sdf:TshowDllForm;<br> &nbsp;h:THandle;<br>begin<br> &nbsp;h := LoadLibrary('Project2.dll');<br> &nbsp;if h &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;@sdf := GetProcAddress(h, 'showDllForm');<br> &nbsp; &nbsp;if Assigned(sdf) then<br> &nbsp; &nbsp; &nbsp;sdf(Self);<br> &nbsp;end;<br>end;<br><br>end.<br><br><br>///////////////unit1.dfm<br>object Form1: TForm1<br> &nbsp;Left = 5<br> &nbsp;Top = 174<br> &nbsp;Width = 431<br> &nbsp;Height = 103<br> &nbsp;Caption = 'Form1'<br> &nbsp;Color = clBtnFace<br> &nbsp;Font.Charset = DEFAULT_CHARSET<br> &nbsp;Font.Color = clWindowText<br> &nbsp;Font.Height = -11<br> &nbsp;Font.Name = 'MS Sans Serif'<br> &nbsp;Font.Style = []<br> &nbsp;OldCreateOrder = False<br> &nbsp;PixelsPerInch = 96<br> &nbsp;TextHeight = 13<br> &nbsp;object Button1: TButton<br> &nbsp; &nbsp;Left = 36<br> &nbsp; &nbsp;Top = 16<br> &nbsp; &nbsp;Width = 75<br> &nbsp; &nbsp;Height = 25<br> &nbsp; &nbsp;Caption = 'call dll'<br> &nbsp; &nbsp;TabOrder = 0<br> &nbsp; &nbsp;OnClick = Button1Click<br> &nbsp;end<br> &nbsp;object DataSource1: TDataSource<br> &nbsp; &nbsp;DataSet = ADOTable1<br> &nbsp; &nbsp;Left = 212<br> &nbsp; &nbsp;Top = 12<br> &nbsp;end<br> &nbsp;object ADOConnection1: TADOConnection<br> &nbsp; &nbsp;Connected = True<br> &nbsp; &nbsp;ConnectionString = <br> &nbsp; &nbsp; &nbsp;'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb;Persist Se' +<br> &nbsp; &nbsp; &nbsp;'curity Info=False'<br> &nbsp; &nbsp;LoginPrompt = False<br> &nbsp; &nbsp;Mode = cmShareDenyNone<br> &nbsp; &nbsp;Provider = 'Microsoft.Jet.OLEDB.4.0'<br> &nbsp; &nbsp;Left = 148<br> &nbsp; &nbsp;Top = 12<br> &nbsp;end<br> &nbsp;object ADOTable1: TADOTable<br> &nbsp; &nbsp;Active = True<br> &nbsp; &nbsp;Connection = ADOConnection1<br> &nbsp; &nbsp;CursorType = ctStatic<br> &nbsp; &nbsp;TableName = 'NewGoods'<br> &nbsp; &nbsp;Left = 180<br> &nbsp; &nbsp;Top = 12<br> &nbsp;end<br>end<br><br><br><br><br><br>dll部分<br>///////////Project2.dpr<br>library Project2;<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp;Forms,<br> &nbsp;Windows,<br> &nbsp;Messages,<br> &nbsp;Classes,<br> &nbsp;DB,<br> &nbsp;ADODB,<br> &nbsp;Unit1,<br> &nbsp;Unit2 in 'Unit2.pas' {Form2};<br><br>{$R *.res}<br><br>procedure showDllForm(fm:TForm1); stdcall;<br>begin<br> &nbsp;form2 := Tform2.Create(nil);<br> &nbsp;with form2 do<br> &nbsp;begin<br> &nbsp; &nbsp;dbgrid1.DataSource :=fm.DataSource1;<br> &nbsp; &nbsp;DBNavigator1.DataSource :=fm.DataSource1;<br> &nbsp; &nbsp;DBEdit1.DataSource :=fm.DataSource1;<br> &nbsp; &nbsp;show;<br> &nbsp;end; &nbsp;// with<br>end;<br><br>exports<br> &nbsp;showDllForm;<br><br>begin<br>end.<br><br><br><br>//////////unit2.pas<br>unit Unit2;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, Grids, DBGrids, DB, ADODB, ExtCtrls, DBCtrls, StdCtrls, Mask;<br><br>type<br> &nbsp;TForm2 = class(TForm)<br> &nbsp; &nbsp;DBGrid1: TDBGrid;{三个控件}<br> &nbsp; &nbsp;DBNavigator1: TDBNavigator;<br> &nbsp; &nbsp;DBEdit1: TDBEdit;<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form2:TForm2;<br><br>implementation<br><br>{$R *.dfm}<br><br>end.<br><br><br><br>///////unit2.dfm<br>object Form2: TForm2<br> &nbsp;Left = 202<br> &nbsp;Top = 140<br> &nbsp;Width = 508<br> &nbsp;Height = 312<br> &nbsp;Caption = 'Form2'<br> &nbsp;Color = clBtnFace<br> &nbsp;Font.Charset = DEFAULT_CHARSET<br> &nbsp;Font.Color = clWindowText<br> &nbsp;Font.Height = -11<br> &nbsp;Font.Name = 'MS Sans Serif'<br> &nbsp;Font.Style = []<br> &nbsp;OldCreateOrder = False<br> &nbsp;PixelsPerInch = 96<br> &nbsp;TextHeight = 13<br> &nbsp;object DBGrid1: TDBGrid<br> &nbsp; &nbsp;Left = 32<br> &nbsp; &nbsp;Top = 44<br> &nbsp; &nbsp;Width = 429<br> &nbsp; &nbsp;Height = 185<br> &nbsp; &nbsp;TabOrder = 0<br> &nbsp; &nbsp;TitleFont.Charset = DEFAULT_CHARSET<br> &nbsp; &nbsp;TitleFont.Color = clWindowText<br> &nbsp; &nbsp;TitleFont.Height = -11<br> &nbsp; &nbsp;TitleFont.Name = 'MS Sans Serif'<br> &nbsp; &nbsp;TitleFont.Style = []<br> &nbsp;end<br> &nbsp;object DBNavigator1: TDBNavigator<br> &nbsp; &nbsp;Left = 32<br> &nbsp; &nbsp;Top = 12<br> &nbsp; &nbsp;Width = 240<br> &nbsp; &nbsp;Height = 25<br> &nbsp; &nbsp;TabOrder = 1<br> &nbsp;end<br> &nbsp;object DBEdit1: TDBEdit<br> &nbsp; &nbsp;Left = 32<br> &nbsp; &nbsp;Top = 252<br> &nbsp; &nbsp;Width = 121<br> &nbsp; &nbsp;Height = 21<br> &nbsp; &nbsp;DataField = 'Name'<br> &nbsp; &nbsp;TabOrder = 2<br> &nbsp;end<br>end
 
可以把所有的打印函数做成一个通用的打印单元( .PAS)<br>然后需要打印的时候,在要工程里加入并uses这个单元不就行了。
 
这个问题我以前问过啊,我用的是RmReport,他的2.6和3.0不是兼容的,已经用2.6做了许多,可现在又要用3.0,两个不能装在一起,所以只好分工编译,把程序拆成几个DLL.
 
kinneng :<br>你好:试了一下,提示:<br>Access Voilation at Address 06B61CA6 in module 'project2.dll'. Write of address 00720061
 
To 5411:<br>试没试过我说的办法?
 
呵呵,为了这个,所以向难度挑战,你将为此付出比重新编程更大的代价,甚至失败,因为dll实在不是个好玩的东西,有些控件不能随便传递,或者不接受传递过来的其它对象。
 
to 雪狐狸:<br>2.6 和 3.0 不能装在一起的,我怎么试你的办法啊.
 
to kinneng:<br>你好,你先说你写的,我完全照搬的,只是换了一下数据库,出这个错误'Access Voilation at Address 06B61CA6 in module 'project2.dll'. Write of address 00720061<br>'是怎么一回事?
 
这是我一个叫“开心就好”的网友写出来的<br>没有经过他的同意,我想我不该帖出来的<br>不过,可以看看<br>就是这么一种思路<br>需要的时候<br>可以在工程中加入并 USER 这个打印单元<br>并直接调用!<br><br>(***************************************************)<br>(* 单元名: UN_TDprint &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(* 功 &nbsp;能: 通用的打印单元 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TianYueZeng &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*)<br>(******************************************************************)<br>(* 应用:在要套打的单元里uses这个单元:UN_TDprint, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; Printer.BeginDoc; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; PrintText(16, 14, '开心', 'config.txt',9); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; PrintText(26, 14, '就好', 'config.txt',9); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; Printer.EndDoc; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*)<br>(* 观察结果,用尺子测量偏移量,在config.txt文件中修改X,Y的值即可 *)<br>(******************************************************************)<br>unit UN_TDprint;<br>interface<br>uses<br> &nbsp; &nbsp;Printers,Windows, Messages, SysUtils, Variants, Classes, Graphics, Dialogs, Controls,forms;<br> &nbsp; &nbsp; &nbsp;//取得字符的高度<br> &nbsp; &nbsp;function CharHeight: Word;<br> &nbsp; &nbsp; &nbsp;//取得字符的平均宽度<br> &nbsp; &nbsp;function AvgCharWidth: Word;<br> &nbsp; &nbsp; &nbsp;//取得纸张的物理尺寸---单位:点<br> &nbsp; &nbsp;function GetPhicalPaper: TPoint;<br> &nbsp; &nbsp; &nbsp;//取得纸张的逻辑宽度--可打印区域,取得纸张的逻辑尺寸<br> &nbsp; &nbsp;function PaperLogicSize: TPoint;<br> &nbsp; &nbsp; &nbsp;//纸张水平对垂直方向的纵横比例<br> &nbsp; &nbsp;function HVLogincRatio: Extended;<br> &nbsp; &nbsp; &nbsp;//取得纸张的横向偏移量-单位:点<br> &nbsp; &nbsp;function GetOffSetX: Integer;<br> &nbsp; &nbsp; &nbsp;//取得纸张的纵向偏移量-单位:点<br> &nbsp; &nbsp;function GetOffSetY: Integer;<br> &nbsp; &nbsp; &nbsp;//毫米单位转换为英寸单位<br> &nbsp; &nbsp;function MmToInch(Length: Extended): Extended;<br> &nbsp; &nbsp; &nbsp;//英寸单位转换为毫米单位<br> &nbsp; &nbsp;function InchToMm(Length: Extended): Extended;<br> &nbsp; &nbsp; &nbsp;//取得水平方向每英寸打印机的点数<br> &nbsp; &nbsp;function HPointsPerInch: Integer;<br> &nbsp; &nbsp; &nbsp;//取得纵向方向每英寸打印机的光栅数<br> &nbsp; &nbsp;function VPointsPerInch: Integer;<br> &nbsp; &nbsp; &nbsp;//横向点单位转换为毫米单位<br> &nbsp; &nbsp;function XPointToMm(Pos: Integer): Extended;<br> &nbsp; &nbsp; &nbsp;//纵向点单位转换为毫米单位<br> &nbsp; &nbsp;function YPointToMm(Pos: Integer): Extended;<br> &nbsp; &nbsp; &nbsp;//设置纸张高度-单位:mm<br> &nbsp; &nbsp;procedure SetPaperHeight(Value:integer);<br> &nbsp; &nbsp; &nbsp;//设置纸张宽度:单位--mm<br> &nbsp; &nbsp;Procedure SetPaperWidth(Value:integer);<br> &nbsp; &nbsp; &nbsp;//在 (Xmm, Ymm)处按指定配置文件信息和字体输出字符串<br> &nbsp; &nbsp;procedure PrintText(X, Y: Extended; Txt: string; ConfigFileName: string;FontSize: Integer=12);<br>implementation<br>//取得字符的高度<br>function CharHeight: Word;<br>var<br> &nbsp; &nbsp;Metrics: TTextMetric;<br>begin<br> &nbsp; &nbsp;GetTextMetrics(Printer.Canvas.Handle, Metrics);<br> &nbsp; &nbsp;Result := Metrics.tmHeight;<br>end;<br>//取得字符的平均宽度<br>function AvgCharWidth: Word;<br>var<br> &nbsp; &nbsp;Metrics: TTextMetric;<br>.......<br>end;<br>//在 (Xmm, Ymm)处按指定配置文件信息和字体输出字符串<br>procedure PrintText(X, Y: Extended; Txt: string; ConfigFileName: string;FontSize: Integer=12);<br>var<br>......
 
to kinneng:<br><br>在DLL中再返过来引用调用该DLL的Unit,循环引用啊.
 
这是全部源代码了,包括dpr,pas,dfm,除了数据库,我这里很正常,没有问题,这就难搞了。
 
你在DLL中又引用主程序的unit,这不行,我要他与主程序无关。
 
无关?无关的东西怎么传?
 
就是我想打印的时候,在主程序中只要调用打印函数传个窗体过去就可以了.而不必理会DLL,<br>你写的代码,在DLL中还得引用所要打印的窗体单元
 
只有窗体,有什么用?
 
1.DLL本身是自成体系的,有自己的一套,因此很多用到RTTI的东西就会出错,比如is等。<br>不过其实你可以用ClassName来判断。除非极个别,基本上用ClassName是可以做到的.<br>必要的地方再加上强制转换,基本上就没问题了.<br>比如<br>var ed : TEdit;<br>ed := TEdit(AppForm.Edit1)
 
不可以啊,classneme,显示并不是此控件的,建议楼上试试
 
后退
顶部