自定义MessageDlg

时间:2010-11-30 20:22:38

标签: c++ c++builder

我正在尝试在borland c ++ builder中创建自定义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 个答案:

答案 0 :(得分:3)

它创建了新按钮,因为它实际上是在行中创建新按钮:

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];
}

这将允许您修改现有按钮而不是创建新按钮。

答案 1 :(得分: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();
}
//---------------------------------------------------------------------------