2010-11-30 47 views
0

我想在borland C++ builder中自定義messagedlg。但是代碼如下改爲改變按鈕標題,在對話框的左上角創建新的按鈕。自定義MessageDlg

#include <vcl.h> 
#pragma hdrstop 

#include "frmMsgDlg.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
    : TForm(Owner) 
{ 
} 
int __fastcall MsgDlg(const String Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, const String* Captions, int Captions_maxidx) 
{ 
    int m = 0; 
    TButton *DlgButton; 
    int CaptionIndex = 0; 
    TForm* aMsgDlg = CreateMessageDialog(Msg, DlgType, Buttons); 
    aMsgDlg->Color = TColor(clWindow); 

    for (m = 0; m <= aMsgDlg->ComponentCount - 1; m++) 
    { 
    if (dynamic_cast< TButton *>(aMsgDlg->Components[m])) 
    { 
     DlgButton = new TButton(aMsgDlg->Components[m]); 
     DlgButton->Parent = aMsgDlg; 
     if (CaptionIndex > Captions_maxidx /*# High(Captions) */) break; 

     DlgButton->WordWrap = true; 
     DlgButton->Caption = Captions[CaptionIndex]; 
     DlgButton->Width = 56; 
     DlgButton->Height= 28; 
     DlgButton->Cancel = false; 
     CaptionIndex++; 
    } 
    } 
    return aMsgDlg->ShowModal(); 

} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::FormShow(TObject *Sender) 
{ 
    int msg; 
    String capt[2] = {"PO","JO"}; 

    msg = MsgDlg("Tre tentime dështuan, ju lutem startoni programin nga ikona!!!", mtInformation,TMsgDlgButtons() << mbOK <<mbCancel,capt,2); 

} 
//--------------------------------------------------------------------------- 

回答

2

它創建新的按鈕,因爲它實際上是創造新的按鈕,在該行:

DlgButton = new TButton(aMsgDlg->Components[m]); 

您設置WidthHeight,但不是TopLeft屬性,因此它們將被設置默認爲0 - 即父組件的左上角。

如果你想更改現有按鈕的標題,而不是做一個新的按鈕,簡單地把組件按鈕(您剛纔檢查您可以在if聲明中安全地完成,因爲dynamic_cast執行看到一個運行時檢查,如果類是相關的,返回NULL如果不能安全地轉換),並修改它,而不是像這樣:

if (dynamic_cast<TButton*>(aMsgDlg->Components[m])) { 
    DlgButton = dynamic_cast<TButton*>(aMsgDlg->Components[m]) // <- this is the key difference 
    if (CaptionIndex > Captions_maxidx /*# High(Captions) */) break; 
    DlgButton->WordWrap = true; 
    DlgButton->Caption = Captions[CaptionIndex]; 
} 

這將允許你修改,而不是創建新的現有的按鈕。

+0

非常感謝。就是這個。 – 2010-12-01 08:02:35

0

我已經犧牲了一點時間來擴展原始消息對話框按鈕修飾符。你可以把這個全部剪下來,它會運行。

//--------------------------------------------------------------------------- 
#include <vcl.h> 
#pragma hdrstop 

#include "frmMsgDlg.h" 
#include <memory> 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 

int MsgDlg(const String Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, TStringList* Captions); 

TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
    : TForm(Owner) 
{ 
} 
//--------------------------------------------------------------------------- 

int MsgDlg(const String Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, TStringList* Captions) 
{ 
    TForm* aMsgDlg = CreateMessageDialog(Msg, DlgType, Buttons); 
    int i, j; 
    TButton *DlgButton; 

    aMsgDlg->Color = clWindow; 

    for (i = 0; i <= aMsgDlg->ComponentCount - 1; i++) { 
     DlgButton = dynamic_cast<TButton*>(aMsgDlg->Components[i]); 

     if (DlgButton) { 
       switch (DlgButton->ModalResult) { 
       case mrOk: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbOK)); 
        break; 
       case mrCancel: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbCancel)); 
        break; 
       case mrAbort: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbAbort)); 
        break; 
       case mrRetry: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbRetry)); 
        break; 
       case mrIgnore: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbIgnore)); 
        break; 
       case mrYes: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbYes)); 
        break; 
       case mrNo: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbNo)); 
        break; 
       case mrAll: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbAll)); 
        break; 
       case mrNoToAll: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbNoToAll)); 
        break; 
       case mrYesToAll: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbYesToAll)); 
        break; 
       case mrClose: 
        j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbClose)); 
        break; 
       default: 
        j = -1; 
      } 

      if (j != -1) { 
       DlgButton->WordWrap = true; 
       DlgButton->Caption = Captions->Strings[j]; 
       DlgButton->Width = 56; 
       DlgButton->Height = 28; 
       DlgButton->Cancel = false; 
      } 
     } 
    } 

    return aMsgDlg->ShowModal(); 
} 
//--------------------------------------------------------------------------- 

void __fastcall TForm1::FormShow(TObject *Sender) 
{ 
    int rc; 
    std::auto_ptr<TStringList>capt(new TStringList()); 

    capt->AddObject("mbYes", reinterpret_cast<TObject *>(mbYes)); 
    capt->AddObject("mbNo", reinterpret_cast<TObject *>(mbNo)); 
    capt->AddObject("mbOK", reinterpret_cast<TObject *>(mbOK)); 
    capt->AddObject("mbCancel", reinterpret_cast<TObject *>(mbCancel)); 
    capt->AddObject("mbAbort", reinterpret_cast<TObject *>(mbAbort)); 
    capt->AddObject("mbRetry", reinterpret_cast<TObject *>(mbRetry)); 
    capt->AddObject("mbIgnore", reinterpret_cast<TObject *>(mbIgnore)); 
    capt->AddObject("mbAll", reinterpret_cast<TObject *>(mbAll)); 
    capt->AddObject("mbNoToAll", reinterpret_cast<TObject *>(mbNoToAll)); 
    capt->AddObject("mbYesToAll", reinterpret_cast<TObject *>(mbYesToAll)); 
    capt->AddObject("mbClose", reinterpret_cast<TObject *>(mbClose)); 

    ::MsgDlg("Tre tentime dështuan, ju lutem startoni programin nga ikona!!!", 
     mtInformation, 
     TMsgDlgButtons() << mbYes << mbNo << mbOK << mbCancel << mbAbort << 
     mbRetry << mbIgnore << mbAll << mbNoToAll << mbYesToAll << mbClose, 
     capt.get()); 

    Close(); 
} 
//---------------------------------------------------------------------------