2013-02-27 32 views
8

我有下面的過程允許從窗口中刪除文件,刪除工作得很好,但是當我在運行時使用(TStyleManager.TrySetStyle(styleName))更改樣式時,表單accept沒有更多的下降!這裏究竟有什麼不對?在運行時更改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);報告目前使用的窗口句柄形式接受文件。窗體的一些更改會導致窗口句柄被銷燬並重新創建,並且更改樣式就是其中之一。發生這種情況時,不會再調用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; 
+0

+1 @hvd thaks你的答案!像魅力一樣工作 – Raul 2013-02-27 12:36:01

+2

感謝編輯@TLama,同意這使得它更清晰。 – hvd 2013-02-27 19:12:24