2013-02-27 8 views
8

Windowsからファイルをドロップできるようにする手順はありますが、ドロッピングはうまく動作しますが、(TStyleManager.TrySetStyle(styleName))もはや落ちることはありません!ここで何が間違っていますか?実行時にDelphiスタイルを変更すると、ファイルをフォームにドロップできない

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; 

答えて

16

DragAcceptFiles(Handle, True);は現在がファイルを受け入れるなど、フォームのウィンドウハンドルを使用し報告します。フォームの一部を変更すると、ウィンドウハンドルが破棄されて再作成され、スタイルの変更もその1つです。この場合、FormCreateは再度呼び出されません。ウィンドウハンドルが再作成されると、新しいハンドルをファイル受け入れとして報告する必要もあります。あなたは、単にそのためCreateWndにごFormCreateにコードを移動することができます:あなたの答えのための

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; 
+0

1 @hvdのthaksを!魅力のように働く – Raul

+2

@TLamaを編集してくれてありがとう、それはそれがかなり少し明確になることに同意した。 – hvd

関連する問題