大家都别光顾着聊天,来帮帮忙!用DLL封装窗体(难道真的没有人遇到过?) (100分)

  • 主题发起人 bambooheart
  • 开始时间
B

bambooheart

Unregistered / Unconfirmed
GUEST, unregistred user!
将窗体封装到DLL中,在程序中动态调用DLL中的窗体,当释放DLL时,原先的程序窗体界面
会被隐藏!必须点击一下任务栏中程序图标将应用程序最小化,然后再点击任务栏中的程序
图标才可以重新显示窗体!

请问这个问题怎么解决?

 
你可以在Dll释放之后用TForm.Show 来作吗!!!
 
那我也试过了,不管用
 
dll释放后调用
SetForegroundWindow(主form.handle)
 
我的很正常,不知你的代码是怎么写的?
 
我贴一下,大家看看
 
这是调用单元
--------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TShowAbout = Function(AHandle: THandle):boolean;stdcall;
TForm1 = class(TForm)
Button6: TButton;
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation

{$R *.dfm}

procedure TForm1.Button6Click(Sender: TObject);
var
ShowAbout:TShowAbout;
Handles :THandle;
begin
Handles := loadlibrary('AboutInfo.dll');
if Handles<>0 then
begin
@ShowAbout := GetProcAddress(Handles,'ShowAbout');
if @ShowAbout<>nil then
begin
ShowAbout(application.Handle);
end;
FreeLibrary(Handles);
end;
end;

end.
 

library AboutInfo;
uses
SysUtils,
Classes,
forms,
unit_version_info in 'unit_version_info.pas' {frm_version};

{$R *.res}
exports
ShowAbout;

begin
end.
--------------------------------------
unit unit_version_info;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls;

type
Tfrm_version = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }

public
{ Public declarations }
end;

var
frm_version: Tfrm_version;
Function ShowAbout(AHandle: THandle):Boolean; stdcall;
implementation
{$R *.dfm}
Function ShowAbout(AHandle: THandle):Boolean;
var
AboutForm :Tfrm_version;
begin
Application.Handle := AHandle;
AboutForm := Tfrm_version.Create(application);
AboutForm.ShowModal;
AboutForm.Free;
end;

procedure Tfrm_version.Button1Click(Sender: TObject);
begin
Close;
end;

end.
 
调用单元:
unit uUseDLL;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TUseDLLForm = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
UseDLLForm: TUseDLLForm;
procedure OpenForm(mainForm:TForm);stdcall;External'MyDLL.dll';
procedure ShowCCC;stdcall;External'MyDLL.dll';
procedure InputCCC(Text: Pchar);stdcall;External'MyDLL.dll';
implementation

{$R *.dfm}

procedure TUseDLLForm.Button1Click(Sender: TObject);
var
Text: Pchar;
begin
Text := Pchar(Edit1.Text);
OpenForm(Application.MainForm);//为了调MDICHILD
InputCCC(Text);//为了实验DLL中的全局变量是否在各个应用程序间共享
end;

procedure TUseDLLForm.Button2Click(Sender: TObject);
begin
ShowCCC;
end;

end.

DLL单元:
library MyDLL;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes,Forms,
Windows, Messages, Variants, Graphics, Controls,
Dialogs, StdCtrls,
uTDLL in 'uTDLL.pas' {TDLLForm};

{$R *.res}
var
ccc: Pchar;
procedure OpenForm(mainForm:TForm);stdcall;
var
Form1: TTDLLForm;
ptr:pLongInt;
begin
ptr:=@(Application.MainForm);
ptr^:=LongInt(mainForm);
Form1:=TTDLLForm.Create(mainForm);
Form1.Show;
end;

procedure InputCCC(Text: Pchar);stdcall;
begin
ccc := Text;
end;

procedure ShowCCC;stdcall;
begin
ShowMessage(String(ccc));
end;
exports
OpenForm,
InputCCC,
ShowCCC;
begin
end.
 
静态调用时没有问题
 
可我不想用静态调用方式
 
那就不要过早地释放动态连接库,
在主窗口的 OnDestroy 中关闭动态连接库。
 
to:jsxjd
那我还用动态方式调用做什么?
 
做了那么多,没碰到你那种情况。
 
to:郭玉梁
那你把我上面贴的代码粘下来试试,或者留个Mail,我把这个问题DLL给你
 
在适当的地方使用SetForegroundWindow(主form.handle)难道也不可以吗??
 
to:windbell
不可以
 
我的email:
xzlzq@163.com
 
to:windbell
邮件已发,请查收
 
顶部