8

I have the following procedure that allows droping files from windows, the dropping works just fine but when I change the style at runtime using (TStyleManager.TrySetStyle(styleName)), the form accept no more dropping! what is wrong here exactly?

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;
Raul
  • 656
  • 5
  • 17

2 Answers2

16

DragAcceptFiles(Handle, True); reports the currently used window handle for the form as accepting files. Some changes to the form cause the window handle to be destroyed and recreated, and changing the style is one of them. When this happens, FormCreate is not called again. When the window handle gets recreated, you need to also report the new handle as accepting files. You can simply move the code in your FormCreate to CreateWnd for that:

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;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 2
    Thanks for editing @TLama, agreed that that makes it quite a bit clearer. –  Feb 27 '13 at 19:12
0

Nine years later, here's a hint of you find yourself reading the above and wondering how it applies to Raize's DropMaster.

https://raize.com/forums/topic/tdmtexttarget-on-a-frame/ says add these lines:

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

The above worked form me when I was changing styles.

RobertFrank
  • 7,332
  • 11
  • 53
  • 99