2010-11-30 4 views
0

私はborlandのC++ビルダーでカスタム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ではなくTopまたはLeftプロパティを設定するので、彼らが設定されます。デフォルトでは0に設定されています。つまり、親コンポーネントの左上になります。

既存のボタンのキャプションを変更しようとし、代わりに新しいボタンを作っている場合は、単にdynamic_cast実行するので、あなたはちょうどあなたが安全にif文で行うことができますチェックしたボタン(にコンポーネントをキャストそれは安全にキャスト)などのように、代わりにそれを変更できない場合、クラスが関連して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

ありがとうございました。これだよ。 –

+0

素晴らしい!それを知ってうれしい:)あなたはupvoteし、答えとしてそれを受け入れることができますか?あなたはそれの隣のチェックマークをクリックすることでそれを行うことができます。 –

+0

aMsgDlgフォームでCreateparamsを使用する方法はありますか? –

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(); 
} 
//---------------------------------------------------------------------------