2013-02-27 8 views
8

Ho la seguente procedura che consente di eliminare file da Windows, il dropping funziona bene ma quando cambio lo stile in runtime usando (TStyleManager.TrySetStyle(styleName)), il modulo accetta non più cadere! cosa c'è di sbagliato qui esattamente?Cambiare gli stili di Delphi in fase di runtime non consente di trascinare i file nel modulo

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; 

risposta

16

DragAcceptFiles(Handle, True); riporta il attualmente utilizzato handle di finestra per il modulo come accettare i file. Alcune modifiche al modulo causano la distruzione e la ricreazione dell'handle della finestra e la modifica dello stile è una di queste. Quando ciò accade, FormCreate non viene richiamato. Quando l'handle della finestra viene ricreato, è necessario segnalare anche il nuovo handle come file di accettazione. Si può semplicemente spostare il codice nel tuo FormCreate-CreateWnd per questo:

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 per la risposta! funziona come un fascino – Raul

+2

Grazie per la modifica di @TLama, siamo d'accordo che ciò lo rende un po 'più chiaro. – hvd

Problemi correlati