全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1738
推到 Plurk!
推到 Facebook!

請教有關自訂事件內Destroy自己問題

答題得分者是:william
chinliang
一般會員


發表:16
回覆:26
積分:13
註冊:2002-06-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-08-01 13:53:40 IP:61.218.xxx.xxx 未訂閱
各位先進好:    小弟在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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-08-01 14:03:16 IP:147.8.xxx.xxx 未訂閱
Don't call Destroy directly....    
procedure TEventOBJ.OnClick(Sender: TObject);
begin
  Sender.Free;
end;
chinliang
一般會員


發表:16
回覆:26
積分:13
註冊:2002-06-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-08-01 14:13:29 IP:61.218.xxx.xxx 未訂閱
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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-08-01 15:26:51 IP:61.155.xxx.xxx 未訂閱
改了一下: TRY~ > > > >
chinliang
一般會員


發表:16
回覆:26
積分:13
註冊:2002-06-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-08-01 16:02:22 IP:61.218.xxx.xxx 未訂閱
sos_admin先進您好: 我CopyPaste你的程式碼執行,一樣會出現Access Violation問題,我是使用Delphi 5,您在您的系統不會出現錯誤嗎? 若無法在物件本身的事件Destroy或是Free自己,有沒有其他的解決方法?我目前只能Click後,帶出PopupMenu,再選刪除,這樣的操作方式有點麻煩。 請各位先進不吝指教,謝謝。
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-08-01 16:37:25 IP:147.8.xxx.xxx 未訂閱
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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-08-01 17:20:45 IP:61.155.xxx.xxx 未訂閱
//任意创建多个image,选中其中任意一个都可以将其free > > 發表人 -
chinliang
一般會員


發表:16
回覆:26
積分:13
註冊:2002-06-17

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-08-01 18:44:32 IP:61.218.xxx.xxx 未訂閱
感謝William及sos_admin先進回應,目前照William所示已可正確執行。    有一個問題請教sos_admin先進,您說:"william 兄和我想的一样,不过william兄的方法有点缺点哦" 我看您的程式作法大致與William兄一樣,使用postmessage來作,不過您是多加了判斷,因為William使用integer(Sender)所產生的值來判斷,若integer(Sender)不重複的話,應無任何問題才是,另請教integer(Sender)所得出的是位址嗎?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-08-01 19:51:07 IP:210.3.xxx.xxx 未訂閱
引言: 我看您的程式作法大致與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

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-08-01 20:53:56 IP:218.2.xxx.xxx 未訂閱
和朋友喝酒去了,刚从外面回来~~~< > >>]. 我上面的程式和>重複]的情况.< >
chinliang
一般會員


發表:16
回覆:26
積分:13
註冊:2002-06-17

發送簡訊給我
#11 引用回覆 回覆 發表時間:2003-08-02 09:16:16 IP:61.218.xxx.xxx 未訂閱
感謝william & sos_admin先進的指教,讓小弟收穫良多,又到了我最傷腦筋的時候,給分真是太痛苦了,因為William比較符合我實際需求(物件名稱不是陣列形式,為不固定),不過也感謝sos_admin的指教,謝謝。    另外sos_admin先進個人資料不是只有17歲嗎?還跑出去喝酒,難道是程式寫不出來找靈感嗎?< >< >還是不要喝太多了啦< > 祝二位先進有個愉快的週末,小弟上午還要上班< >。
系統時間:2024-05-15 16:17:24
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!