用delphi写的动态库,在delphi中调用结果正常,但在word(vba)中调用结果不正确,请大家来看看,是哪里写得不对!(100分)

  • 主题发起人 主题发起人 dirk
  • 开始时间 开始时间
D

dirk

Unregistered / Unconfirmed
GUEST, unregistred user!
帮别人忙,随便写个合成图片的小程序(代码在后面):<br>function LoadBJ(PicName: PChar): boolean; stdcall;<br>导入背景图<br>function LoadQJ(PicName: PChar;X,Y:integer): boolean; stdcall;<br>导入前景图,指定其显示在背景的位置<br>function LoadText(Text,FontName: PChar;X,Y:integer;FontColor:integer=0;FontSize:integer=9;<br>&nbsp; FontBold:boolean=false;FontItalic:boolean=false;FontUnderline:boolean=false): boolean; stdcall;<br>在背景上输出文字,指定位置和字体<br>function SavePic(NewPicName: PChar): boolean; stdcall;<br>保存合成图片<br><br>然后输出这几个函数<br>exports<br>&nbsp; &nbsp; LoadBJ,<br>&nbsp; &nbsp; LoadQJ,<br>&nbsp; &nbsp; LoadText,<br>&nbsp; &nbsp; SavePic;<br><br>在delphi中图片、文字完全可以合成,产生新的图片文件,但是在word中调用,只能把背景图保存出来,但前景图、文字却不能画到背景图上去,以下是word中的代码:<br><br>Option Explicit<br>Private Declare Function LoadBJ Lib "e:/SMG.dll" (ByVal PicName As String) As Boolean<br><br>Private Declare Function LoadQJ Lib "e:/SMG.dll" (ByVal PicName As String, X, Y As Integer) As Boolean<br><br>Private Declare Function LoadText Lib "e:/SMG.dll" (ByRef Text As String, ByVal FontName As String, X, Y As Integer, Optional FontColor As Integer = 0, Optional FontSize As Integer = 9, Optional FontBold As Boolean = False,Optional FontItalic As Boolean = False, Optional FontUnderline As Boolean = False) As Boolean<br><br>Private Declare Function SavePic Lib "e:/SMG.dll" (ByVal NewPicName As String) As Boolean<br><br>Private Sub CommandButton1_Click()<br>&nbsp; &nbsp; Call LoadBJ("e:/5.bmp")<br>&nbsp; &nbsp; Call LoadQJ("E:/6.bmp", 100, 100)<br>&nbsp; &nbsp; Call LoadText("嘿嘿嘿!123", "", 200, 200, 125, 18, True)<br>&nbsp; &nbsp; Call SavePic("e:/dass.bmp")<br><br><br>End Sub<br><br>在word中调用,.Canvas.Draw(X ,Y ,img_wz.Picture.Graphic );这句话根本就没有用,换成bitblt也不行,也不报错,但图像就是不输出到指定的场景上,我真是晕了,为什么会这样?后来我尝试了另一种方式,在dll中添加一个form,然后在form上放image,把form显示出来,一样的代码,在form上的一个button上执行,到是能把图像画到指定的场景上,这到底是为什么?<br><br>delphi做出来的标准动态库到底是不是真正的标准动态库?那为什么在其他的语言中调用会出现和delphi中调用运行的结果不同呢?<br><br>以下是delphi代码:<br><br>unit Lib;<br><br>interface<br><br>uses<br>&nbsp; &nbsp; SysUtils, Windows, Classes, ExtCtrls, Graphics, StdCtrls ;<br><br>function LoadBJ(PicName: PChar): boolean; stdcall;<br>function LoadQJ(PicName: PChar;X,Y:integer): boolean; stdcall;<br>function LoadText(Text,FontName: PChar;X,Y:integer;FontColor:integer=0;FontSize:integer=9;<br>&nbsp; FontBold:boolean=false;FontItalic:boolean=false;FontUnderline:boolean=false): boolean; stdcall;<br>function SavePic(NewPicName: PChar): boolean; stdcall;<br><br>implementation<br><br>var<br>&nbsp; &nbsp; img_bj: TImage;<br>&nbsp; &nbsp; img_wz: TImage;<br>&nbsp; &nbsp; img_qj: TImage;<br>&nbsp; &nbsp; Lbl: TLabel;<br><br>function LoadBJ(PicName: PChar): boolean; stdcall;<br>begin<br>&nbsp; &nbsp; img_bj.Free ;<br>&nbsp; &nbsp; img_bj:=TImage.Create(nil);<br>&nbsp; &nbsp; img_bj.AutoSize :=true;<br><br>&nbsp; &nbsp; img_bj.Picture.LoadFromFile(PicName);<br>&nbsp; &nbsp; Result :=true;<br>end;<br><br>function LoadQJ(PicName: PChar;X,Y:integer): boolean; stdcall;<br>begin<br>&nbsp; &nbsp; img_qj.Free ;<br>&nbsp; &nbsp; img_qj:= TImage.Create(nil);<br>&nbsp; &nbsp; img_qj.AutoSize :=true;<br><br>&nbsp; &nbsp; img_qj.Picture.LoadFromFile(PicName);<br>&nbsp; &nbsp; img_bj.Canvas.Draw(X ,Y ,img_qj.Picture.Graphic );<br><br>&nbsp; &nbsp; Result :=true;<br>end;<br><br>function LoadText(Text,FontName: PChar;X,Y:integer;FontColor:integer=0;FontSize:integer=9;<br>&nbsp; FontBold:boolean=false;FontItalic:boolean=false;FontUnderline:boolean=false): boolean; stdcall;<br>begin<br>&nbsp; &nbsp; Lbl.Font.Name :=FontName;<br>&nbsp; &nbsp; Lbl.Font.Size :=FontSize;<br>&nbsp; &nbsp; Lbl.Font.Color :=FontColor;<br>&nbsp; &nbsp; Lbl.Font.Style :=[];<br><br>&nbsp; &nbsp; if FontBold then Lbl.Font.Style :=Lbl.Font.Style +[fsBold];<br>&nbsp; &nbsp; if FontItalic then Lbl.Font.Style :=Lbl.Font.Style +[fsItalic];<br>&nbsp; &nbsp; if FontUnderline then Lbl.Font.Style :=Lbl.Font.Style +[fsUnderline];<br><br>&nbsp; &nbsp; Lbl.Caption :=Text;<br><br>&nbsp; &nbsp; img_wz.Width :=lbl.Width ;<br>&nbsp; &nbsp; img_wz.Height :=lbl.Height ;<br><br>&nbsp; &nbsp; img_wz.Canvas.Font.Name :=FontName;<br>&nbsp; &nbsp; img_wz.Canvas.Font.Size :=FontSize;<br>&nbsp; &nbsp; img_wz.Canvas.Font.Color :=FontColor;<br>&nbsp; &nbsp; img_wz.Canvas.Font.Style :=[];<br><br>&nbsp; &nbsp; if FontBold then img_wz.Canvas.Font.Style :=img_wz.Canvas.Font.Style +[fsBold];<br>&nbsp; &nbsp; if FontItalic then img_wz.Canvas.Font.Style :=img_wz.Canvas.Font.Style +[fsItalic];<br>&nbsp; &nbsp; if FontUnderline then img_wz.Canvas.Font.Style :=img_wz.Canvas.Font.Style +[fsUnderline];<br><br>&nbsp; &nbsp; img_wz.Canvas.TextOut(0,0,Text);<br><br>&nbsp; &nbsp; img_bj.Canvas.Draw(X ,Y ,img_wz.Picture.Graphic );<br><br>&nbsp; &nbsp; Result :=true;<br>end;<br><br>function SavePic(NewPicName: PChar): boolean; stdcall;<br>begin<br>&nbsp; &nbsp; img_bj.Picture.SaveToFile(NewPicName);<br>&nbsp; &nbsp; Result :=true;<br>end;<br><br>initialization<br>&nbsp; &nbsp; img_bj:=TImage.Create(nil);<br>&nbsp; &nbsp; img_bj.AutoSize :=true;<br><br>&nbsp; &nbsp; img_qj:= TImage.Create(nil);<br>&nbsp; &nbsp; img_qj.AutoSize :=true;<br><br>&nbsp; &nbsp; img_wz:=TImage.Create(nil);<br>&nbsp; &nbsp; img_wz.AutoSize :=false;<br>&nbsp; &nbsp; img_wz.Transparent :=true;<br><br>&nbsp; &nbsp; Lbl:= TLabel.Create(nil);<br>&nbsp; &nbsp; Lbl.AutoSize :=true;<br>&nbsp; &nbsp; Lbl.Font.Name :='宋体';<br>&nbsp; &nbsp; Lbl.Font.Size :=9;<br>&nbsp; &nbsp; Lbl.Font.Color :=0;<br>&nbsp; &nbsp; Lbl.Font.Style :=[];<br><br>finalization<br>&nbsp; &nbsp; img_bj.Free ;<br>&nbsp; &nbsp; img_qj.Free ;<br>&nbsp; &nbsp; img_wz.Free ;<br>&nbsp; &nbsp; Lbl.Free ;<br><br>end.<br><br>
 
没遇见过!帮你顶下先!
 
我想,这些画图操作,必须有实际的Canvas才能生效,不然会有问题,也就是说,需要显式的执行。<br><br>
 
Canvas实际上就是封装的windows系统的DC,所以要先正确地创建。
 
用TBitmap取代TImage即可!
 
Canvas是肯定创建了的,TBitmap也试过了,不行。<br><br>请看清楚我问题的关键:用delphi写的动态库,在delphi中调用结果正常,但在word(vba)中调用结果不正确,代码有哪里写得不正确?
 
也有点疑问.<br><br>学习一下!
 
最终源代码贴出来!
 
上面贴的就是啊,编译成一个dll,在delphi中调用,没问题,合成的图像可以生成,然后照上面的word中的代码写到word中,就不行。
 
用TBitmap取代TImage即可! &nbsp;(你到底试过没有?)
 
为了避免参数传递的问题,请先试试没有参数的函数调用。就是在dll中导出<br>没有参数的函数,在VBA中调用看看。
 
也有点疑问.<br><br>学习一下! &nbsp;
 
xusong168,你试过TBitmap了吗?这里帖出来的代码是最后试验的代码,之前,TBitmap就试过了。
 
TBitmap必然可以,因为它是在内存中的Canvas上绘图<br>TImage不行,因为它需要一个Windows DC,也就是说,它必须放在一个Form上才有效,<br>在dll中的Form没正常显示出来的情况下,就不行了。
 
你是用什么方式的?stdcall?<br><br>word里好象是ole啊。要用和word兼容的参数传递方式!
 
???上次的答复没了,dfw丢数据了。<br><br>&gt;xusong168:TImage不行,因为它需要一个Windows DC,也就是说,它必须放在一个Form上才有效,在dll中的Form没正常显示出来的情况下,就不行了。<br><br>这个说法不对,我在delphi中调用上面的代码,绝对没有问题,但在Word中就不行,所以,再说一边,我的问题是:<br><br>[red]用delphi写的动态库,在delphi中调用结果正常,但在word(vba)中调用结果不正确,代码有哪里写得不正确?<br><br>delphi做出来的标准动态库到底是不是真正的标准动态库?那为什么在其他的语言中调用会出现和delphi中调用运行的结果不同呢?[/red]<br><br>不是说不能在dll中的TImage中画图。<br><br>firstrose,是stdcall调用,<br><br>&gt;word里好象是ole啊。要用和word兼容的参数传递方式!<br><br>我是在Word中的VBA代码中调用,和VB是一样的,<br><br>Private Declare Function LoadText Lib "e:/SMG.dll" (ByRef Text As String, ByVal FontName As String, X, Y As Integer, Optional FontColor As Integer = 0, Optional FontSize As Integer = 9, Optional FontBold As Boolean = False,Optional FontItalic As Boolean = False, Optional FontUnderline As Boolean = False) As Boolean<br><br>是标准的VB申明动态库的语句,和VB中调用系统API的语句是一样的,为了和Windows标准兼容,字符串用的也是pchar。
 
向楼主学习!
 
为了避免参数传递的问题,请先试试没有参数的函数调用。就是在dll中导出<br>没有参数的函数,在VBA中调用看看。 &nbsp;<br>TBitmap必然可以,因为它是在内存中的Canvas上绘图<br>TImage不行,因为它需要一个Windows DC,也就是说,它必须放在一个Form上才有效,<br>在dll中的Form没正常显示出来的情况下,就不行了。 &nbsp;<br>你到底试过没有?
 
delphi做出来的标准动态库到底是不是真正的标准动态库?那为什么在其他的语言中调用会出现和delphi中调用运行的结果不同呢?<br><br>是真正的标准动态库!不过要传递参数就有问题,对于pchar这种参数,必须要在<br>delphi dll project中加入shmem这个单元的引用!
 
后退
顶部