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

若二個元件內裡所編寫程式相近,該如何避免程式重複敘述?

尚未結案
YingChyuan
一般會員


發表:3
回覆:5
積分:1
註冊:2004-08-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-03 17:48:41 IP:61.59.xxx.xxx 未訂閱
是這樣的:    我在程式中分別使用Button1與ToolButton2二個按鍵。 但是開頭的第一段是相同的,只有在第二段因為細部功能有些微差異。    舉例: void __fastcall TForm1::ButtonClick1(TObject *Sender) { 第一段 第二段 } void __fastcall TForm1::ToolButton2Click(TObject *Sender) { 第一段 第二段 } 因為第一段較多,在瀏覽以及編輯起來尚且不便又麻煩。 所以請教各位高手,是否可以直接在Button2引用或是呼叫前面Button1的第一段? 有什麼函數或方法可以達成此目的呢?說明差了請見諒,謝謝!
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-03 18:04:04 IP:61.63.xxx.xxx 未訂閱
引言: 是這樣的: 我在程式中分別使用Button1與ToolButton2二個按鍵。 但是開頭的第一段是相同的,只有在第二段因為細部功能有些微差異。 舉例: void __fastcall TForm1::ButtonClick1(TObject *Sender) { 第一段 第二段 } void __fastcall TForm1::ToolButton2Click(TObject *Sender) { 第一段 第二段 } 因為第一段較多,在瀏覽以及編輯起來尚且不便又麻煩。 所以請教各位高手,是否可以直接在Button2引用或是呼叫前面Button1的第一段? 有什麼函數或方法可以達成此目的呢?說明差了請見諒,謝謝!
YingChyuan你好: 應該是 class="code"> Button1->MyButtonClick; Button2->MyButtonClick; 並於.cpp中撰寫這個事件的內容, such as : void __fastcall TForm1::MyButtonClick(TObject *Sender) { TButton* now_button=(TButton*)Sender; 第一段 第二段 if (now_button->Name=="Button1") // whatever you want if ((now_button->Name=="Button2") // whatever you want } 不過照你所言應該是直接把第一段寫成一個function也可的! All,參著! 發表人 -
JPTseng
一般會員


發表:14
回覆:22
積分:7
註冊:2004-10-27

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-03 21:24:27 IP:140.124.xxx.xxx 未訂閱
看到順便問一下~~~~ 可以使用Action嗎 ? 因為不太確定~~~所以想問一下 感覺好像可以~~
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-03 22:58:08 IP:211.76.xxx.xxx 未訂閱
YingChyuan 你好:    如果不用到Sender這個參數,可以這麼用:     
void TForm1::CommonPart(void) //別忘了在檔頭要宣告
{
//第一段
}
void __fastcall TForm1::ButtonClick1(TObject *Sender)
{
CommonPart();
//不同的部分
}    void __fastcall TForm1::ToolButton2Click(TObject *Sender)
{
CommonPart();
//不同的部分
}     
發表人 - pwipwi 於 2004/11/03 23:04:02
YingChyuan
一般會員


發表:3
回覆:5
積分:1
註冊:2004-08-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-04 00:02:02 IP:61.59.xxx.xxx 未訂閱
謝謝m8815010的回覆。 按照您的方式下去做,的確可行。但是卻衍生了另一個問題……… 由於我是修改美化關於影像處理的程式,其功能是按下按鈕後可以放大或固定倍率之>> >>>>>>>>>>>>>>>>>>>>
YingChyuan
一般會員


發表:3
回覆:5
積分:1
註冊:2004-08-31

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-04 00:11:18 IP:61.59.xxx.xxx 未訂閱
謝謝pwipwi的回答 <>< face="Verdana, Arial, Helvetica">引言: [code] void TForm1::CommonPart(void) //別忘了在檔頭要宣告 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 抱歉,請問應該加在哪裡?檔頭的前面有 __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } 我有試著加上您所敘述的那一行,但似乎會產生錯誤?此外需要在.h做修改嗎?實在是很不好意思... 發表人 - pwipwi 於 2004/11/03 23:04:02
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-11-04 00:16:07 IP:211.76.xxx.xxx 未訂閱
YingChyuan你好,    請在.H檔中:  
private:        // User declarations
    void CommonPart(void); //加在這裡
public:                // User declarations
    __fastcall TForm1(TComponent* Owner);
 
在cpp檔裡則是 void TForm1::CommonPart(void) //別忘了在檔頭要宣告 { //寫入共同部分碼 }
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-11-04 00:29:02 IP:211.74.xxx.xxx 未訂閱
引言: 謝謝m8815010的回覆。 按照您的方式下去做,的確可行。但是卻衍生了另一個問題……… 由於我是修改美化關於影像處理的程式,其功能是按下按鈕後可以放大或固定倍率之>> >>>>>>>>>>>>>>>>>>>>>< face="Verdana, Arial, Helvetica"> YingChyuan你好: 感覺你的 >,所以我舉個簡單的例子: < class="code"> In Unit1.h ~~ class TForm1 : public TForm { __published: // IDE-managed Components TImage *Image1; TButton *Button1; TButton *Button2; void __fastcall FormCreate(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); void __fastcall MyButtonClick(TObject* Sender); <--our personal new event }; ~~ In Unit1.cpp ~~ TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { Button1->MyButtonClick; //event assign Button2->MyButtonClick; } //--------------------------------------------------------------------------- void __fastcall TForm1::MyButtonClick(TObject* Sender) { Image1->Picture->LoadFromFile("C:\\background.bmp"); //共同會做的事 TButton* now_btn=(TButton*)Sender; if (now_btn->Name=="Button1") //個別要做的事 Image1->Stretch=true; if (now_btn->Name=="Button2") Image1->Stretch=false; } 這個例就是說button1和button2按下後首先同樣都會載入image1一張圖,不同的後來做的事,button1會設Image1->Stretch=true;而button2不會! 嗯,這是依你一開始的問題的解,但如果你有特殊的變化就要再說了!
YingChyuan
一般會員


發表:3
回覆:5
積分:1
註冊:2004-08-31

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-11-04 12:27:32 IP:61.59.xxx.xxx 未訂閱
謝謝pwipwi的回覆 >------------------------------- >>> ↑ >>>>>>>>>>>↓ >>//------------------------------------------箭頭所指二處整個為> >> <> >>>>> >(不包括紅色部分),而><>> } >>> <> >>>>> >
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-11-04 14:10:32 IP:211.76.xxx.xxx 未訂閱
看了你的程式碼後,我覺得用m8815010大大的方法是比較適合你的。    你上上一篇的程式碼中:  
 
//-------------------------------------------------------------------
void __fastcall TForm1::ToolButton2Click(TObject *Sender)
{
ToolButton1->Button1Click; //這行可以刪除
Button1Click(ToolButton1); //加入這一行即可~
}
//---------------------------------------------------------------------
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-11-04 15:00:33 IP:61.63.xxx.xxx 未訂閱
引言: 謝謝pwipwi的回覆 >------------------------------- >>> ↑ >>>>>>>>>>>↓ >>//------------------------------------------箭頭所指二處整個為> >> <> >>>>> >(不包括紅色部分),而><>> } >>> <> >>>>> > < face="Verdana, Arial, Helvetica"> YingChyuan你好: 你的問題是因為把部份>先取消之前>): < class="code"> In Unit1.h ~~ class TForm1 : public TForm { __published: // IDE-managed Components TImage *Image1; TButton *Button1; TButton *Button2; void __fastcall FormCreate(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); void __fastcall MyButtonClick(TObject* Sender); <--our personal new event }; ~~ In Unit1.cpp ~~ TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { Button1->OnClick = MyButtonClick; //event assign ToolButton2->OnClick = MyButtonClick; } //--------------------------------------------------------------------------- //這個事件是自已全部撰寫的,不是用inspect object快捷點出來的 void __fastcall TForm1::MyButtonClick(TObject* Sender) { Graphics::TBitmap *pSourceBitmap = Image1->Picture->Bitmap; ↑ Graphics::TBitmap *pDestBitmap = new Graphics::TBitmap(); pDestBitmap->Width = Edit1->Text.ToInt(); pDestBitmap->Height = Edit2->Text.ToInt(); pDestBitmap->PixelFormat = pf24bit; Image1->Width = pDestBitmap->Width; Image1->Height = pDestBitmap->Height; Pic->Begin(); ImageProcess(pSourceBitmap, pDestBitmap);//↓ Pic->End();//------------------------------------------箭頭所指二處整個為A段 pSourceBitmap->Assign(pDestBitmap); if (Sender->ClassName()=="ToolButton") { //判斷是不是ToolButton2按的,方法不唯一 Edit1->Text = IntToStr(pSourceBitmap->Width*125/100); Edit2->Text = IntToStr(pSourceBitmap->Height*125/100); } delete pDestBitmap; delete Pic; } All! 發表人 - m8815010 於 2004/11/04 15:06:26
YingChyuan
一般會員


發表:3
回覆:5
積分:1
註冊:2004-08-31

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-11-04 21:05:23 IP:61.59.xxx.xxx 未訂閱
再次謝謝m8815010&pwipwi二位熱心的回覆 首先我根據>< face="Verdana, Arial, Helvetica">引言:
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Button1->OnClick     = MyButtonClick;    //event assign
 ToolButton2->OnClick = MyButtonClick;
}
//---------------------------------------------------------------------------    //這個事件是自已全部撰寫的,不是用inspect object快捷點出來的
void __fastcall TForm1::MyButtonClick(TObject* Sender)
{
 Pic = new TPerformanceCounter();---補上
 Graphics::TBitmap *pSourceBitmap = Image1->Picture->Bitmap;
 Graphics::TBitmap *pDestBitmap = new Graphics::TBitmap();
 pDestBitmap->Width = Edit1->Text.ToInt();
 pDestBitmap->Height = Edit2->Text.ToInt();
 pDestBitmap->PixelFormat = pf24bit;     Image1->Width = pDestBitmap->Width;
 Image1->Height = pDestBitmap->Height;     Pic->Begin();
 ImageProcess(pSourceBitmap, pDestBitmap);// 
 Pic->End();     pSourceBitmap->Assign(pDestBitmap);     if (Sender->ClassName()=="ToolButton2") {       //判斷是不是ToolButton2按的,方法不唯一
     Edit1->Text = IntToStr(pSourceBitmap->Width*125/100);
     Edit2->Text = IntToStr(pSourceBitmap->Height*125/100);
 }     delete pDestBitmap;
 delete Pic;
}
All! 發表人 - m8815010 於 2004/11/04 15:06:26 執行後剩下一個錯誤在if (Sender->ClassName()=="ToolButton2") 訊息是:'operator==' not implemented in type 'ShortString' for arguments of type 'char *' 訊息說User沒有設定一個ShortString的參數 "char*"? 我不瞭解所他代表的意義,該怎麼更正?
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-11-04 22:29:40 IP:210.244.xxx.xxx 未訂閱
引言: 執行後剩下一個錯誤在if (Sender->ClassName()=="ToolButton2") 訊息是:'operator==' not implemented in type 'ShortString' for arguments of type 'char *' 訊息說User沒有設定一個ShortString的參數 "char*"? 我不瞭解所他代表的意義,該怎麼更正?
YingChyuan你好: 嗯,歹勢歹勢,記得我貼上的是 >! 那一行修正成: < class="code"> if (AnsiString(Sender->ClassName())=="TToolButton") 嗯,就這樣,注意是 TToolButton (<--class name) , 不是 TToolButton2 (<-- variable name) 哦!
YingChyuan
一般會員


發表:3
回覆:5
積分:1
註冊:2004-08-31

發送簡訊給我
#14 引用回覆 回覆 發表時間:2004-11-04 23:31:36 IP:61.59.xxx.xxx 未訂閱
謝謝m8815010!!! 在這裡先謝謝您的賜教,看了您所提供修改的方式,我想即將快成功了! 但發現我在前幾篇所曾提出的問題,就是: <>假設載入的> 在第一次按下>但> 等到按第二下時,>> 目的是做 >>>>>>>>>>>
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#15 引用回覆 回覆 發表時間:2004-11-05 12:53:53 IP:61.63.xxx.xxx 未訂閱
YingChyuan你好: 我大至聽得懂你的問題,但是不好意思哦,因為沒有你的全部程式碼,所以沒辦法幫你測問題到到底在那! 只能幫你猜測而已! 首先下面是是原來的程式碼,不曉得對不對: < class="code"> Button1的click事件 void __fastcall TForm1::Button1Click(TObject *Sender) { Pic = new TPerformanceCounter();------------------------------- Graphics::TBitmap *pSourceBitmap = Image1->Picture->Bitmap; ↑ Graphics::TBitmap *pDestBitmap = new Graphics::TBitmap(); pDestBitmap->Width = Edit1->Text.ToInt(); pDestBitmap->Height = Edit2->Text.ToInt(); pDestBitmap->PixelFormat = pf24bit; Image1->Width = pDestBitmap->Width; Image1->Height = pDestBitmap->Height; Pic->Begin(); ImageProcess(pSourceBitmap, pDestBitmap);//↓ Pic->End();//------------------------------------------箭頭所指二處整個為A段 pSourceBitmap->Assign(pDestBitmap); delete pDestBitmap; delete Pic; } ToolButton2的click事件 void __fastcall TForm1::ToolButton2Click(TObject *Sender) { Pic = new TPerformanceCounter();------------------------------- Graphics::TBitmap *pSourceBitmap = Image1->Picture->Bitmap; ↑ Graphics::TBitmap *pDestBitmap = new Graphics::TBitmap(); pDestBitmap->Width = Edit1->Text.ToInt(); pDestBitmap->Height = Edit2->Text.ToInt(); pDestBitmap->PixelFormat = pf24bit; Image1->Width = pDestBitmap->Width; Image1->Height = pDestBitmap->Height; Pic->Begin(); ImageProcess(pSourceBitmap, pDestBitmap);//↓ Pic->End();//------------------------------------------箭頭所指二處整個為A段 pSourceBitmap->Assign(pDestBitmap); Edit1->Text = IntToStr(pSourceBitmap->Width*125/100); 就是多了這兩行,其它都一樣 Edit2->Text = IntToStr(pSourceBitmap->Height*125/100); delete pDestBitmap; delete Pic; } 我的建議使用事件 void __fastcall TForm1::MyButtonClick(TObject* Sender) { Pic = new TPerformanceCounter(); Graphics::TBitmap *pSourceBitmap = Image1->Picture->Bitmap; Graphics::TBitmap *pDestBitmap = new Graphics::TBitmap(); pDestBitmap->Width = Edit1->Text.ToInt(); pDestBitmap->Height = Edit2->Text.ToInt(); pDestBitmap->PixelFormat = pf24bit; Image1->Width = pDestBitmap->Width; Image1->Height = pDestBitmap->Height; Pic->Begin(); ImageProcess(pSourceBitmap, pDestBitmap);// Pic->End(); pSourceBitmap->Assign(pDestBitmap); if (AnsiString(Sender->ClassName())=="TToolButton") { Edit1->Text = IntToStr(pSourceBitmap->Width*125/100); Edit2->Text = IntToStr(pSourceBitmap->Height*125/100); } delete pDestBitmap; delete Pic; } 以上如果都沒錯的話,應該是不會有你所說的問題,如果有的話,那應該你原來的程式也會有,建議你可 以同樣的操作步驟測試你原來的程式和我的範例,做為比對依據! 另外再加一個TToolButton3的話,判斷可以是:

if (AnsiString(Sender->ClassName())=="TToolButton")
{
 TToolButton* now_btn=(TToolButton*)Sender;
 
 if (now_btn->Name=="ToolButton2")
     //do something
     
 if (now_btn->Name=="ToolButton3")
     // do something
}    
就是類似這樣的一個同理類推而已!也就是說比如今天有十個button共用一個事件的話,當然你要在事件 中判斷是那一個button驅動這事件的,那判斷的方法也就是那幾招了! All!
系統時間:2024-06-26 22:05:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!