在运行时更改Delphi样式不允许将文件拖放到表单

Rau*_*aul 8 delphi vcl-styles

我有以下过程允许从Windows中删除文件,删除工作正常,但是当我在运行时使用(TStyleManager.TrySetStyle(styleName))更改样式时,表单接受不再丢弃!这到底有什么问题?

public //public section of the form
...
procedure AcceptFiles( var msg : TMessage ); message WM_DROPFILES;

...

procedure TMainFrm.AcceptFiles(var msg: TMessage);
 var
   i,
   fCount     : integer;
   aFileName : array [0..255] of char;
begin
   // find out how many files the form is accepting
   fCount := DragQueryFile( msg.WParam, {uses ShellApi is required...}
                            $FFFFFFFF,
                            acFileName,
                            255 );

  for I := 0 to fCount - 1 do
  begin
    DragQueryFile(msg.WParam, i, aFileName, 255);
    if UpperCase(ExtractFileExt(aFileName)) = '.MSG' then //accept only .msg files
    begin
       if not itemExists(aFileName, ListBox1) then// function checks whether the file was already added to the listbox
       begin
        ListBox1.Items.Add(aFileName);

       end
    end;
  end;
  DragFinish( msg.WParam );
end;
Run Code Online (Sandbox Code Playgroud)

...

procedure TMainFrm.FormCreate(Sender: TObject);
begin
  DragAcceptFiles( Handle, True ); //Main form accepts the dropped files 
end;
Run Code Online (Sandbox Code Playgroud)

小智 16

DragAcceptFiles(Handle, True);将窗体当前使用的窗口句柄报告为接受文件.对表单进行一些更改会导致窗口句柄被销毁并重新创建,并且更改样式就是其中之一.发生这种情况时,FormCreate不会再次调用.重新创建窗口句柄时,您还需要将新句柄报告为接受文件.你可以简单的移动代码在你FormCreateCreateWnd为:

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  protected
    procedure CreateWnd; override;
  public
    { Public declarations }
  end;

implementation

procedure TForm1.CreateWnd;
begin
  inherited;
  DragAcceptFiles(Handle, True);
end;
Run Code Online (Sandbox Code Playgroud)

  • 感谢编辑@TLama,同意这让它更加清晰. (2认同)