在运行时更改 Delphi 样式不允许将文件拖放到窗体中

ejk8hzay  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(145)

我有下面的过程,允许从窗口拖放文件,拖放工作得很好,但当我在运行时使用(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;

...

procedure TMainFrm.FormCreate(Sender: TObject);
begin
  DragAcceptFiles( Handle, True ); //Main form accepts the dropped files 
end;
blmhpbnm

blmhpbnm1#

DragAcceptFiles(Handle, True);将窗体的“当前”使用的窗口句柄报告为接受文件。对窗体的某些更改会导致窗口句柄被破坏并重新创建,更改样式就是其中之一。当发生这种情况时,将不会再次调用FormCreate。当窗口句柄被重新创建时,您还需要报告新句柄正在接受文件。您可以简单地将FormCreate中的代码移到CreateWnd中:

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;
ldioqlga

ldioqlga2#

九年后,这里有一个提示,你发现自己阅读上述内容,并想知道它如何适用于Raize的DropMaster。
https://raize.com/forums/topic/tdmtexttarget-on-a-frame/表示添加以下行:

DMTextTarget1.AcceptorControl := nil;  // needed to disconnect the drag and drop 
DMTextTarget1.AcceptorControl := theOriginalAcceptorControl; // reconnect the drag and drop.

当我改变风格的时候,上面的工作就开始了。

相关问题