DLL中的VCL样式问题

问题描述 投票:8回答:3

我已经开发了一种具有一种形式的DLL。我已使用以下代码为其设置了样式。

library TestLib;

uses Vcl.Themes, Vcl.Styles,....
.
.
exports
   function1,
   function2;

begin
   TStyleManager.TrySetStyle('Style1');
end.

当我加载此dll并调用function1时,它将打开此表单。表单打开并应用样式。

现在,当我最小化该窗口时,我遇到访问冲突。包括“最大化和还原”在内的所有功能都可以正常工作。此外,所有功能都可以正常工作。

我想它不处理此表单的Minimize事件生成的消息。请指教。

注意:删除样式时,一切正常。

Call Stack

:0976742b TWinControl.HandleNeeded + $3
:0978ad8a TStyleManager.HandleMessage + $56
:09762a3c TWinControl.DoHandleStyleMessage + $14
:0972e6be TCustomForm.WndProc + $612
:09763c2b TWinControl.MainWndProc + $2F

UPDATE:SSCCE

Project1.EXE(具有一种形式Unit1.pas / dfm)

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function InitDLL: Boolean;
  end;

var
  Form1: TForm1;

implementation

const
   cLIBRARY = 'Project2.dll';

var
   DLLHandle : THandle;
   showfrm: procedure;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if InitDLL then
      showfrm;
end;

function TForm1.InitDLL: Boolean;
begin
   if DLLHandle = 0 then
   begin
      DLLHandle := LoadLibrary(PChar(cLIBRARY));
      if DLLHandle <> 0 then
      begin
         @showfrm := GetProcAddress(DLLHandle, 'showfrm');
      end
      else
      begin
         Result := False;
         raise Exception.Create('Error loading DLL: ' + cLIBRARY);
      end;
   end;

   Result := (DLLHandle > 0);
end;

{$R *.dfm}

end.

创建一个DLL Project2.dll,其unit2为任何形式,而unit3将调用该形式。向此dll中添加一种样式(例如AnyStyle1)作为资源。

library Project2;

{ 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. }

{$R *.dres}

uses
  ShareMem,
  Vcl.Themes,
  Vcl.Styles,
  Vcl.Dialogs,
  System.SysUtils,
  System.Classes,
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas';

{$R *.res}

exports
showfrm;

begin
   if TStyleManager.TrySetStyle('AnyStyle1') then
   begin
      ShowMessage('True');
   end
   else
      ShowMessage('False');
end.

unit3.pas

unit Unit3;

interface

uses Unit2;

   procedure showfrm;

implementation
   procedure showfrm;
   begin
      with TForm2.Create(nil) do
         Show;
   end;
end.

现在按下Unit2窗口的最小化按钮。您将收到访问冲突。

delphi dll delphi-xe2 vcl-styles
3个回答
6
投票

访问冲突的原因是,Delphi XE2中的vcl样式似乎并不是在dll中考虑VCL样式设计的。将AV抛出到表单样式挂钩的WM_SIZE处理程序中:

procedure TFormStyleHook.WMSize(var Message: TWMSize);
begin
  if IsIconic(Handle) and (Application.MainForm.Handle <> Handle) then
    InvalidateNC;

  ...

样式挂钩测试是否在主窗体上处理了消息,但是dll中没有主窗体。访问未分配引用的句柄会导致异常。

下面的变通办法引入了后代样式挂钩来防止这种情况,它绕过了对主窗体的检查,并让消息的处理在TWinControl处继续。

这是dll中完整的,修改后的'unit3':

unit Unit3;

interface

uses forms, messages, themes, windows, Unit2;

procedure showfrm;

implementation

type
  TForm2StyleHook = class(TFormStyleHook)
  private
    procedure WMSize(var Message: TWMSIZE); message WM_SIZE;
  end;

procedure TForm2StyleHook.WMSize(var Message: TWMSIZE);
begin
  if IsIconic(Handle) then begin
    // duplicate the code in ascendant, for whatever it serves
    InvalidateNC;
    // the rest of the code in ascendant class is related with MDI

    Handled := False; // if this is set to true TWinControl.WndProc returns
  end else
    inherited;
end;

procedure showfrm;
begin
  TStyleManager.Engine.RegisterStyleHook(TForm2, TForm2StyleHook);

  with TForm2.Create(nil) do
    Show;
end;

end.

注意在考虑在dll中使用样式时,还请注意继续遇到此类问题的可能性。


0
投票

您应在启用了“运行时程序包”的同一版本的RAD Studio中编译bouth dll和exe。仅在这种情况下,才能保证dll中的表格正常工作。


0
投票

样式变得越来越流行,所以也许会对将来的读者有所帮助,特别是因为Delphi RIO 10.3.3仍然提供最小化的AV。

我发现在dll中创建一个虚拟的隐藏主形式会有所帮助。

只需将应用程序对象传递到dll并在内部执行此操作

var
  aMainForm: TForm;
begin
      Application.Handle := App.Handle;
      Application.OnIdle := App.OnIdle;
      Application.OnMessage := App.OnMessage;
      Application.CreateForm(TForm, aMainForm);
© www.soinside.com 2019 - 2024. All rights reserved.