我正在使用David Heffernan在這里分享的一個軟體:
我如何在不處理Windows訊息的情況下讓表單接受檔案投放?
如何讓表單在不處理 Windows 訊息的情況下接受檔案丟棄? 我已將該組件從表單改為面板,并且在 MainForm 中運行良好。
然而,當在 我做了一個小演示來展示這個問題。
Unit1: Unit2: 我在這里用 看起來有兩個表單。 PS1:作業正常。
PS1:在 MainForm 中作業正常。
PS2: 在 PS3: 將 PS4. 我使用的是Delphi XE4.ShowModal()環境中使用時,我無法訪問Modal Form中的組件的值。
unit Unit1;
介面
使用
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics。
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Unit2;
型別
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Private
protected
公共
End;
var
Form1: TForm1;
實作; 實作
{$R *.dfm}
程式 TForm1.Button1Click(Sender: TObject);
Var aForm: TForm2;
Begin
aForm := TForm2.Create(Nil)。
嘗試
aForm.ShowModal;
最終End;
End;
End.
unit Unit2。
介面
用途
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics。
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, DragAndDrop;
型別
TmyPanel = class(TPanel, IDragDrop)
private[/span
FDropTarget: TDropTarget;
//實作IDragDrop。
function DropAllowed(const FileNames。array of string): Boolean;
procedure Drop(const FileNames: array of string);
protected
// procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;
procedure CreateWindowHandle(Const Params: TCreateParams); override。
procedure DestroyWindowHandle; override;
end;
TForm2 = class(TForm)
DropRefPanel。TPanel。
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
DropPanel: TmyPanel;
end。
var
Form2: TForm2;
實作; 實作
{$R *.dfm}
procedure TmyPanel. CreateWindowHandle(Const Params: TCreateParams);
begin
inherited;
FDropTarget := TDropTarget.Create(WindowHandle, Self);
end;
程式 TmyPanel.DestroyWindowHandle;
begin。
FreeAndNil(FDropTarget)。
inherited;
end;
function TmyPanel.DropAllowed(const FileNames: array of string): Boolean;
begin>
結果 :=真。
end;
procedure TmyPanel.Drop(const FileNames: array of string);
Var i: 整數。
begin
// Form2.Label1.Caption := 'Drop';
begin
for i := 0 to Length(FileNames)-1 do
ShowMessage(Form2.Label1.Caption ': ' FileNames[i])。
end;
end;
procedure TForm2.Button1Click(Sender: TObject); /span>
begin
showMessage(Label1.Caption)。
Label1.Caption := 'Button';
end。
procedure TForm2.FormActivate(Sender: TObject);
begin
Label1.Caption := 'Activation';
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
Label1.Caption := 'FormCreate';
DropRefPanel.Color :=$00BBF7B3 -50;
DropPanel := TmyPanel.Create(Form2);
with DropPanel do
begin[/span
Parent := DropRefPanel;
Left := 3;
Top := 3;
Width := DropRefPanel.Width - 6;
Height := DropRefPanel.Height - 6;
Visible := True;
ParentBackground := False;
顏色 := $00BBF7B3;
BevelOuter := bvLowered;
Caption := 'DropBox';
end。
end。
procedure TForm2. FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeAndNil(DropPanel)。
end。
end.
DragAndDrop單元的代碼與David所做的一樣,沒有改變。
Label1演示了這個問題。
當我在Drop()程序中詢問Form2.Label1.Caption時,它顯示了設計時的Label1值,或在FormCreate()中分配的值,但沒有顯示FormActivate()中的值或在程式后來分配的值。
TmyPanel.Create(Form2)中把Form2改成nil并沒有改變行為。
Create()函式移到OnActivate并沒有改變行為。
有沒有人知道這個ShowModal()例子中的問題?
uj5u.com熱心網友回復:
當顯示模態表單時,你正在創建一個新的Form2實體,但沒有將其分配給Form2全域變數,而Drop()正試圖訪問它。默認情況下,該全域變數只用于自動創建的(即設計時)TForm2物件,而不是動態創建的物件。
您試圖將創建的TForm2物件指定為TmyPanel物件的Owner,但是您正在使用全域Form2變數(它并不指向創建的TForm2物件! ),而你應該使用FormCreate()的隱式Self引數,例如:
DropPanel := TmyPanel.Create({Form2}Self) 。
現在,Drop()可以通過面板的Owner屬性訪問表單,例如:
procedure TmyPanel. Drop(const FileNames: array of string);
var
i: 整數。
myFrm: TForm2;
begin
myFrm :=Owner as TForm2;
myFrm.Label1.Caption := 'Drop';
begin
for i := 0 to Length(FileNames)-1 do
ShowMessage(myFrm.Label1.Caption ': ' FileNames[i])。
end。
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/316474.html
標籤:
