請教有關自訂事件內Destroy自己問題 |
答題得分者是:william
|
chinliang
一般會員 發表:16 回覆:26 積分:13 註冊:2002-06-17 發送簡訊給我 |
各位先進好: 小弟在Form Create時,動態產生一個TImage物件,而後將自訂的OnClick事件指定給該TImage物件,OnClick事件內為Destroy自己,每當執行Destroy後,即會出現Access Violation錯誤,不過TImage有刪除了,請教各位先進那裡需要修正,煩請指點,謝謝。 程式碼如下:
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, jpeg, ExtCtrls, ArkImage, StdCtrls; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; TEventOBJ = Object procedure onclick(sender: TObject); end; var Form1: TForm1; MyEvent: TEventOBJ; implementation {$R *.DFM} procedure TEventOBJ.OnClick(Sender: TObject); begin (Sender as TImage).Destroy; end; procedure TForm1.FormCreate(Sender: TObject); var Image: TImage; begin Image := Timage.Create(Self); Image.Parent := Form1; Image.OnClick := MyEvent.OnClick; Image.Left := 200; Image.Top := 200; Image.Picture.LoadFromFile('D:\myphoto.bmp'); Image.Visible:=true; end; end. |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
chinliang
一般會員 發表:16 回覆:26 積分:13 註冊:2002-06-17 發送簡訊給我 |
william先進您好: 還是會有錯誤,我找了Free的Help,內容如下,不知有沒有其他可行的方法? Destroys an object and frees its associated memory, if necessary. procedure Free; Description
Use Free to destroy an object. Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an Owner should be destroyed by a call to Free, so that can be properly destroyed and the memory released. Unlike Destroy, Free is successful even if the object is nil, so if the object was never initialized, Free wont result in an error. When you call Free for a component, it calls Free for all components that it owns, that is, all components in its component list. A form owns all the controls and non-visual components that are created on it in design mode. When it is freed, all of these components are automatically freed as well. Since, by default, all forms are owned by the Application object, when the application terminates, it frees the Application object, which frees all forms. For all objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with the object; otherwise the allocated memory will not be usable until after the application terminates. Warning: Never explicitly free a component within one of its own event handlers or free a component from the event handler of a component it owns or contains. For example, dont free a button in its OnClick event handler or free the form that owns the button from the button's OnClick event. To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.
|
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
|
chinliang
一般會員 發表:16 回覆:26 積分:13 註冊:2002-06-17 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Post a message and let the mainform kill it:
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, jpeg, ExtCtrls, ArkImage, StdCtrls; const WM_KILL = WM_USER 1; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } procedure Kill(var Msg: TMessage); message WM_KILL; public { Public declarations } end; TEventOBJ = Object procedure onclick(sender: TObject); end; var Form1: TForm1; MyEvent: TEventOBJ; implementation {$R *.DFM} procedure TEventOBJ.OnClick(Sender: TObject); begin PostMessage(Form1.Handle,WM_KILL,0,integer(Sender)); end; procedure TForm1.FormCreate(Sender: TObject); var Image: TImage; begin Image := Timage.Create(Self); Image.Parent := Form1; Image.OnClick := MyEvent.OnClick; Image.Left := 200; Image.Top := 200; Image.Picture.LoadFromFile('D:\myphoto.bmp'); Image.Visible:=true; end; end. procedure TForm1.Kill(var Msg: TMessage); var AObj: TObject; begin AObj := TObject(pointer(Msg.lParam)); if Assigned(AObj) then AObj.Free; Msg.Result := 0; end; |
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
|
chinliang
一般會員 發表:16 回覆:26 積分:13 註冊:2002-06-17 發送簡訊給我 |
感謝William及sos_admin先進回應,目前照William所示已可正確執行。 有一個問題請教sos_admin先進,您說:"william 兄和我想的一样,不过william兄的方法有点缺点哦" 我看您的程式作法大致與William兄一樣,使用postmessage來作,不過您是多加了判斷,因為William使用integer(Sender)所產生的值來判斷,若integer(Sender)不重複的話,應無任何問題才是,另請教integer(Sender)所得出的是位址嗎?
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: 我看您的程式作法大致與William兄一樣,使用postmessage來作,不過您是多加了判斷,因為William使用integer(Sender)所產生的值來判斷,若integer(Sender)不重複的話,應無任何問題才是,另請教integer(Sender)所得出的是位址嗎?integer(Sender) 是將物件 Sender 所在的記憶体位置 (32bit OS, i.e. 4 bytes) 轉換成 integer (32bit Delphi, = longint, i.e. 4 bytes)。所以這樣做只能在 32bit Delphi 用,在 16bit Delphi 1 下是行不通的 |
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
|
chinliang
一般會員 發表:16 回覆:26 積分:13 註冊:2002-06-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |