動態產生元件,使用TList新增與刪除元件問題 |
答題得分者是:ikk
|
maplefog
一般會員 ![]() ![]() 發表:16 回覆:24 積分:13 註冊:2008-11-08 發送簡訊給我 |
我使用popupmenu,一個選項是產生image,另一個選項是刪除所選的image。 每按一次新增一個image,然後使用TList add 不管產生多少image,顯示List的Count都是1 ??? 所以刪除時也只能刪除Items[0],其他的image不能刪除 請問我程式哪邊有錯誤 謝謝! 程式碼如下 Unit2.h [code cpp] class TForm2 : public TForm { __published: // IDE-managed Components TPopupMenu *PopupMenu1; TMenuItem *addDG; TMenuItem *delDG; void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y); void __fastcall addDGClick(TObject *Sender); void __fastcall delDGClick(TObject *Sender); void __fastcall FormCreate(TObject *Sender); void __fastcall MyMouseEnter(TObject *Sender); private: // User declarations TList *ListImg; TImage *img; int Mouse_X; int Mouse_Y; int NumDG; int LastTag; int SelDG; public: // User declarations __fastcall TForm2(TComponent* Owner); }; [/code] Unit2.cpp [code cpp] void __fastcall TForm2::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { Mouse_X = X; Mouse_Y = Y; } //--------------------------------------------------------------------------- void __fastcall TForm2::addDGClick(TObject *Sender) { ListImg = new TList; img = new TImage(this); img->Parent = Form2; img->Width = 24; img->Height = 24; img->Left = Mouse_X; img->Top = Mouse_Y; img->Tag = LastTag; img->OnMouseEnter = MyMouseEnter; //自訂MouseEnter事件 img->Picture->LoadFromFile("DG.ico"); ListImg->Add((TImage*)img); NumDG = 1; LastTag = 1; } //--------------------------------------------------------------------------- void __fastcall TForm2::delDGClick(TObject *Sender) { delete (TForm2*)(ListImg->Items[SelDG]); //只能刪除Items[0] } //--------------------------------------------------------------------------- void __fastcall TForm2::FormCreate(TObject *Sender) { NumDG = 0; LastTag = 0; } //--------------------------------------------------------------------------- void __fastcall TForm2::MyMouseEnter(TObject *Sender) { SelDG = ((TImage*)Sender)->Tag; ShowMessage(ListImg->Count); //每次都顯示1 } //--------------------------------------------------------------------------- [/code] 編輯記錄
maplefog 重新編輯於 2009-09-09 20:16:12, 註解 無‧
|
ikk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:4 回覆:413 積分:768 註冊:2003-06-30 發送簡訊給我 |
|
maplefog
一般會員 ![]() ![]() 發表:16 回覆:24 積分:13 註冊:2008-11-08 發送簡訊給我 |
感謝! 問題已經解決了 Unit2.h [code cpp] class TForm2 : public TForm { __published: // IDE-managed Components TPopupMenu *PopupMenu1; TMenuItem *addDG; TMenuItem *delDG; void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y); void __fastcall addDGClick(TObject *Sender); void __fastcall delDGClick(TObject *Sender); void __fastcall FormCreate(TObject *Sender); void __fastcall MyMouseEnter(TObject *Sender); void __fastcall PopupMenu1Popup(TObject *Sender); void __fastcall MyMouseLeave(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); void __fastcall FormDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept); void __fastcall FormDragDrop(TObject *Sender, TObject *Source, int X, int Y); private: // User declarations TList *ListImg; TImage *img; int Mouse_X,Mouse_Y; int NumDG; int LastTag; int SelDG; bool AddImg,DelImg; public: // User declarations __fastcall TForm2(TComponent* Owner); }; [/code] Unit2.cpp [code cpp] void __fastcall TForm2::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { Mouse_X = X; Mouse_Y = Y; } //--------------------------------------------------------------------------- void __fastcall TForm2::addDGClick(TObject *Sender) { img = new TImage(this); img->Parent = Form2; img->Width = 24; img->Height = 24; img->Left = Mouse_X; img->Top = Mouse_Y; img->Tag = LastTag; img->Transparent = true; img->DragMode = dmAutomatic; img->OnMouseEnter = MyMouseEnter; img->OnMouseLeave = MyMouseLeave; img->Picture->LoadFromFile("DG.ico"); ListImg->Add((TImage*)img); NumDG = 1; LastTag = 1; } //--------------------------------------------------------------------------- void __fastcall TForm2::delDGClick(TObject *Sender) { delete (TForm2*)(ListImg->Items[SelDG]); NumDG -= 1; } //--------------------------------------------------------------------------- void __fastcall TForm2::FormCreate(TObject *Sender) { ListImg = new TList; NumDG = 0; LastTag = 0; AddImg = true; DelImg = false; } //--------------------------------------------------------------------------- void __fastcall TForm2::MyMouseEnter(TObject *Sender) { AddImg = false; DelImg = true; SelDG = ((TImage*)Sender)->Tag; } //--------------------------------------------------------------------------- void __fastcall TForm2::PopupMenu1Popup(TObject *Sender) { if(AddImg==true) addDG->Enabled = true; else addDG->Enabled = false; if(DelImg==true) delDG->Enabled = true; else delDG->Enabled = false; } //--------------------------------------------------------------------------- void __fastcall TForm2::MyMouseLeave(TObject *Sender) { AddImg = true; DelImg = false; } //--------------------------------------------------------------------------- void __fastcall TForm2::FormDestroy(TObject *Sender) { delete ListImg; } //--------------------------------------------------------------------------- void __fastcall TForm2::FormDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept) { Accept = Source->ClassNameIs("TImage"); } //--------------------------------------------------------------------------- void __fastcall TForm2::FormDragDrop(TObject *Sender, TObject *Source, int X, int Y) { if(Source->ClassNameIs("TImage")) { int WIDTH = ((TLabel*)Source)->Width; int HEIGHT = ((TLabel*)Source)->Height; ((TLabel*)Source)->Left = X-WIDTH/2; ((TLabel*)Source)->Top = Y-HEIGHT/2; } } //--------------------------------------------------------------------------- [/code]
編輯記錄
maplefog 重新編輯於 2009-09-10 12:35:02, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |